使用数组创建PHP表单时,类出现问题

时间:2019-03-21 13:17:49

标签: php arrays

一切正常,但是当我将两个标签放在一起时,会创建第二个标签,但不是第一个标签

<?php 
require 'header/security-class.php';

class SecureForm{

    function form($action ,$method ,$InputArray ,$button=false){
        global $security;
        $form = '';
        $form .= "<form action='$action' method='$method'>";
        $form .= "<input type='hidden' name='token' value= '". $security->GenerateTokens(3, 20) ."' />";

        foreach ($InputArray as $input=>$key){
            // for label elemnt
            //requier contect in attribute
            if($input == 'label' ){
                $form .= "<label ";
                foreach($key as $attribute=>$value){
                    $form .= $attribute."='".$value."' ";
                }
                $form .= ">";
                $form .= $key['content'];
                $form .="</label>";
            }else{
                //for input element
                //esay to make type by input:type
                //else you can make it in attrbuits

                $type = explode(':',$input);
                if($type[0] == "input")$elemnt = $type[0];
                if($type[0] == "input" and count($type) > 1 ){
                $form .= "<". $type[0] ." type ='". $type[1] ."' "; 
                }elseif($type[0] == "input" and count($type) > 1 ){
                    $form .= "<input ";
                }else{
                    $form .= "<$input ";
                }
                foreach($key as $attribute=>$value)
                $form .= $attribute."='".$value."' ";

                if($type[0] == "input"){
                    $form .= '/>';
                }else{
                    $form .= " ></$input>";
                }

            }
        }

        if(is_array($button)){
            $form .= '<button name="'.$button['name'].'" class="'.$button['class'].'">'.
            $button['content'].'</button>';
        }

        $form .= "</form>";

        return $form;
    }
}

$form = new SecureForm;

$InputArray = array(
    'input:text' => array(
        'id'=>'input',
        'class'=>'input',
        'max' =>'250'
    ),
    'input:text' => array(
        'name'=>'button',
        'value'=>'name',
        'id'   =>'button'
    )
);
echo $form->form('#','post',$InputArray );

//结果

<input type="text" name="button" value="name" id="button">

//必须为

<input type="text" id="input" class="input" max="250">
<input type="text" name="button" value="name" id="button">

这是怎么了? 因为我在很多代码中进行了测试 两个textarea,隐藏或输入 但我解决不了

抱歉,但是我必须写任何东西,因为这个问题

1 个答案:

答案 0 :(得分:2)

您正在重写数组索引。

$InputArray = array(
//  v----------v--------------- first index name
    'input:text' => array(
        'id'=>'input',
        'class'=>'input',
        'max' =>'250'
    ),
//  v----------v--------------- Same index name, the last value will be kept
    'input:text' => array(
        'name'=>'button',
        'value'=>'name',
        'id'   =>'button'
    )
);
提供此var_dump($InputArray);

array(1) {
  ["input:text"]=>
  array(3) {
    ["name"]=>
    string(6) "button"
    ["value"]=>
    string(4) "name"
    ["id"]=>
    string(6) "button"
  }
}

您可能要使用适当的唯一索引,并在一个值中单独使用输入类型,例如:

$InputArray = array(
     array(
        'input_type' => 'input:text',
        'id'=>'input',
        'class'=>'input',
        'max' =>'250'
    ),
    array(
        'input_type' => 'input:text',
        'name'=>'button',
        'value'=>'name',
        'id'   =>'button'
    )
);

然后,在您的循环中,而不是使用$input,请使用适当的索引:

foreach ($InputArray as $value) {
...
    if($value['input_type'] == 'label' ) {
    ...
相关问题