English 中文(简体)
POST阵列没有显示未经检查的检查箱
原标题:POST arrays not showing unchecked Checkboxes

遇到麻烦,难以从我的表格中显示所有检查箱值。

我的表格如下:

<form name= foo  method= post  action=  >
    <table>
       <tr>
          <td class= bla >Checkbox: <input type= checkbox  name= cBox[] /></td>
      </tr>
       <tr>
          <td class= bla >Checkbox: <input type= checkbox  name= cBox[] /></td>
      </tr>
       <tr>
          <td class= bla >Checkbox: <input type= checkbox  name= cBox[] /></td>
      </tr>
   </table>
</form>

我的底部有一顿,由支部职能承担,该功能使表格中增加了5个空档(投入名称的阵列)。

现在,问题。 让我们说,第一个检查箱没有受到检查,最后2个检查箱也受到检查。 在我产出价值时(使用购买力平价打印机进行分解),我将:

Array ( [0] => on [1] => on)

由于某种原因,这些阵列没有包含任何未经检查的检查箱的价值。

I have seen some solutions where a hidden variable is passed with each checkbox, but can this solution be implemented in my situation (using arrays)?

最佳回答

这种行为并不令人惊讶,因为浏览器没有提交任何未经检查的检查箱的价值。

If you are in a situation where you need to submit an exact number of elements as an array, why don t you do the same thing you do when there s an id of some sort associated with each checkbox? Just include the PHP array key name as part of the <input> element s name:

  <tr>
                                                       <!-- NOTE [0] --->
      <td class= bla >Checkbox: <input type= checkbox  name= cBox[0] /></td>
  </tr>
   <tr>
      <td class= bla >Checkbox: <input type= checkbox  name= cBox[1] /></td>
  </tr>
   <tr>
      <td class= bla >Checkbox: <input type= checkbox  name= cBox[2] /></td>
  </tr>

这仍然使你面临这样的问题:在阵列中,箱子仍然不会存在。 这可能或不会成为问题。 一个人可能确实不关心:

foreach($incoming as $key => $value) {
    // if the first $key is 1, do you care that you will never see 0?
}

即便是你的照顾,你也很容易纠正这一问题。 这里有两种直截了当的做法。 一种是,隐藏的输入要素是:

  <tr>
      <td class= bla >
        <input type="hidden" name="cBox[0]" value="" />
        Checkbox: <input type= checkbox  name= cBox[0] />
      </td>
  </tr>
   <tr>
      <td class= bla >
        <input type="hidden" name="cBox[1]" value="" />
        Checkbox: <input type= checkbox  name= cBox[1] />
      </td>
  </tr>

And two, which I find preferable, fill in the blanks from PHP instead:

// assume this is what comes in:
$input = array(
     1  =>  foo ,
     3  =>  bar ,
);

// set defaults: array with keys 0-4 all set to empty string
$defaults = array_fill(0, 5,   );

$input = $input + $defaults;
print_r($input);

// If you also want order, sort:

ksort($input);
print_r($input);

<http://ideone.com/Pu4fh”rel=“noretinger”> 见行动

问题回答

如果有检查,一个国家将超越检查箱的价值。 否则,其价值将是0。

<form>
  <input type= hidden  value= 0  name="smth">
  <input type= checkbox  value= 1  name="smth">
</form>

提 出

<input type= checkbox  value="XXX" name= cBox[] />
<input type= checkbox  value="YYY" name= cBox[] />
<input type= checkbox  value="ZZZ" name= cBox[] />

检查箱的工作就是这样。 如果加以核对,则只公布价值。

如果你处理动态检查箱阵列,你可以尝试:

传真:

<label>
  <input type="hidden" name="cBox[]" value="" />
  <input type="checkbox" class="checkbox" value="on" />
</label>
<label>
  <input type="hidden" name="cBox[]" value="" />
  <input type="checkbox" class="checkbox" value="on" />
</label>
<!-- extend -->

Javascript (jQuery):

$(document).on("change", "input.checkbox", function() {

    var value = $(this).is(":checked") ? $(this).val() : null;

    $(this).siblings("input[name= cBox[] ]").val(value);    
});

反馈结果 (只检查第二个):

// PHP $_POST[ cBox ]
Array
(
    [0] => 
    [1] => on
)

In this implement, the checkboxes are used to controller each hidden input in a group.

显示页 你们可以把价值分配到隐藏的投入和标识核对箱中,作为<代码>checked<>。

对每个检查箱设定价值1或1。

<input type= checkbox  value= 1  name= cBox[1] />

这可能是为什么不寄任何东西?

控制器:

request()->joint([cBox => request()->input(cBox , []);





相关问题
CSS working only in Firefox

I am trying to create a search text-field like on the Apple website. The HTML looks like this: <div class="frm-search"> <div> <input class="btn" type="image" src="http://www....

image changed but appears the same in browser

I m writing a php script to crop an image. The script overwrites the old image with the new one, but when I reload the page (which is supposed to pickup the new image) I still see the old one. ...

Firefox background image horizontal centering oddity

I am building some basic HTML code for a CMS. One of the page-related options in the CMS is "background image" and "stretch page width / height to background image width / height." so that with large ...

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签