如何将复选框数组从 FormData 传递到 php
how to pass checkbox array to php from FormData
对于 FormData,复选框数组无法通过 ajax 传递给 php。以下脚本在未使用 FormData 的情况下工作正常。所以我假设问题出在附加 FormData 或从中传递时。它returns invalid arguments supplied for foreach()
.
复选框不是表单元素的一部分,所以
<input type="checkbox" id="menu" name="menu[]" value="1">
<input type="checkbox" id="menu" name="menu[]" value="2">
<input type="checkbox" id="menu" name="menu[]" value="3">
<form id="form">
...........
</form>
Jquery 和 ajax
$(document).on('submit', '#form', function(e){
e.preventDefault();
var navid = [];
$("[name='menu[]']:checked").each(function (i) {
navid[i] = $(this).val();
});
if (navid.length === 0){ //tell you if the array is empty
alert("Please Select atleast one checkbox");
}
else {
var formData = new FormData(this);
formData.append('navid', navid);
$.ajax({
type: 'POST',
url: 'upload.php',
data: formData,
contentType: false,
cache: false,
processData:false,
success: function(data){
alert(data);
}
});
}
});
PHP
foreach ($_POST["navid"] AS $key => $item){
$query1 =$con->prepare("INSERT INTO menu(cid, title, en_title) VALUES (:navid, :menuin, :menueng)");
$query1->bindParam(':menunin',$_POST["menunin"][$key]);
$query1->bindParam(':menueng',$_POST["menueng"][$key]);
$query1->bindParam(':navid',$item);
$query1->execute();
echo 'Menu has inserted';
}
是的,您的 php 代码中存在问题:
您传递的是逗号分隔值,因此首先分解并使用
$data = explode("," ,$_POST["navid"]);
foreach ($data AS $key => $item){
$query1 =$con->prepare("INSERT INTO menu(cid, title, en_title) VALUES (:navid, :menuin, :menueng)");
$query1->bindParam(':menunin',$_POST["menunin"][$key]);
$query1->bindParam(':menueng',$_POST["menueng"][$key]);
$query1->bindParam(':navid',$item);
$query1->execute();
echo 'Menu has inserted';
}
对于 FormData,复选框数组无法通过 ajax 传递给 php。以下脚本在未使用 FormData 的情况下工作正常。所以我假设问题出在附加 FormData 或从中传递时。它returns invalid arguments supplied for foreach()
.
复选框不是表单元素的一部分,所以
<input type="checkbox" id="menu" name="menu[]" value="1">
<input type="checkbox" id="menu" name="menu[]" value="2">
<input type="checkbox" id="menu" name="menu[]" value="3">
<form id="form">
...........
</form>
Jquery 和 ajax
$(document).on('submit', '#form', function(e){
e.preventDefault();
var navid = [];
$("[name='menu[]']:checked").each(function (i) {
navid[i] = $(this).val();
});
if (navid.length === 0){ //tell you if the array is empty
alert("Please Select atleast one checkbox");
}
else {
var formData = new FormData(this);
formData.append('navid', navid);
$.ajax({
type: 'POST',
url: 'upload.php',
data: formData,
contentType: false,
cache: false,
processData:false,
success: function(data){
alert(data);
}
});
}
});
PHP
foreach ($_POST["navid"] AS $key => $item){
$query1 =$con->prepare("INSERT INTO menu(cid, title, en_title) VALUES (:navid, :menuin, :menueng)");
$query1->bindParam(':menunin',$_POST["menunin"][$key]);
$query1->bindParam(':menueng',$_POST["menueng"][$key]);
$query1->bindParam(':navid',$item);
$query1->execute();
echo 'Menu has inserted';
}
是的,您的 php 代码中存在问题:
您传递的是逗号分隔值,因此首先分解并使用
$data = explode("," ,$_POST["navid"]);
foreach ($data AS $key => $item){
$query1 =$con->prepare("INSERT INTO menu(cid, title, en_title) VALUES (:navid, :menuin, :menueng)");
$query1->bindParam(':menunin',$_POST["menunin"][$key]);
$query1->bindParam(':menueng',$_POST["menueng"][$key]);
$query1->bindParam(':navid',$item);
$query1->execute();
echo 'Menu has inserted';
}