我的网站具有以下菜单结构。我正试图通过它nested arrays'
传递parent array
值。我的ajax代码传递嵌套和非嵌套数组,但问题是! 非嵌套数组包含其他嵌套值。例如:以下Views/Art
没有嵌套数组,但我的ajax代码将News
嵌套元素的值作为常见值传递。如何传递每个数组的嵌套数组而不影响其他数组?
<li>
<input type="checkbox" name="menu[]" value="2">News
<ul>
<li>
<input type="checkbox" name="menu[][menusub]" value="2">Politics
</li>
<li>
<input type="checkbox" name="menu[][menusub]" value="3">Social
</li>
<li>
<input type="checkbox" name="menu[][menusub]" value="4">Economy
</li>
</ul>
</li>
<li>
<input type="checkbox" name="menu[]" value="12">Views
</li>
<li>
<input type="checkbox" name="menu[]" value="13">ART
</li>
<form enctype="multipart/form-data" id="form">
......
</form>
的Ajax
$(document).on('submit', '#form', function(e){
e.preventDefault();
var navid = [];
$("input[name='menu[]']:checked").each(function(){
navid.push(this.value);
});
var subnavid = [];
$("input[name='menu[][menusub][]']:checked").each(function(){
subnavid.push(this.value);
});
if (navid.length === 0){ //tell you if the array is empty
alert("Please Select atleast one checkbox");
return false;
}
else {
var formData = new FormData(this);
formData.append('navid', navid);
formData.append('subnavid', subnavid);
$.ajax({
type: 'POST',
url: 'upload.php',
data: formData,
contentType: false,
cache: false,
processData:false,
success: function(data){
alert(data);
}
});
}
});
PHP
$data = explode("," ,$_POST["navid"]);
subdata = explode("," ,$_POST["subnavid"]);
foreach ($data AS $key => $menu){
foreach($subdata AS $k => $submenu){
......
......
}
}