2017-09-25 127 views
0

在這裏,我有一個疑問,我知道它與你的人看起來不相關,但我需要得到它。問題是我們可以將輸入類型分配給像這樣的變量。我們如何將輸入類型分配給一個變量

<?php $option = '<input type="text" name="project_id" id="pr_id"/>'; 
var_dump($option);?> 

這裏pr_id包含一個值,並把它分配給一個變量

+0

我不確定我知道你在問什麼。你能稍微解釋一下還是包括一些例子? –

+0

Php在服務器端執行,因此您需要使用POST請求將輸入的值發送到php。 – Stefan

+1

你想要什麼?目前,您將HTML元素設置爲字符串變量。你是否想要參考輸入,以便讀取它的值?你想用某個變量替換'pr_id'嗎? – Glubus

回答

0

肯定有想法,你可以將任何東西!這是我剛剛建立了一個輸入對象,看看這個:

<?php 

class Input 
{ 
    /** @var array $attributes */ 
    private $attributes = []; 

    /** 
    * @param $key 
    * @return mixed|string 
    */ 
    public function getAttribute($key) 
    { 
     return isset($this->attributes[$key]) ? $this->attributes[$key] : null; 
    } 

    /** 
    * @param $key 
    * @param $value 
    * @return $this 
    */ 
    public function setAttribute($key, $value) 
    { 
     $this->attributes[$key] = $value; 
     return $this; 
    } 

    /** 
    * @param array $attributes 
    * @return $this 
    */ 
    public function setAttributes(array $attributes) 
    { 
     $this->attributes = $attributes; 
     return $this; 
    } 

    /** 
    * @return array 
    */ 
    public function getAttributes() 
    { 
     return $this->attributes; 
    } 

    public function render() 
    { 
     $html = '<input '; 
     foreach ($this->getAttributes() as $key => $val) { 
      $html .= $key.'="'.$val.'" '; 
     } 
     $html .= '/>'; 
     return $html; 
    } 
} 

所以,你現在可以生成與下面的代碼輸入:

$input = new Input(); 
$input->setAttribute('id', 'pr_id') 
     ->setAttribute('name', 'project_id') 
     ->setAttribute('type', 'text'); 

echo $input->render(); 

,輸出:

<input id="pr_id" name="project_id" type="text" />

這裏玩吧:https://3v4l.org/sAiWd

相關問題