通过.ajax post将多维表单数组发送到PHP准备语句
我了解如何正常发送此表格并定期提交。然而,对于如何通过.ajax发送多维表单数组,我感到困惑 我的表格:通过.ajax post将多维表单数组发送到PHP准备语句,php,jquery,Php,Jquery,我了解如何正常发送此表格并定期提交。然而,对于如何通过.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=""/&
<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=""/>
$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();
}
我的PHP:
<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=""/>
$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();
}
$stmt=$connection->stmt_init();
如果($stmt->prepare(“更新表名集…=?,…=?,)){
//绑定变量
$stmt->bind_参数('ss',$..,$..);
//从表单中获取多数组
$returnedData=$_POST['data'];
//让我们循环一下。
对于($i=0;$iexecute();
}
如果我没听错的话,这是相对容易的。不久前我回答了一个类似的问题
$("#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][]" />
^^--
谢谢!在服务器端呢?服务器端-您的示例中的php可以工作,正如您所说。在这种情况下,将代码放入一个文件
path/to/your/script.php
,它将在表单提交时执行。表单正在提交,但数据没有插入到我的数据库中。还有其他快速的想法吗?再次感谢如果要进行错误检查,请使用带有一个基本值的输入字段的表单,并确保您的脚本具有DB连接。是的,我还可以使用print_r函数查看数组。不用担心,我会解决的。谢谢您的时间。如果您执行了一个操作,应该可以工作,因为您只需要将urlencoded字符串发送到脚本