Red Moon Posted December 5, 2010 Share Posted December 5, 2010 I have encountered a brick wall trying to use checkboxes. I am attempting to create a small form where I can record if a customer's item has been sent. The way I intend it to work is that the name and ID of the customer (stored in the 'customer' table) are retrieved through a query and displayed in two columns. In the third column is a checkbox beside the name and ID of each customer which, when ticked, sends a value to a second table called 'sentorders' for that particular person. As I am just trying to understand the basics of this for now, a list of all customers will be shown for the time being. The default value of the checkbox needs to be 'No' and if the checkbox is ticked the value should become 'Yes'. When a checkbox is ticked, the customer name, ID and current date are sent to the 'sentorders' table. Only if the box is ticked will the details be stored in the table. The problem I having is how do I set up the checkbox to work as described above. I have had a look at other questions online, but don't understand how it works. Any help would be appreciated. My current code is as follows. <?php $query = mysqli_query ($connect, "SELECT name, id FROM customer"); // Create Table echo '<table align="left"> <tr> <td align="left"><b>Name</b></td> <td align="left"><b>ID</b></td> <td align="left"><b>Order Sent</b></td> </tr>'; // Show Name/ID while ($row = mysqli_fetch_array($query)) { echo '<tr><form action="order_sent.php" method="post"><td align="left"> <td align="left">' . $row['name'] . '</a></td> <td align="left">' . $row['id'] . '</td> <input type="checkbox" name="order[];" value=''></tr>'; } echo '</table><p align="left"><input type="submit" name="enter" value="Update"/><input type="hidden" name="enter" value="TRUE" /></form>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/ Share on other sites More sharing options...
Lautarox Posted December 5, 2010 Share Posted December 5, 2010 If you need to work with real time updates of the database, you should read about javascript and ajax Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143306 Share on other sites More sharing options...
laffin Posted December 5, 2010 Share Posted December 5, 2010 Problem #1: echo '<tr><form action="order_sent.php" method="post"><td align="left"> U have this in the while loop, yet the submit button is outside of the while loop. Fix: I suspect that this is one form, so this line should be above the while loop. Problem #2 <input type="checkbox" name="order[]" value=''></tr>' You show the name and id of the particular row, however when it comes to the forum input, u don't specify to which id this checkbox belongs to. U have to let php know somehow. Checkboxes are funny thing, if left uncheck they don't return a name/value pair for the checkbox, when checked, the name/value pair is sent Fix: <input type="checkbox" name="order[]" value='{$row['id']}'></tr>' So when u do check a number of orders, php will have $_POST['order'] as an array with selected id's. but always check if it's set before U add it to a variable if(isset($_POST['order']) $order=$_POST['order] else $order=array(); Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143314 Share on other sites More sharing options...
Red Moon Posted December 5, 2010 Author Share Posted December 5, 2010 Thanks, laffin. How exactly is the value set then when ticked? Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143324 Share on other sites More sharing options...
Pikachu2000 Posted December 5, 2010 Share Posted December 5, 2010 The way you have the field's name= attribute set up with the empty [] square braces after it, the value will be in the $_POST['order'][0] element. Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143328 Share on other sites More sharing options...
Red Moon Posted December 5, 2010 Author Share Posted December 5, 2010 So it should look like this? if(isset($_POST['order'])) { $order= ($_POST['order'][0]); } else { $order=array(); } After that, is it then just a case of using an INSERT INTO query to have each individual customer added to the database? Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143338 Share on other sites More sharing options...
laffin Posted December 5, 2010 Share Posted December 5, 2010 no the code i had is fine remember it's an array so $_POST['order'] will hold array ( 1,5,7,9 ) or if one order array(5) the elements are the order id so u can update of the db either by inserts if using a seperate table or update if its the field is attached to an already existing table foreach($orders as $orderid) mysql_exec("INSERT INTO orders_sent (id,senton) VALUES($orderid,NOW())"); or $order=implode(',',$order); mysql_exec("UPDATE ORDERS senton=NOW() WHERE id IN ($order)"); $order is an array, so u access each element as you would a normal array. Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143365 Share on other sites More sharing options...
Pikachu2000 Posted December 5, 2010 Share Posted December 5, 2010 I would probably check to see if the specific element is set. If it isn't, and I needed to use the value of $order in a database query, I would just initialize it as an empty string. if( isset($_POST['order'][0]) ) { $order = $_POST['order'][0]; } else { $order = ''; } Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143370 Share on other sites More sharing options...
Red Moon Posted December 5, 2010 Author Share Posted December 5, 2010 Thank you very much to both of you! It's working perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/220750-using-multiple-checkboxes/#findComment-1143373 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.