【问题标题】:Passing textbox array to ajax json将文本框数组传递给ajax json
【发布时间】:2015-10-20 04:49:50
【问题描述】:

我有两个不同的数组,即 main_name[] 和 sub_name[]。 我必须使用 AJAX 将这些值传递到下一页,以便插入 DB。请您帮帮我,我如何将数组作为 AJAX 中的数据传递。 HTML

<input type="text" name = "main_name[]" value = "" >
<input type="text" name = "sub_name[]" value = "" >

JS

$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});

【问题讨论】:

  • ajax 中尝试data: $('form').serialize(),
  • @Tushar 你能建议我用代码格式吗

标签: javascript php jquery json ajax


【解决方案1】:

$('form').serialize() 会将所有表单元素与值一起序列化,并与 ajax 查询一起发送。

 $.ajax({
     type: "POST",
     dataType: "json",
     url: "response.php", //Relative or absolute path to response.php file
     data: $('#form_id').serialize(),
     success: function(data) {
       $(".the-return").html
     );
     alert("Form submitted successfully.\nReturned json: " + data["json"]);
    }
    });
    return false;
    });
    });

【讨论】:

  • 如何获取response.php中的值
  • 您可以使用$_REQUEST。尝试在 response.php 上将 var_dump 作为 echo print_r($_REQUEST, true)
【解决方案2】:

你可以简单地使用serializeArray

var data = $(form).serializeArray();

$.ajax({
    type: "POST",
    dataType: "json",
    url: "response.php", //Relative or absolute path to response.php file
    data: data,
    success: function (data) {
        $(".the-return").html();
    }
});

【讨论】:

  • 它返回(警报)对象
【解决方案3】:

试试这个,它会在 (data) 变量中获取表单的所有值,并通过 ajax 以 json 格式发送。

var data = $('form').serialize();
$.ajax({
type: "POST",
dataType: "json",
url: "response.php", //Relative or absolute path to response.php file
data: data,
success: function(data) {
$(".the-return").html
);
alert("Form submitted successfully.\nReturned json: " + data["json"]);
}
});
return false;
});
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多