开发者

echo the value of a checkbox

I'm just trying to make sure the value of my checkbox is set to the ID

 <td align="center" ><input type="checkbox" name="bob[]" value="<?php echo $row_contactlist['contact_id']; ?>"></td>
      <td align="cent开发者_JAVA技巧er" ><p><?php echo $row_contactlist['contact_id'];?></p></td>
      <td align="center" ><p><?php echo $_POST['bob']; ?></p></td>

The $_POST['bob'] does not return anything, but the echo $row_contactlist works just fine.

What am I missing?


Two things :

  • First of all, when $_POST will only contain data when the form has been submitted (not when it's displayed for the first time)
  • Then, you name your checkbox bob[]
    • This means PHP will receive an array for bob
    • If you use var_dump($_POST), you'll see how $_POST['bob'] looks like : an array -- which can contain several values, depending on the number of checkboxes you have.


The data will appear only if:

  • The form has been submitted
  • The checkbox is checked

You also need to reference it correctly. PHP will convert form controls with a name ending in [] into an array, so you need to access it as an array.

  • $_POST['bob'][0]


To get the value of each check box, instead of typing everything as $_POST['bob'][0], you can put it inside loop.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜