Jump to content


Photo

Testing for Which Checkbox is Checked


  • Please log in to reply
6 replies to this topic

#1 hanlonj

hanlonj
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 27 September 2006 - 12:00 AM

Hi,

I have a form as follows:

<?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>
?>

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

#2 phporcaffeine

phporcaffeine
  • Members
  • PipPipPip
  • Advanced Member
  • 361 posts
  • LocationOhio, USA

Posted 27 September 2006 - 12:05 AM

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.
Thanks,

Ryan Huff
President & Founder, MyCodeTree
support@mycodetree.com | http://mycodetree.com

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 September 2006 - 02:16 AM

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:
<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>
Then the code to process this form (at least the checkboxes) would be something like:
<?php
if (isset($_POST['fruit']))
   echo 'The following were checked: ' . ucwords(implode(', ',$_POST['fruit'])) .'<br>';
?>

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

#4 tobes

tobes
  • New Members
  • Pip
  • Newbie
  • 5 posts
  • LocationNH

Posted 27 September 2006 - 02:51 AM

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

#5 hanlonj

hanlonj
  • Members
  • PipPipPip
  • Advanced Member
  • 38 posts

Posted 27 September 2006 - 10:21 AM

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

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 27 September 2006 - 11:52 AM

If you want to know that at least one checkbox is checked, use something like this:
<?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>';
?>

Ken

#7 steveclondon

steveclondon
  • Members
  • PipPipPip
  • Advanced Member
  • 161 posts

Posted 27 September 2006 - 01:19 PM

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;
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users