简体   繁体   English

PHP Multiple Dropdown Box表单提交给MySQL

[英]PHP Multiple Dropdown Box Form Submit To 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. 如何使用php从多选下拉html表单中获取所选数据并提交到数据库中。 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. 我很高兴知道如何获取数据并将其放入数组中。

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. 然后在服务器端, $_POST['colors']将是具有所选值的数组。

The key here is to use the bracket notation in the name to let PHP know to expect an array. 此处的关键是在名称中使用方括号表示法,以使PHP知道期望使用数组。

For more, check out the PHP documentation's example . 有关更多信息, 请查看PHP文档的example

Once you have the variables, it is trivial to create new rows. 一旦有了变量,创建新行就很简单了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM