【问题标题】:How to parse php input array into json then back to php如何将php输入数组解析为json然后返回php
【发布时间】:2021-01-08 15:49:08
【问题描述】:

所以我为表格中的每一行都制作了一个复选框,这样我就可以一次删除多行。

我正在尝试对表格中的每一行执行此操作:

<td><input id="checkItem" name="delete[]" type="checkbox" value="<?=$product->id;?>"></td>

那么delete[]就持有数组。

我的问题是,我正在使用 json (ajax) 来解析我的数据。

它只识别我页面上的最后一个 delete[] 复选框。 如果我选中最后一个复选框并点击全部删除(我的按钮执行多选行),它会显示产品 ID,这很好。

如果我尝试任何其他复选框,它不会返回任何内容。

这是为每一行创建复选框数组的复选框行: enter image description here

这里是发布功能: enter image description here

这是我的删除功能: enter image description here

这是我处理请求的 json ajax 代码:

$("body").on("submit", ".ajax-form", function (e) {
    e.preventDefault();

    form = $(this);

    url = form.attr("action");
    method = form.attr("method");
    data = {};

    form.find("[name]").each(function (key, value) {
        type = $(this).attr("type");
        name = $(this).attr("name");

        if (type == "radio" || type == "checkbox") {
            value = "";

            if ($(this).prop("checked")) value = $(this).val();
        } else value = $(this).val();

        data[name] = value;
    });

    button_lang = $("[type=submit]", form).html();


    var saveData = $.ajax({
          type: method,
          crossDomain: true,
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          url: url,
          data: data,
          dataType: "json",
          success: function(response) {
            messages = "";
            if(response.success === true)
            {
                if(response.redirect)
                {
                  $('.ajax-form').trigger("reset");
                  window.location.href = response.redirect;
                }
                else if(response.success)
                {
                    for (var message in response.messages) {
                        messages += response.messages[message] + "<br>";
                    }
                    if(response.timer) {
                        const Toast = Swal.mixin({
                          toast: true,
                          position: 'top-end',
                          showConfirmButton: false,
                          timer: 3000,
                          timerProgressBar: true,
                          onOpen: (toast) => {
                            toast.addEventListener('mouseenter', Swal.stopTimer)
                            toast.addEventListener('mouseleave', Swal.resumeTimer)
                          }
                        });

                        Toast.fire({
                          icon: 'success',
                          title: "<h6 style='text-align: left!important;'>" + messages + "</h6>"
                        });
                        $('.ajax-form').trigger("reset");
                        window.setTimeout(function(){window.location.href = response.sendTo;},response.timer);
                    } else {

                        const Toast = Swal.mixin({
                          toast: true,
                          position: 'top-end',
                          showConfirmButton: false,
                          timer: 3000,
                          timerProgressBar: true,
                          onOpen: (toast) => {
                            toast.addEventListener('mouseenter', Swal.stopTimer)
                            toast.addEventListener('mouseleave', Swal.resumeTimer)
                          }
                        });

                        Toast.fire({
                          icon: 'success',
                          title: "<h6 style='text-align: left!important;'>" + messages + "</h6>"
                        });

                        $('.ajax-form').trigger("reset");
                    }
                }
                else
                {
                    location.reload();
                }
            }
            else
            {
                for (var message in response.messages) {
                    messages += response.messages[message] + "<br>";
                }
                const Toast = Swal.mixin({
                  toast: true,
                  position: 'top-end',
                  showConfirmButton: false,
                  timer: 3000,
                  timerProgressBar: true,
                  onOpen: (toast) => {
                    toast.addEventListener('mouseenter', Swal.stopTimer)
                    toast.addEventListener('mouseleave', Swal.resumeTimer)
                  }
                });

                Toast.fire({
                  icon: 'error',
                  title: "<h6 style='text-align: left!important;'>" + messages + "</h6>"
                });
            }
          }
    });

    saveData.fail(function() {
      const Toast = Swal.mixin({
        toast: true,
        position: 'top-end',
        showConfirmButton: false,
        timer: 3000,
        timerProgressBar: true,
        onOpen: (toast) => {
          toast.addEventListener('mouseenter', Swal.stopTimer)
          toast.addEventListener('mouseleave', Swal.resumeTimer)
        }
      });

      Toast.fire({
        icon: 'error',
        title: "<h6 style='text-align: left!important;'>A system error occured</h6>"
      });
    });

});

【问题讨论】:

  • 您应该尝试缩小您的问题范围。最有可能在您收集输入值的步骤中。我会做console.log(data) 看看在你迭代输入后它持有什么。另外,您为什么不只是serialize 表格?

标签: php json ajax


【解决方案1】:

试试这个。但是@El_Vanja 是对的。您的代码有很大的改进空间

 form.find('input:checkbox').each(function (key, value) {

        type = $(this).attr("type");
        name = $(this).attr("name");

        if (type == "radio" || type == "checkbox") {
            value = "";

            if ($(this).prop("checked")) value = $(this).val();
        } else value = $(this).val();

        data.push({name:value});

    }); 

【讨论】:

    猜你喜欢
    • 2015-05-01
    • 1970-01-01
    • 2019-02-12
    • 2013-05-14
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2016-05-10
    相关资源
    最近更新 更多