dvent Posted April 9, 2012 Share Posted April 9, 2012 Hi All, I'm very new to PHP so please bear with me. I have created a form with 3 fields. One field is a multi select / checkbox with the name 'tenCode'. How do I get all of the values selected from this box and add them to my database? My submission code (submit.php) <?php $con = mysql_connect("localhost","dbname","dbpassword"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("dbname", $con); $sql="INSERT INTO tablename (tenFirst,tenPhone,tenID) VALUES ('$_POST[tenFirst]','$_POST[tenPhone]','$_POST[tenID]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } echo "Adding Ten. The page will refresh in 5 seconds."; echo "<meta http-equiv='Refresh' content='5; URL=nextpage.php'/>"; mysql_close($con) ?> Thanks dvent Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/ Share on other sites More sharing options...
synking Posted April 9, 2012 Share Posted April 9, 2012 How are you sending the info.... you need to use the $_POST or $_GET functions depends on how you are sending the information away from the form. it will end up as an array that you can use to grab the data. Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335833 Share on other sites More sharing options...
dcro2 Posted April 9, 2012 Share Posted April 9, 2012 If you name your checkboxes "tenCode[]" like an array, then $_POST['tenCode'] will also be an array containing the values of the checkboxes that were selected. For example, <input type="checkbox" name="tenCode[]" value="pare1"> <input type="checkbox" name="tenCode[]" value="pare2"> <input type="checkbox" name="tenCode[]" value="pare3"> <input type="checkbox" name="tenCode[]" value="pare4"> <input type="checkbox" name="tenCode[]" value="pare5"> results in $_POST = Array ( [tenCode] => Array ( [0] => pare1 [1] => pare3 ) ) How you choose to store that is up to you. You could for example store them separated by a comma or semicolon using implode. $tenCode = implode(';', $_POST['tenCode']); Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335834 Share on other sites More sharing options...
dvent Posted April 9, 2012 Author Share Posted April 9, 2012 Thanks for the replies. I've added my example table here: http://bit.ly/Hslw4X You'll see the box asking for suitable postcodes. That is built using the following code: <div class="mws-form-row"> <label>Suitable Postcodes</label> <div class="mws-form-item large"> <div class="mws-dualbox clearfix"> <div class="mws-dualbox-col1"> <div class="mws-dualbox-filter clearfix"> <label for="box1Filter">Filter</label> <input type="text" id="box1Filter" class="mws-textinput" /> <button type="button" id="box1Clear">CLEAR</button> </div> <select id="box1View" multiple="multiple" size="3"> <option value="1">Code 1</option> <option value="2">Code 2</option> <option value="3">Code 3</option> </select> <span id="box1Counter" class="countLabel"></span> <select id="box1Storage"></select> </div> <div class="mws-dualbox-col2"> <button id="to2" type="button">></button> <button id="allTo2" type="button">>></button> <div class="clear"></div> <button id="allTo1" type="button"><<</button> <button id="to1" type="button"><</button> </div> <div class="mws-dualbox-col3"> <div class="mws-dualbox-filter clearfix"> <label for="box2Filter">Filter</label> <input type="text" id="box2Filter" class="mws-textinput" /> <button type="button" id="box2Clear">CLEAR</button> </div> <select id="box2View" multiple="multiple" size="3" name="tenSuitablePostcodes"></select> <span id="box2Counter" class="countLabel"></span> <select id="box2Storage"></select> </div> </div> </div> </div> My three values are defined in this bit: <select id="box1View" multiple="multiple" size="3"> <option value="1">Code 1</option> <option value="2">Code 2</option> <option value="3">Code 3</option> </select> How can I put these into your example and post them to the DB, separated by a comma? Thanks dvent Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335843 Share on other sites More sharing options...
dcro2 Posted April 9, 2012 Share Posted April 9, 2012 Same principle, just name the <select> "box1View[]" and $_POST['box1View'] will be an array. <select name="box1View[]" id="box1View" multiple="multiple" size="3"> EDIT: $box1View = implode(',', $_POST['box1View']); Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335845 Share on other sites More sharing options...
dvent Posted April 9, 2012 Author Share Posted April 9, 2012 Sorry to be completely thick, but where does this bit go? EDIT: $box1View = implode(',', $_POST['box1View']); dvent Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335849 Share on other sites More sharing options...
dcro2 Posted April 9, 2012 Share Posted April 9, 2012 I just realized what's going on with the two selects on that page haha. Let's say we were going to insert these values into the database. You would store all the values in variables and then insert it into the query which inserts a row in the database. The fields in your first code don't match up here so I'm making this up. This goes in submit_add_tenant.php: $forename = mysql_real_escape_string($_POST['tenForename']); $surname = mysql_real_escape_string($_POST['tenSurname']); $tenSuitablePostcodes = mysql_real_escape_string(implode(',', $_POST['tenSuitablePostcodes'])); $sql = "INSERT INTO table (`tenForename`, `tenSurname`, `tenSuitablePostcodes`) VALUES ('$forename', '$surname', '$tenSuitablePostcodes')"; $res = mysql_query($sql); Your second select would need to be renamed into <select id="box2View" multiple="multiple" size="3" name="tenSuitablePostcodes[]"> Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335859 Share on other sites More sharing options...
dvent Posted April 9, 2012 Author Share Posted April 9, 2012 Getting an error on submit: Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'table (`tenForename`, `tenSurname`, `tenSuitablePostcodes`) VALUES ('1', '1111',' at line 1 Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335865 Share on other sites More sharing options...
dcro2 Posted April 9, 2012 Share Posted April 9, 2012 You were supposed to put in your own table and column names there, I don't know what they are... Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335868 Share on other sites More sharing options...
dvent Posted April 9, 2012 Author Share Posted April 9, 2012 You were supposed to put in your own table and column names there, I don't know what they are... Aha! Forgot to change the table name. Cheers dcro2!! Quote Link to comment https://forums.phpfreaks.com/topic/260640-include-checkbox-value/#findComment-1335870 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.