Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Multiple Dropdown Box Form Submit To MySQL

Tags:

html

php

mysql

Couldn't find any good information on how to do this so I thought I'd add it here. How do I grab the selected data from a multiple choice drop down html form using php and submit into a database. I need a seperate row for each choice.

I'd be happy just knowing how to grab the data and put it into an array.

like image 864
zuk1 Avatar asked Sep 08 '26 22:09

zuk1


1 Answers

It gets sent into an array, actually!

<form action="myscript.php" method="POST">
<select name="colors[]" multiple="multiple" size="2">
<option>Red</option>
<option>Blue</option>
<option>Green</option>
<option>Orange</option>
</select>
<input type="submit" value="Go!"> 
</form>

Then in the server side, $_POST['colors'] will be an array with the selected values.

The key here is to use the bracket notation in the name to let PHP know to expect an array.

For more, check out the PHP documentation's example.

Once you have the variables, it is trivial to create new rows.

like image 170
Paolo Bergantino Avatar answered Sep 11 '26 12:09

Paolo Bergantino