且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在jQuery的阿贾克斯后的onclick

更新时间:2023-12-05 16:33:16

试试这个。编辑到最后的答案。

Try this. Edited to the final answer.

按钮

<div class= "obutton feature2" data-id="<?php echo $bookID;?>">
    <button class="reserve-button">Reserve Book</button>
</div>

剧本

<script>
$('.reserve-button').click(function(){

    var book_id = $(this).parent().data('id');

    $.ajax
    ({ 
        url: 'reservebook.php',
        data: {"bookID": book_id},
        type: 'post',
        success: function(result)
        {
            $('.modal-box').text(result).fadeIn(700, function() 
            {
                setTimeout(function() 
                {
                    $('.modal-box').fadeOut();
                }, 2000);
            });
        }
    });
});
</script>

reservebook.php

<?php
session_start();

$conn = mysql_connect('localhost', 'root', '');
mysql_select_db('library', $conn);

if(isset($_POST['bookID']))
{
    $bookID = $_POST['bookID'];

    $result = mysql_query("INSERT INTO borrowing (UserID, BookID, Returned) VALUES ('".$_SESSION['userID']."', '".$bookID."', '3')", $conn);

    if ($result)
        echo "Book #" + $bookId + " has been reserved.";
    else
        echo "An error message!";
}
?>

PS#1 :变更为的mysqli 是最小到code,但强烈建议

PS#1: The change to mysqli is minimal to your code, but strongly recommended.

PS#2 :在成功在Ajax调用并不意味着查询成功。仅仅意味着阿贾克斯交易都是正确的,并得到了satisfatory响应。这意味着,它发送到网​​址正确的数据,但并不总是网​​址做了正确的事情。

PS#2: The success on Ajax call doesn't mean the query was successful. Only means that the Ajax transaction went correctly and got a satisfatory response. That means, it sent to the url the correct data, but not always the url did the correct thing.