drccfl9407 2018-02-08 23:35
浏览 135
已采纳

通过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 :

Example Ajax Post Data

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

  • 写回答

1条回答 默认 最新

  • dongqing6755 2018-02-08 23:43
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配