【问题标题】:trying to insert multi select input with ajax试图用ajax插入多选输入
【发布时间】:2021-12-09 13:45:05
【问题描述】:

我正在尝试在 Laravel 中使用 ajax 存储多选输入,但出现此错误

{message: "Function name must be a string", exception: "Error", …}

这是我的刀片代码

<select class="" multiple="" id="medicine_pills" name="medicine_pills[]">
    <option value="2">test</option>
    <option value="2">test</option>
</select>

var medicine_pills = $('#medicine_pills').val();
$.ajax({
    url: '{{ url('medicine') }}',
    type: "post",
    data: {
        '_token': "{{ csrf_token() }}",
        'medicine_pills': medicine_pills,
        'last_appointment_id': last_appointment_id,
    },

在控制器中

$medicines = request()->post('medicine_pills');

foreach ($medicines as $iteam) {
    echo $medicines;
    $user = Medicine::create([
        'medicines_cats' => $iteam,
        'appointmen_id' => $last_appointment_id,
    ]);

【问题讨论】:

  • 请在dd($medicines)分享你的收获

标签: javascript php mysql ajax laravel


【解决方案1】:
<select class="" multiple="" id="medicine_pills" name="medicine_pills[]">
    <option value="2">test</option>
    <option value="2">test</option>
</select>

var medicine_pills = $("input[name='medicine_pills[]']").val();
$.ajax({
    url: '{{ url('medicine') }}',
    type: "post",
    data: {
        '_token': "{{ csrf_token() }}",
        'medicine_pills': medicine_pills,
        'last_appointment_id': last_appointment_id,
    },

请尝试一下。

【讨论】:

  • 这可能是答案,但添加解释会大大提高答案的质量。你做了什么,为什么你认为它会有所帮助? 解释您的答案,以便所有技能水平的未来访问者都可以从中学习。 Code-only answers are considered low quality on SO.
猜你喜欢
  • 2019-02-18
  • 1970-01-01
  • 2015-08-15
  • 2019-10-24
  • 2016-04-03
  • 2018-10-26
  • 2013-03-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多