[英]AJAX Sends data to PHP file, but PHP can't get data from $_POST
I am making a page on a website in PHP where a user fills out 3 fields and hits submit. 我在PHP网站上制作了一个页面,用户在其中填写3个字段并点击提交。 The submit button should call my AJAX function to send the data to a database connection PHP file.
提交按钮应该调用我的AJAX函数,以将数据发送到数据库连接PHP文件。 I can confirm the data is sent from AJAX (via an alert) and the function returns a Success.
我可以确认数据是从AJAX发送(通过警报),并且该函数返回成功。 This must mean my database query file is not interpreting the data correctly.
这必须表示我的数据库查询文件没有正确解释数据。 Please help me understand where I went wrong.
请帮助我了解我哪里出错了。
Code from php page where the form is: 来自php页面的代码,其中的形式是:
<script type="text/javascript">
function storeInvoice() {
//var c_name = document.getElementById('c_name');
//var c_license = document.getElementById('c_license');
//var c_licenseemail = document.getElementById('c_licenseemail');
var data=$('#myForm').serialize();
$.ajax({
url: "/paydb.php",
type: "POST",
data: data,
async:false,
dataType:'html',
success: function (value) {
alert("Sent: "+data);
}
});
}
</script>
Relevant Code from Database php file: 数据库php文件中的相关代码:
mysqli_select_db($conn, "main_db" );
$c_license = $_POST['c_license'];
$c_name = $_POST['c_name'];
$c_licenseemail = $_POST['c_licenseemail'];
//Another method was attempted below.
//$data=$_POST['serialize'];
//$c_licenseemail = $data['c_licenseemail'];
//$c_license = $data['c_license'];
//$c_name = $data['c_name'];
$query = "INSERT INTO `invoices`(`company`, `licensenum`, `licenseemail`) VALUES ('$c_name','$c_license','$c_licenseemail');";
mysqli_query($conn, $query);
The data is sent as: 数据发送为:
c_name=testname&c_license=3&c_licenseemail=testemail%40email.com
Any help is much appreciated! 任何帮助深表感谢!
Please use the 请使用
mysqli_query($conn, $query) or die(mysqli_error($conn));
For duplicate key, you need to make the primary key to auto increment in your database. 对于重复键,您需要使主键自动在数据库中递增。
在成功的回调函数中,将alert(data)
替换为alert(value)
并在database.php
文件中回显任何post变量,以仅检查值是否通过ajax post正确发送到了database.php
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.