2017-09-25 131 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

相关问题