jw12345 Posted April 16, 2007 Share Posted April 16, 2007 Let me say upfront I'm not php or mysql expert - enough to be really dangerous. I have a form on my website that contains three different checkboxes. I have named all 3 of those checkboxes the same thing, with different values. My hope by doing so, when the form is submitted it would insert all the different values checked into the column "type" into mySQL database. But it doesn't... only take the last one checked. So, my question is this - is there any php code I can put in to connect all three of those checkboxes to insert properly into the database? Not sure what the best way is to solve this. Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/ Share on other sites More sharing options...
per1os Posted April 16, 2007 Share Posted April 16, 2007 <input type="checkbox" name="cb[]" value="some value" /> Will put them into an array IE: if (isset($_POST['cb']) && is_array($_POST['cb'])) { foreach ($_POST['cb'] as $value) { print $value . "<br />"; } } Should work, not sure but yea. Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230675 Share on other sites More sharing options...
Wildbug Posted April 16, 2007 Share Posted April 16, 2007 Use arrays. <input type=checkbox name="my_checkbox[]" value=3> <input type=checkbox name="my_checkbox[]" value=2> <input type=checkbox name="my_checkbox[]" value=1> ... $my_checkbox should contain (3,2,1) Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230676 Share on other sites More sharing options...
jw12345 Posted April 16, 2007 Author Share Posted April 16, 2007 the problem with that is ... for it to correctly enter into the database, it must be name="type". If I put a [], then in my database it says "Array" shouldn't display what was inserted? Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230686 Share on other sites More sharing options...
per1os Posted April 16, 2007 Share Posted April 16, 2007 Wow dude, read what we are saying lol <input type=checkbox name="my_checkbox[]" value="type1"> <input type=checkbox name="my_checkbox[]" value="type2"> <input type=checkbox name="my_checkbox[]" value="type3"> <?php if (isset($_POST['cb']) && is_array($_POST['cb'])) { foreach ($_POST['cb'] as $type) { $types .= "type='" . $type . "' "; } } ?> Something along those lines. Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230690 Share on other sites More sharing options...
Wildbug Posted April 16, 2007 Share Posted April 16, 2007 PHP FAQ on arrays in HTML (link) As frost110 has implied: read carefully and think about it. Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230698 Share on other sites More sharing options...
jw12345 Posted April 16, 2007 Author Share Posted April 16, 2007 My bad - like I said, not a pro at all, but should have read more carefully. My apologies. I did copy & paste that directly in there as you had it... didn't alter a thing, and it now it's just blank in the the mySQL database... Will read that link you sent, thank Wildbug... and thanks to everybody for trying to help. I'll get this solved one way or another Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230704 Share on other sites More sharing options...
Barand Posted April 16, 2007 Share Posted April 16, 2007 // the form <input type=checkbox name="type[]" value="type1"> <input type=checkbox name="type[]" value="type2"> <input type=checkbox name="type[]" value="type3"> <?php // processing the form if (isset($_POST['type']) && is_array($_POST['type'])) { $types = join (', ', $_POST['type']; } else $types = ''; // write $types to the db ?> Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230725 Share on other sites More sharing options...
jw12345 Posted April 16, 2007 Author Share Posted April 16, 2007 Hmmmm... when I did that got a syntax error: Parse error: syntax error, unexpected ';' in /home/public_html/form/html/test.php on line 56 Which is: $types = join (', ', $_POST['type']; Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230751 Share on other sites More sharing options...
Wildbug Posted April 16, 2007 Share Posted April 16, 2007 ...missing parenthesis. Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230754 Share on other sites More sharing options...
Barand Posted April 16, 2007 Share Posted April 16, 2007 Sorry, should be $types = join (', ', $_POST['type'] ) ; Quote Link to comment https://forums.phpfreaks.com/topic/47290-inserting-multiple-checkboxes-into-php/#findComment-230761 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.