spillage Posted May 1, 2008 Share Posted May 1, 2008 Hi, I have a html form with four checkboxes. Trying to get it so any boxes ticked will be sent to db. Do I have to have a single variable for each. This is the html checkbox: <form action="http://localhost/mail01.php" method="post"> HP: <input type="checkbox" name="printer" value="hp"> Epson: <input type="checkbox" name="printer" value="epson"> Lexmark: <input type="checkbox" name="printer" value="lex"> Dell: <input type="checkbox" name="printer" value="dell"> </form> Thought I needed a switch statement but stupidly realised this only holds the last box chosen. Could I change it to: <form action="http://localhost/mail01.php" method="post"> HP: <input type="checkbox" name="printer" value="$hp"> Epson: <input type="checkbox" name="printer" value="$epson"> Lexmark: <input type="checkbox" name="printer" value="$lex"> Dell: <input type="checkbox" name="printer" value="$dell"> </form> $choice=$_POST['$hp','$epson','$lex','$dell']; Cheers, Spill Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/ Share on other sites More sharing options...
craygo Posted May 1, 2008 Share Posted May 1, 2008 change the name to printer[]. this will store the data in an array <form action="http://localhost/mail01.php" method="post"> HP: <input type="checkbox" name="printer[]" value="hp"> Epson: <input type="checkbox" name="printer[]" value="epson"> Lexmark: <input type="checkbox" name="printer[]" value="lex"> Dell: <input type="checkbox" name="printer[]" value="dell"> </form> Now you can get the data from $_POST['printer'] print_r($_POST['printer']) Ray Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531245 Share on other sites More sharing options...
Dragen Posted May 1, 2008 Share Posted May 1, 2008 use an array: <form action="http://localhost/mail01.php" method="post"> HP: <input type="checkbox" name="printer[]" value="hp"> Epson: <input type="checkbox" name="printer[]" value="epson"> Lexmark: <input type="checkbox" name="printer[]" value="lex"> Dell: <input type="checkbox" name="printer[]" value="dell"> </form> That will create an array such as: $_POST['printer'] = array('hp', 'lex', dell'); meaning that they want all but the epson. EDIT: beaten to it Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531246 Share on other sites More sharing options...
spillage Posted May 1, 2008 Author Share Posted May 1, 2008 Thanks very much everyone. Was thinking of array but just couldn't get my brain into gear. Thanks allot. Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531247 Share on other sites More sharing options...
spillage Posted May 1, 2008 Author Share Posted May 1, 2008 Just a quick question. would I use somthing like $cust_pri = (array($_POST['printer'])) and mysql_query("INSERT INTO submit VALUES ('$id', '$cust.', '$friend', '$time','$cust_pri')"); or would this just print array. Thanks Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531274 Share on other sites More sharing options...
phpSensei Posted May 1, 2008 Share Posted May 1, 2008 change name to printer[] then use foreach($_POST['printer'] as $item){ // insert $item into db } Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531276 Share on other sites More sharing options...
spillage Posted May 1, 2008 Author Share Posted May 1, 2008 Maybe I should also add that there is also 2 email address to be posted aswell. I have tried foreach ($_POST['printer'] as $row=>$pri){ $cust_pri=$pri; } but only the last checkboxes data is entered into the db. should it not print both. thanks Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531284 Share on other sites More sharing options...
Dragen Posted May 1, 2008 Share Posted May 1, 2008 <?php $cust_pri = (array($_POST['printer'])) mysql_query("INSERT INTO `submit` VALUES ('$id', '$cust.', '$friend', '$time', '" . implode(';', $cust_pri) . "')"); ?> That will insert them into the database, in the same column. Each printer name will be seperated by an ; Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531288 Share on other sites More sharing options...
spillage Posted May 1, 2008 Author Share Posted May 1, 2008 sorry to be a nooby pain but this only prints out Array in the db. Cheers Spill Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531304 Share on other sites More sharing options...
Dragen Posted May 1, 2008 Share Posted May 1, 2008 ah sorry. Probably because you're setting $cust_pri as an array, when you doen't need to because $_POST['printer'] is one. try this: <?php $cust_pri = $_POST['printer']; mysql_query("INSERT INTO `submit` VALUES ('$id', '$cust.', '$friend', '$time', '" . implode(';', $cust_pri) . "')"); ?> Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531318 Share on other sites More sharing options...
spillage Posted May 1, 2008 Author Share Posted May 1, 2008 Thank very much. Thats one more rung up the ladder. Cheers, Spil. Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531328 Share on other sites More sharing options...
Dragen Posted May 1, 2008 Share Posted May 1, 2008 glad I could help Link to comment https://forums.phpfreaks.com/topic/103760-solved-checkboxes/#findComment-531329 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.