Jump to content

Archived

This topic is now archived and is closed to further replies.

hanlonj

Testing for Which Checkbox is Checked

Recommended Posts

Hi,

I have a form as follows:

[code]
<?php
<form id='form1' name='form' method='post' action='checkout.php'>
  <table width='100%' border='0' cellspacing='2' cellpadding='2'>
    <tr>
      <td width='200'>Title:</td>
      <td><input name='title' type='text' id='title' /></td>
    </tr>
    <tr>
      <td>Fruits:</td>
      <td><input type='checkbox' name='fruit[]' value='001' />
        Apple<br />
        <input type='checkbox' name='fruit[ ]' value='002' />
        Pear<br />
        <input type='checkbox' name='fruit[ ]' value='003' />
        Banana<br />
        <input type='checkbox' name='fruit[]' value='004' />
        Grapefruit<br />
        <input type='checkbox' name='fruit[]' value='005' />
        Pineapple<br />
        <input type='checkbox' name='fruit[]' value='006' />
        Mango</td>
    </tr>
    <tr>
      <td>Description:</td>
      <td><textarea name='description' cols='50' rows='5' id='description'></textarea></td>
    </tr>
    <tr>
      <td>Postal code: </td>
      <td><input name='postalcode' type='text' id='postalcode' size='2' maxlength='2'>
        -
        <input name='postalcode2' type='text' id='postalcode2' size='3' maxlength='3'></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type='submit' name='Submit' value='Submit' /></td>
    </tr>
  </table>
</form>
?>
[/code]

I want to be able to check which boxes are ticked? Can someone just give me the basic syntax of how to check the first two boxes?

Also, to use an array of checkboxes as above, does teh form have to be generated by a php script or can I use the same in a html form?

John

Share this post


Link to post
Share on other sites
What your looking for would really be done better with plain ole' JavaScript.  The best PHP can do is tell you if it's in a $_POST / $_GET array.

Javascript on the other hand can do it on the client side with something like document.getElementByID();

Or you can get a bit more hefty and throw some AJAX in their to get some really cool interaction in the UI.

Share this post


Link to post
Share on other sites
Do not rely on client side validation. What happens when someone accesses your form with Javascript disabled?

It is very easy in PHP to tell which checkboxes are checked, if you change your form slightly:
[code]
<form id='form1' name='form' method='post' action='checkout.php'>
  <table width='100%' border='0' cellspacing='2' cellpadding='2'>
    <tr>
      <td width='200'>Title:</td>
      <td><input name='title' type='text' id='title' /></td>
    </tr>
    <tr>
      <td>Fruits:</td>
      <td><input type='checkbox' name='fruit[1]' value='apple' />
        Apple<br />
        <input type='checkbox' name='fruit[2]' value='pear' />
        Pear<br />
        <input type='checkbox' name='fruit[3]' value='banana' />
        Banana<br />
        <input type='checkbox' name='fruit[4]' value='grapefruit' />
        Grapefruit<br />
        <input type='checkbox' name='fruit[5]' value='pineapple' />
        Pineapple<br />
        <input type='checkbox' name='fruit[6]' value='mango' />
        Mango</td>
    </tr>
    <tr>
      <td>Description:</td>
      <td><textarea name='description' cols='50' rows='5' id='description'></textarea></td>
    </tr>
    <tr>
      <td>Postal code: </td>
      <td><input name='postalcode' type='text' id='postalcode' size='2' maxlength='2'>
        -
        <input name='postalcode2' type='text' id='postalcode2' size='3' maxlength='3'></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td><input type='submit' name='submit' value='Submit' /></td>
    </tr>
  </table>
</form>[/code]
Then the code to process this form (at least the checkboxes) would be something like:
[code]<?php
if (isset($_POST['fruit']))
  echo 'The following were checked: ' . ucwords(implode(', ',$_POST['fruit'])) .'<br>';
?>[/code]

This works since only the values of the checked boxes are returned.

When a form is generated by PHP, it is plain HTML, so it will work, no matter how it is generated.

Ken

Share this post


Link to post
Share on other sites
If you want to use a form with multiple checkboxes (e.g. one per row) and assign the same name to each checkbox then the name needs to end with []. This tells PHP to put all checked values into an array variable.
For example:
<input type="checkbox" name="id[]" value="value_1">
<input type="checkbox" name="id[]" value="value_2">
..
<input type="checkbox" name="id[]" value="value_x">

You can now retrieve all values by using:
  $values = $_POST['id'];

If the name does not end with [], then only a single value will be available via the $_POST variable even if the user checks several checkboxes.


Reference: http://us3.php.net/reserved.variables

Share this post


Link to post
Share on other sites
Thanks for that.

And if I just want to check that at least one checkbox is ticked?What's the basic "if" statement required?

John

Share this post


Link to post
Share on other sites
If you want to know that at least one checkbox is checked, use something like this:
[code]<?php
if (isset($_POST['submit']))
if (count($_POST['fruit']) == 0) echo '<span style="color:red">Error no boxes were checked</span>';
  else echo 'The following were checked: ' . ucwords(implode(', ',$_POST['fruit'])) .'<br>';
?>[/code]

Ken

Share this post


Link to post
Share on other sites
remember that is your check box is not checked there will be no post for that. So if you have a checkbox named : box you would need to

if (isset($_POST['box']))
{
$value=$_POST['box'];
}
else
{
value=NULL;
}

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.