我尝试从$ .ajax post
插入数据<form action="" class="form-horizontal form-groups validate" enctype="multipart/from-data" id="validated_form" method="post" accept-charset="utf-8">
<div class="row">
<div class="col-md-12">
<div class="panel panel-default" data-collapsed="0">
<div class="panel-heading">
<div class="panel-title">
<i class="entypo-user"></i> Customer Information </div>
</div>
<div class="panel-body">
<div class="col-sm-5">
<div class="form-group">
<label class="col-sm-3 control-label">SKU_No</label>
<div class="col-sm-8">
<div class="input-group">
<span class="input-group-addon"><i class="entypo-code"></i></span>
<input type="text" id="SKU_No" class="form-control" name="SKU_No"
value="" required>
</div>
</div>
</div>
....................
</form>
我的参数从表单
开始正常这是我发送数据的ajax调用
$.ajax({
data: data,
type: "post",
url: "Product_DB.php",
success: function(data){
alert(data);
}
});
这是我的php文件
<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pharmacy";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "insert into `pharmacy`.`tblproduct` ( SKU_No, Batch_No, Strip_No, SAP_Code, Item_Name, Item_Qty, Purchase_Price, Deal%, Created_Date, Modified_Date, IsDeleted)
values ('".$_POST["SKU_No"]."','".$_POST["Batch_No"]."' ,'".$_POST["Strip_No"]."' , '".$_POST["SAP_Code"]."','".$_POST["Item_Name"]."' ,'".$_POST["Qty"]."' ,'".$_POST["Purchase_Price"]."' , '".$_POST["Discount"]."','".date("Y-m-d")."' ,'".date("Y-m-d")."' ,'0' )";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "" . mysqli_error($conn);
}
$conn->close();
}
?>
作为回应我没有得到任何消息或错误.. php文件没有打。 我也试着 我该如何追踪这个问题?任何想法
答案 0 :(得分:1)
我认为问题在于您将data
发布到您的php文件并检查isset($_POST['submit'])
肯定没有设置。试试这个
$.ajax({
data: {'submit': data}, //Align your submit with the data your're posting
type: "post",
url: "Product_DB.php",
success: function(data){
alert(data);
}
});
答案 1 :(得分:0)
我是否在评论中说过,$_POST['submit']
未定义,因此您的测试if(isset($_POST["submit"]))
失败,其余代码未执行。
因为您需要继续每个参数,我建议您测试是否设置了请求中的每个参数:
if(isset($_POST["SKU_No"]) && isset($_POST["Batch_No"]) && ...isset($_POST["Discount"]))
或
if(isset($_POST["SKU_No"], $_POST["Batch_No"], ..., $_POST["Discount"]))
而不是
if(isset($_POST["submit"]))