第一:我能够通过AJAX成功地将1数组传递给PHP,如下所示:
成功的例子
// Basic HTML Form
// (with sample inputs & values)
// ------------------------------
<form method="post" id="unitDataForm" >
<!-- sample 1 -->
<input type="text" name="seqImg[]" value="111" />
<textarea name="seqDesc[]" />sample 1</textarea>
<!-- sample 2 -->
<input type="text" name="seqImg[]" value="222" />
<textarea name="seqDesc[]" />sample 2</textarea>
</form>
// Script for sending a single array
// ---------------------------------
var seqImg = new Array();
$('input[name="seqImg[]"]').each( function() {
seqImg.push($(this).val());
} );
$.ajax({
type: 'POST',
url: 'test_form_process2.php',
data: {seqImg:seqImg},
cache: false,
async: false
})
// The PHP...
// -----------------
//...config with $dbc...validation & error testing...
if(!empty($_POST["seqImg"])) {
$seqImgs = $_POST['seqImg'];
$seqArray = $seqImgs;
$sql= "INSERT INTO unit_test(`unit_id`,`seq_img`) VALUES ";
$i=0;
$seqArray_count = count($seqArray);
foreach($seqImgs as $seqImg) {
$seqImg = mysqli_real_escape_string($dbc, $seqImg);
$end = ($i == $seqImgs_count-1) ? ';' : ',';
$sql .= "('".$unit_id1."','".$seqImg."')".$end;
$i++;
以上操作正常(请参阅jsFiddle)。。。除了我也想在这个POST中通过seq_desc[]提交。我已经尝试了一些我发现的例子,但我不是100%如何在ajax中submit/attach
&则CCD_ 2是php中的附加数组。
当前尝试(无效):
// added the seqDesc input
var seqDesc = new Array();
$('textarea[name="seqDesc[]"]').each( function() {
seqDesc.push($(this).val());
} );
// and updated the ajax data to:
data: {seqImg:seqImg,seqDesc:seqDesc},
// The php
// --------
// added
$seqDesc=$_POST['seqDesc'];
$seqArray = array_combine($seqImgs,$seqDescs);
foreach($seqArray as $seqImg=>$seqDesc) {
目前,我正在控制台中获取[]
。。。
事实证明,上面所有的提交/接收编码都运行良好。问题出现在我的LOGIC(if/else)语句中。。。
在控制台日志中看到[]
,我意识到php中的if(!empty){}else{}
逻辑恰好是向后的。我只需要将if(!empty)...
反转为if(empty)....
if(empty($_POST["seqImg"])
&& empty($_POST['seqDesc']) ) { }else {