提交 ajax 表单而不刷新页面 php
Submit ajax form without page refresh php
我有这个表格,ajax 打电话给我 index.php-
<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)){
我有这个表格,ajax 打电话给我 index.php-
<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)){