如何将数组传递给AJAX [英] How to pass an Array to a AJAX
本文介绍了如何将数组传递给AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个表单,其值为数组
I have a form that has a value of array
<form id="detail_form">
<input type="text" name="owner_modal" value="Rey,Jan">
<button type="button" id="submit"> Send </button>
</form>
如何将该数组传递给AJAX进程
How can I pass this array to AJAX process
$('#submit').click(function(){
$.ajax({
url:"test-process.php",
method:"POST",
data:$('#detail_form').serialize(),
success:function(data) {
alert(data);
}
});
});
test-process.php
test-process.php
<?php
$number = count($_POST["owner_modal"]);
if($number > 0) {
for($i=0; $i<$number; $i++) {
echo $_POST["owner_modal"][$i];
}
}
?>
推荐答案
将字符串转换为数组.
if (isset($_POST["owner_modal"])) {
$owners = explode(',', $_POST["owner_modal"]);
if(is_array($owners) && !empty($owners)) {
foreach ($owners as $index => $owner) {
echo $owner;
}
}
}
这篇关于如何将数组传递给AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文