用ajax发布值,php把它放在数据库中

时间:2015-12-21 18:10:19

标签: php jquery mysql ajax

我想接受表单中的用户数据,使用AJAX将值发布到PHP脚本,然后将它们插入到数据库中。

JQuery的:

$(".add").click(function(){
    var order = {
        Name: $name.val(),
        Drink: $drink.val(),
    }
    $.ajax({
        type: 'POST',
        url: '/ajax/api/orders.php',
        data: order,
        success: function(newOrder){
            $orders.append('<li>Name: ' + newOrder.Name +', Drink: '+ newOrder.Drink +'</li>')
    }
    })
});

腓:

 //open connection to mysql db
$connection = mysqli_connect("localhost","root","xxxxxxxxx","test") or die("Error " . mysqli_error($connection));

//fetch table rows from mysql db
$sql = "select * from orders";
$result = mysqli_query($connection, $sql) or die("Error in Selecting " . mysqli_error($connection));

$name = $_POST["Name"];
$drink = $_POST["Drink"];

$sql2 = "INSERT INTO orders (Id, Name, Drink) VALUES (NULL, '$name', '$drink')";
//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);

//close the db connection
mysqli_close($connection);

有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

我认为你应该在收到所有订单之前插入

$name = $_POST["Name"];
$drink = $_POST["Drink"];

$sql2 = "INSERT INTO orders (Id, Name, Drink) VALUES (NULL, '$name', '$drink')";
mysqli_query($connection, $sql) or 
             die("Error in Inserting" . mysqli_error($connection))

$id = mysqli_insert_id ($connection);
$sql = "select * from orders where Id = $id";
$result = mysqli_query($connection, $sql) or 
             die("Error in Selecting " . mysqli_error($connection));

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);

javascript将是正确的:

$(".add").click(function(){
    var order = {
        Name: $name.val(),
        Drink: $drink.val(),
    }
    $.ajax({
        type: 'POST',
        url: '/ajax/api/orders.php',
        data: order,
        success: function(newOrder) {
            $.each(newOrder, function(order) {
                //$order is a ul that have id = orders
                $orders.append('<li>Name: ' + order.Name +', Drink: '+ order.Drink +'</li>');
            });
        }
    });
});

如果你需要什么,请问:]

<强>更新

请更新您对此的成功功能

        success: function(data) {
            data.forEach(function(order) {
                //$orders is a ul that have id = orders
                $orders.append('<li>Name: ' + order.Name +', Drink: '+ order.Drink +'</li>');
            });
        }

更新2

success: function(data) {
    var obj = JSON.parse(data);
    var length = obj.length;
    for (var i = 0; i < length; i++) {
        $orders.append('<li>Name: ' + obj[i].Name +', Drink: '+ obj[i].Drink +'</li>');
    }
}

更新3

$name = $_POST["Name"];
$drink = $_POST["Drink"];

$sql2 = "INSERT INTO orders (Id, Name, Drink) VALUES (NULL, '$name', '$drink')";
mysqli_query($connection, $sql2) or 
             die("Error in Inserting" . mysqli_error($connection))

$id = mysqli_insert_id($connection);
$sql = "select * from orders where Id = $id";
$result = mysqli_query($connection, $sql) or 
             die("Error in Selecting " . mysqli_error($connection));

//create an array
$emparray = array();
while($row =mysqli_fetch_assoc($result))
{
    $emparray[] = $row;
}
echo json_encode($emparray);