2016-12-05 65 views
0

我有问题,如何使用命名数组输入字段。 因为在这里我有一个像每一个问题的情况有不同的答案,所以我设计的HTML这样HTML数组输入字段名称 - 在PHP中获取值

<div class="form-group"> 
    <label for="" id=""><?=$data->question?></label> 
    <?php 
     if (isset($data->answer)) { 
      $answer = explode("|", $data->answer); 
     } 
    ?> 
    <div class="input-container"> 
     <div class="radio-inline"> 
      <label> 
       <input type="radio" name="<?=$data->id?>[]" value="yes" <?=((isset($answer[0]))? (($answer[0] == "yes") ? "checked" : ""): '')?> > 
       Yes 
      </label> 
     </div> 
     <div class="radio-inline"> 
      <label> 
       <input type="radio" name="<?=$data->id?>[]" value="no" <?=((isset($answer[0]))? (($answer[0] == "no") ? "checked" : ""): '')?> > 
       No 
      </label> 
     </div> 
     <div class="radio-inline"> 
      <label> 
       <input type="radio" name="<?=$data->id?>[]" value="dont_know" <?=((isset($answer[0]))? (($answer[0] == "dont_know") ? "checked" : ""): '')?> > 
       Don't Know 
      </label> 
     </div> 
    </div> 
    <div class="input-container"> 
     <div class="checkbox"> 
      <label> 
       <input type="checkbox" id="" name="<?=$data->id?>[]" value="1" <?=(isset($answer[1])=='1')?'checked':'' ?>> Check if recent change 
      </label> 
     </div> 
    </div> 
</div> 

这样我有各种不同的答案意见(这里其交易一方从无线电,如果它是最近更改或不是复选框,有时候两者都有,有时候有人 - 没有验证通过这个)

所以这里目前有“|”在数据库中已分离的答案blob,但是我想将它更改为JSON,就好像用户选择了一样(是和复选框)/(没有收音机复选框)/(只有收音机没有复选框){["0"=> 'yes', "1"=>1], ["0"=> null, "1"=>1], ["0"=> 'yes', "1"=>null]} 由此我可以在编辑模式下设置数据。所以在HTML/PHP中,我怎么能得到它?

+0

对可能由用户输入/选择的值使用命名数组字段。但是无线电领域被用于像是/否这样的奇异值。对于多个值使用复选框(NamedArray场),并尝试以namedArray(对于复选框)和奇异领域,如单选按钮,输入等方面给予不同的名称 –

+0

@Hiren感谢您的答复,我改变这样的复选框的名字=” ID ?> [] [复选框]“,但仍然有什么值选择只来到PHP ..但我想如果不选择得到null种类 –

回答

0

我没有正确理解你的问题,所以我写了一些示例代码。

<div class="form-group"> 
<?php 
    if (isset($_POST)) { 
      echo "<pre>". json_encode($_POST) . "</pre>"; 
    } 
?> 
<form action="" method="post"> 
    <div class="input-container"> 
     Single Value Question: 
     <div class="radio-inline"> 
      <label> 
       <input type="radio" name="singleAnswer" value="yes" > 
       Yes 
      </label> 
     </div> 
     <div class="radio-inline"> 
      <label> 
       <input type="radio" name="singleAnswer" value="no"> 
       No 
      </label> 
     </div> 
     <div class="radio-inline"> 
      <label> 
       <input type="radio" name="singleAnswer" value="dont_know"> 
       Don't Know 
      </label> 
     </div> 
    </div> 
    <div class="input-container"> 
    Multiple answer question 
     <div class="checkbox"> 
      <label> 
       <input type="checkbox" id="" name="array[first]" value="1" > First choice 
      </label> 
      <label> 
       <input type="checkbox" id="" name="array[second]" value="1" > Scond choice maybe..     
      </label> 
      <label> 
       <input type="checkbox" id="" name="array[third]" value="1" > I want to be null..     
      </label> 
     </div> 
    </div> 
    <button type="submit">Submit Changes</button> 
</form> 
</div> 
  • 这里我添加命名阵列的输入字段(集合[ITEMNAME])为复选框。
  • 单个值的单选按钮(重要)的通用名称。

    但是,您必须检查 传递和不传递的表单值的结果。它可以通过Javascript在客户端进行验证,您可以在没有选择的字段中添加空值,然后通过ajax或标准请求将请求传播到服务器。 并根据$ _POST值,您可以将值设置为null。