我在index.php-中有此表单和ajax调用
<form method='post' id='saleForm' >
<input type='text' name='addCustomer' class='addInput' id='adlastname' placeholder='customer last name'>
<input type='text' name='addYear' class='addInput' id='adYear' placeholder='year'>
<input type='text' name='addMake' class='addInput' id='adMake' placeholder='make'>
<input type='text' name='addModel' class='addInput' id='adModel' placeholder='model'>
<input type='text' name='addGross' class='addInput' id='adGross' placeholder='front gross'>
<input type='hidden' name='Id' value='<?php echo $id;?>'>
<input type='submit' name='subSale' id='subSale' value='Save' >
</form>
<script>
$("form").submit(function(e) {
var url = "add.php"; // the script where you handle the form input.
$.ajax({
type: "POST",
url: url,
data: $("form").serialize(), // serializes the form's elements.
success: function(data)
{
alert(data); // show response from the php script.
}
});
e.preventDefault(); // avoid to execute the actual submit of the form.
});
</script>
我正在尝试使用add.php将其提交给数据库-
$cust = $_POST['addCustomer'];
$addMake = $_POST['addMake'];
$addModel = $_POST['addModel'];
$addYear = $_POST['addYear'];
$addGross = $_POST['addGross'];
$subSale = $_POST['subSale'];
$soldDate = date('m/d/yy');
if(isset($subSale)){
$sql = "INSERT INTO `$sold_table` ( `Id`,`Customer`, `Year`, `Make`, `Model`, `FrontGross`, `SoldDate` ) VALUES (NULL,'$cust', '$addYear', '$addMake', '$addModel', '$addGross', '$soldDate')";
if (mysqli_query($conn, $sql)) {
echo 'success!';
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
}
当我单击“保存”按钮时,它显示为空警报,并且由于某种原因数据没有被推送。我知道插入是正确的,因为当我不使用ajax调用而只使用表单操作时,它是正确的。
使用jQuery提交表单时,不使用“提交”按钮。因此,一旦PHP接收到发布的数据,它将变成空的。您的脚本正在检查$ subSale以执行查询,该查询现在失败。
尝试提供名称为SubSale的隐藏输入,它将再次起作用。另一个解决方案是根据您自己的建议删除整个if语句。
if(isset($subSale)){
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句