繁体   English   中英

通过.ajax post将多维表单数组发送到PHP预处理语句

[英]Send multidimensional form array via .ajax post to PHP prepared statement

我理解如何通过常规提交正常发送此表单。 然而,我对如何通过.ajax发送我的多维表单数组感到困惑。

我的表格:

<form action="week_update.php" id="theform" method="post">   
 <input type="text" name="data[][wednesday_crew]" value=""/>
 <input type="text" name="data[][thursday_crew]" value=""/>
 <input type="text" name="data[][friday_crew]" value=""/>
 <input type="text" name="data[][saturday_crew]" value=""/>
 <input type="text" name="data[][sunday_crew]" value=""/>

我的PHP:

$stmt = $connection->stmt_init();

if($stmt->prepare("UPDATE tableName SET ... = ?, ... = ?,)) {

// Bind your variables
$stmt->bind_param('ss', $.., $..,);

// get the multi array from form
$returnedData = $_POST['data'];

//let's loop through it.
for($i=0;$i<count($returnedData);$i+=2){
  $log_dates_ID = $returnedData[$i]['...'];
  $crew_chief = $returnedData[$i+1]['...'];
  $stmt->execute();
}

如果我理解正确的话,这相对容易。 我回答了类似的问题。

$("#theform").submit(function() {

    var url = "path/to/your/script.php"; // the script where you handle the form input.

    $.ajax({
           type: "POST",
           url: url,
           data: $("#theform").serialize(), // serializes the form's elements.
           success: function(data)
           {
               alert(data); // show response from the php script.
           }
         });

    return false; // avoid to execute the actual submit of the form.
});

你几乎得到了它,但你的输入元素应该是

<input .... name="data[friday_crew][]" />
                                   ^^--

那将在你所获得的每种类型的数据下生成一个子数组。 现在你的将构建一些可怕的franken-array,这对于遍历/分析来说真的很难看。 相比之下,你可以简单地拥有上述版本

foreach($_POST['data']['friday_crew'] as $key => $val) {
    $fri_crew = $val;
    $sat_crew = $_POST['data']['sat_crew'][$key];
    etc...
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM