hanlonj Posted September 27, 2006 Share Posted September 27, 2006 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> </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 Quote Link to comment Share on other sites More sharing options...
phporcaffeine Posted September 27, 2006 Share Posted September 27, 2006 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. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 27, 2006 Share Posted September 27, 2006 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> </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]<?phpif (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 Quote Link to comment Share on other sites More sharing options...
tobes Posted September 27, 2006 Share Posted September 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
hanlonj Posted September 27, 2006 Author Share Posted September 27, 2006 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 Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted September 27, 2006 Share Posted September 27, 2006 If you want to know that at least one checkbox is checked, use something like this:[code]<?phpif (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 Quote Link to comment Share on other sites More sharing options...
steveclondon Posted September 27, 2006 Share Posted September 27, 2006 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 toif (isset($_POST['box'])){$value=$_POST['box'];}else{value=NULL;} Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.