在php中从ajax中检索数据

时间:2016-12-30 16:53:01

标签: php jquery ajax sql-update

我点击一个按钮触发一个简单的ajax调用,通过POST发送元素的id属性,没有错误,数据正在发送,我在网络控制台上检查。

因此,下一个逻辑步骤是认为它是一个与php有关的问题,其中我正在制作一个简单的更新语句,问题是php无法看到ajax调用传递的数据。请看下面:

AJAX:

$("a.report_btn").click(function() {
    var url = "report_post.php"; // the script where you handle the form input.
    $.ajax({
        type: "POST",
        url: url,
        data: $("a.report_btn").attr("id"),
        success: function(response) {          
            content.html(response); 
            $(".liked_success").show();
        }, error:function(exception){alert('Exeption:'+exception);}
    });
    return false; // avoid to execute the actual submit of the form.
}); 

PHP:

<?php include 'db_connect.php'; 

$id =$_POST['id'];

try {

    $sql = "UPDATE jobs_list
    SET post_like = post_like+1, TimeStamp = TimeStamp
    WHERE id=$id 
    LIMIT 1";

    $statement = $dbh->prepare($sql);
    $statement->bindValue("id", $id, PDO::PARAM_INT);
    $statement->bindValue("post_like", $_POST['post_like'], PDO::PARAM_INT);

    $count = $statement->execute();
    $dbh = null;        // Disconnect

}catch( PDOException $e ) {
    echo 'Ops... something went wrong...'; // error message
    var_dump($statement);
}

?>

0 个答案:

没有答案