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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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 Quote Link to comment 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 } Quote Link to comment 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 Quote Link to comment 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 ; Quote Link to comment 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 Quote Link to comment 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) . "')"); ?> Quote Link to comment 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. Quote Link to comment Share on other sites More sharing options...
Dragen Posted May 1, 2008 Share Posted May 1, 2008 glad I could help 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.