通过Ajax表单提交,通过在DB中用逗号分隔的一行中插入多个选项,但是内容给出错误
I have a Ajax form that is submitting data into my MySQL database.
I have select2 boxes that allow users to select multiple choices.
Here is an example of one box's HTLM code:
<select class="select3 form-control select2-multiple track" name="sellingmethods" id ="sellingmethods" multiple="multiple" multiple data-placeholder="Choose ...">
<option value="Telesales">Telesales</option>
<option value="Party Planning">Party Planning</option>
<option value="Door to Door">Door to Door</option>
<option value="Face to Face">Face to Face</option>
<option value="Online Demos">Online Demos</option>
<option value="Affiliate Link">Affiliate Link</option>
<option value="Appointment Setting">Appointment Setting</option>
</select>
When I click submit it passes the data like this :
As you can see it posts the same id of the box multiple times example seen on selling methods.
At the moment my PHP code that puts it in the DB is only putting the last item into the database.
Is there a way to put the multiple selections in separated by a comma? For example "door to door,telesales".
Here is my current PHP code :
'sellingmethods' => implode(',', $this->input->post('sellingmethods')),
'sellertype' => implode(',', $this->input->post('sellertype')),
'want2sell' => implode(',', $this->input->post('want2sell')),
'productssold' => implode(',', $this->input->post('productssold')),
'industriesrepresented' => implode(',', $this->input->post('industriesrepresented')),
Which is currently giving me the error :
implode(): Invalid arguments passed
我有一个Ajax表单,它将数据提交到我的MySQL数据库。 p>
我有选择2个框,允许用户选择多个选项。 p>
以下是一个框 HTLM strong>代码的示例: p>
点击提交时 它传递的数据如下: p>
正如您所看到它发布相同的ID 在销售方法上看到的多次示例框。 p>
目前,我将其放入数据库的PHP代码只是将最后一项放入数据库。 p>
有没有办法用逗号分隔多个选项? 例如“门到门,电话销售”。 p>
这是我当前的PHP代码: p>
目前正在给我错误: p>
&lt; select class =“select3 form-control select2-multiple track”name =“sellingmethods”id =“sellingmethods”multiple =“multiple”multiple data-placeholder =“Choose ...”&gt;
&lt; option value =“Telesales”&gt; Telesales&lt; / option&gt;
&lt; option value =“Party Planning”&gt; Party Planning&lt; / option&gt;
&lt; option value =“Door to Door”&gt; Door to 门&lt; /选项&gt;
&lt;选项值=“面对面”&gt;面对面&lt; /选项&gt;
&lt;选项值=“在线演示”&gt;在线演示&lt; / option&gt;
&lt;选项 value =“Affiliate Link”&gt; Affiliate Link&lt; / option&gt;
&lt; option value =“约会设置”&gt;约会设置&lt; / option&gt;
&lt; / select&gt;
code> pre> \ n
'sellingmethods'=&gt; implode(',',$ this-&gt; input-&gt; post('sellingmethods')),
'sellertype'=&gt; implode(',',$ this-&gt; input-&gt; post('sellertype')),
'want2sell'=&gt; implode(',',$ this-&gt; input-&gt; post('want2sell')),
'productssold'=&gt; implode(',',$ this-&gt; input-&gt; post('productssold')),
'industriesrepresented'=&gt; implode(',',$ this-&gt; input-&gt; post('industriesrepresented')),
code> pre>
implode():传递的参数无效 code> p>
div>
To access the multiple options submitted through a select multiple as an array, you will need to set the name attribute as an array eg sellingmethods[]
So for example:
<select class="select3 form-control select2-multiple track" name="sellingmethods[]" id ="sellingmethods" multiple="multiple" multiple data-placeholder="Choose ...">
<option value="Telesales">Telesales</option>
<option value="Party Planning">Party Planning</option>
<option value="Door to Door">Door to Door</option>
<option value="Face to Face">Face to Face</option>
<option value="Online Demos">Online Demos</option>
<option value="Affiliate Link">Affiliate Link</option>
<option value="Appointment Setting">Appointment Setting</option>
</select>
Then $this->input->post('sellingmethods')
or $_POST['sellingmethods']
will be an array as expected, and you should no longer get the error for your implode() function.