PHP Save selected options from Multiple Drop Downs to an Array

我的未来我决定 提交于 2020-01-05 10:30:14

问题


I have the following code:

<select id='rq1' class='business' name='q1'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>
<select id='rq2' class='business' name='q1'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>
<select id='rq3' class='business' name='q1'>
  <option value='1'>1</option>
  <option value='2'>2</option>
</select>

Fiddle: http://jsfiddle.net/NMQz8/

What I am trying to understand is if it is possible to save the responses from each of the dropdowns to an Array. The reason for this is so that I can save the results into one column in the database as opposed to having a column for each dropdown response. Does that make sense?

Is this possible, is it as straightforward as adding [] to the name and then unserializing them to post to the database?


回答1:


Do you mean

<?php
    var_dump($_POST);
?>
<form method='post'>
    <select id='rq1' class='business' name='q1[]'>
      <option value='1'>1</option>
      <option value='2'>2</option>
    </select>
    <select id='rq2' class='business' name='q1[]'>
      <option value='1'>1</option>
      <option value='2'>2</option>
    </select>
    <select id='rq3' class='business' name='q1[]'>
      <option value='1'>1</option>
      <option value='2'>2</option>
    </select>
    <input type='submit'>
</form>

When you submit the form, you will get data in Array.



来源:https://stackoverflow.com/questions/18672196/php-save-selected-options-from-multiple-drop-downs-to-an-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!