簡體   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