dousi0144 2017-04-19 17:56
浏览 62
已采纳

AJAX将数据发送到PHP文件,但PHP无法从$ _POST获取数据

I am making a page on a website in PHP where a user fills out 3 fields and hits submit. The submit button should call my AJAX function to send the data to a database connection PHP file. I can confirm the data is sent from AJAX (via an alert) and the function returns a Success. 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:

<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:

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!

  • 写回答

2条回答 默认 最新

  • doudou20145 2017-04-19 18:44
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改