sleepyw Posted March 18, 2010 Share Posted March 18, 2010 I have an HTML form with a multiple select drop down field. I'm trying to figure out 2 things: 1. If the user selects multiple items from the drop down box, how to I write that data to MySQL on the submit page? Can I have it separated by commas? 2. If a user goes back to edit that item's data from that drop down box, how do I script the form's select and options fields to pull all items from that multi select array? I'm not a PHP guru by any means and haven't dealt with a lot of arrays in this fashion. Here's an example of the drop down HTML form (I am pulling the option values from another table to populate the drop- down list): <select name="Type" multiple size="4"> <option value="<? echo $rows['Type']; ?>" selected><? echo $rows['Type']; ?></option> <? $Type_sql="SELECT DISTINCT Type FROM Table ORDER by Type"; $Type_result=mysql_query($Type_sql); while($Type_rows=mysql_fetch_array($Type_result)){ echo "<option value='"; echo $Type_rows['Type']; echo "'>"; echo $Type_rows['Type']; echo "</option>"; } ?> </select> Now on the submission page, I have a simple UPDATE TABLE SET Type=$_POST['Type'] kind of thing. How would I modify that to insert all multi select items? And then how do I read that data to alter the above form code to pull all options in the db and show them as selected in the drop down? Help is very much appreciated. Link to comment https://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/ Share on other sites More sharing options...
scvinodkumar Posted March 19, 2010 Share Posted March 19, 2010 you need to use array type variable, like, <select name="Type[]" multiple size="4"> after submitting form, you can get values like this $type = implode(",",$_POST['Type']); //here i am converting array to string with comma separted Link to comment https://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/#findComment-1028452 Share on other sites More sharing options...
sleepyw Posted March 19, 2010 Author Share Posted March 19, 2010 Let me give that a try, thank you! Link to comment https://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/#findComment-1028533 Share on other sites More sharing options...
Jax2 Posted March 19, 2010 Share Posted March 19, 2010 If you want to store them as separate records (I.e. a new row in your mysql for each drop down thing selected) you can also do something like this: <select name="ingred[]" size=35> <option value="foo">Foo</option> <option value="foo2">Foo2</option> <option value="foo3">Foo3</option> And then process it like this: for ($i = 0; $i < 16; $i++) { if ($ingred[$i] != "") { $sql = "insert into ingredients (ingredient) values (''$ingred[$i]')"; $result = mysql_query($sql ,$db); } } That will make a new record for every choice they selected. Link to comment https://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/#findComment-1028555 Share on other sites More sharing options...
sleepyw Posted March 19, 2010 Author Share Posted March 19, 2010 you need to use array type variable, like, <select name="Type[]" multiple size="4"> after submitting form, you can get values like this $type = implode(",",$_POST['Type']); //here i am converting array to string with comma separted That works perfectly! Now, the only other thing is how do I extract those items to be selected in the form drop down for someone that wants to edit the entry (removing the comma, of course)? It should have each item selected in the drop down. I'm trying to think of how to write something that says if the item in the db matches an item in the option field, then it should be "selected" in the option tag. Jax - thanks for the help, but not quite what I'm trying to do. Link to comment https://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/#findComment-1028734 Share on other sites More sharing options...
sleepyw Posted March 19, 2010 Author Share Posted March 19, 2010 I think I got it working. I did this to select the items exploded from the db into a selected option drop down: $Type_exploded = explode(",", $rows['Type']); if(is_array($Type_exploded)) { while (list ($key, $Type_val) = each ($Type_exploded)) { echo "<option value=" . $Type_val . " selected>" . $Type_val . "</option>"; } } Thanks again for the help! Link to comment https://forums.phpfreaks.com/topic/195729-multiple-select-drop-down-data-saved-to-mysql-how/#findComment-1028755 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.