[英]Post a dynamic input form and fetch the array
I have an issue with my dynamic input form. 我的动态输入表单有问题。 Its for our new Adminportal (We can manage our meals and drinks there) So the dynamic form looks like that:
它适用于我们的新Adminportal(我们可以在此处管理餐食和饮料),因此动态表格如下所示:
...Add Row</button>
<br />
<div class="rowFields<?= $id; ?>">
<div>
<input type="text" name="newMealName[]" placeholder="Name" style="width:30%"/>
<input type="text" name="newMealDescription[]" placeholder="Description" style="width:50%" />
<input type="number" step="0.01" name="newMealPrice[]" placeholder="0.01" style="width:15%" />
<a href="#" id="remove_row">Remove</a>
</div>
</div>
I am adding more rows or remove them via JQuery - that works fine. 我要添加更多行或通过JQuery删除它们-效果很好。 I am passing the form with my Ajax script, I am using for other inserts to our database as well - this works!
我正在用我的Ajax脚本传递表单,我也正在将其他插入物也插入到我们的数据库中-可行!
My PHP insert script for this one looks like that: 我的PHP插入脚本如下所示:
<?php
...
$nameArray = $_POST['newMealName'];
$descriptionArray = $_POST['newMealDescription'];
$priceArray = $_POST['newMealPrice'];
$sqlAddNewMeals = $db->prepare("insert into tableX(name, description, price) values(?, ?, ?)");
for($i = 0; $i<sizeof($nameArray); $i++)
{
$sqlAddNewMeals->bind_param('ssd', $_POST['newMealName'][$i], $_POST['newMealDescription'][$i], $_POST['newMealPrice'][$i]);
$sqlAddNewMeals->execute();
}
$sqlAddNewMeals->close();
?>
I have tried to use 我尝试使用
$sqlAddNewMeals->bind_param('ssdi', $nameArray[$i],...
but this doesn't work either. 但这也不起作用。 I have one last idea, but want to discuss with you before doing that: Do I have to bind params like $name, $description and use a for loop like that:
我有最后一个想法,但是想在此之前与您讨论:我是否必须绑定$ name,$ description之类的参数并使用for循环,例如:
forloop {
$name=$nameArray[1];
...
$sql->execute(); }
Thanks in advance, pl44 在此先感谢,pl44
edit 1: AJAX to pass the form 编辑1:AJAX传递表单
$(function () {
$("button#addNewMeals").click(function () {
var menu_id = $(this).val();
$.ajax({
type: "POST",
url: "XXX/add-new-menu.php",
data: $('form.manageMenu-'+menu_id).serialize(),
success: function () {
alert("Added.");
},
error: function () {
alert("failure");
}
});
});
});
<?php $nameArray = $_POST['newMealName']; $descriptionArray = $_POST['newMealDescription']; $priceArray = $_POST['newMealPrice']; $sqlAddNewMeals = $db->prepare("insert into tableX(name, description, price) values(?, ?, ?)"); $param = array(); for($i = 0; $i<sizeof($nameArray); $i++) { $param[$i]['newMealName'] = $_POST['newMealName'][$i]; $param[$i]['newMealDescription'] = $_POST['newMealDescription'][$i]; $param[$i]['newMealPrice'] = $_POST['newMealPrice'][$i]; } print_r($param); ?>
I have tried to fix it by myself. 我已经尝试自己修复它。 I used this logic:
我使用了以下逻辑:
ANd it´s working now. 它现在正在工作。 Thanks everyone!!
感谢大家!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.