使用jQuery POST

时间:2018-06-25 07:15:57

标签: php jquery json

我无法使用jQuery POST插入数据库

在js

var us = result.user;
alert(us.email); //someone@gmail.com
var user = JSON.stringify(result.user);
$.post("signup.php", user);

在php中,

$data = json_decode($_POST['user'], true);
$conn = new mysqli("localhost", "root", "", "dbname");
$conn->query("INSERT INTO users (uid, name, email, number) VALUES (\'".$data['uid']."\', \'".$data['displayName']."\', \'".$data['email']."\', \'".$data['phoneNumber']."\')");

它没有插入db,上面的一个不起作用。

请给我代码以使用jQuery $ .POST插入db

2 个答案:

答案 0 :(得分:0)

jQuery代码:

$(document).ready(function(){
    /* rather then JSON.stringify i had used the simple varibale  */
    $("button").click(function(){
        $.post("demo_test_post.php",{name: "Donald Duck",city: "Duckburg"},
        function(data,status){
            alert("Data: " + data + "\nStatus: " + status);
        });
    });
});

用于AJAX请求的PHP代码:

<?php 
print_r($_POST);
$name = $_POST['name'];
$city = $_POST['city'];
/* you can now pass this variables in the query and insert your data easily */
?>

答案 1 :(得分:0)

$("#btnSubmit").click(function () {
                $("#unit").val('');
                    jQuery.ajax({
                        type: 'post',
                        url: $("#hdnService").val() + "InsertUomData",
                        data: "{'Uom':'" + $("#unit").val() + "'}",
                        contentType: "application/json; charset=utf-8",
                        dataType: "json",
                        success: function (response) {

});
//you have to pass parameter in data: section. same parameter fetch in service class. you have to define the same name of the parameter in service.cs and aspx file