ajax post within jquery onclick

前端 未结 4 1751
伪装坚强ぢ
伪装坚强ぢ 2021-01-11 09:01

I have a button which calls a modal box to fade into the screen saying a value posted from the button then fade off, this works fine using jquery, but I also want on the sam

相关标签:
4条回答
  • 2021-01-11 09:45

    You have an error in your ajax definitions. It should be:

    $.ajax
    ({ 
        url: 'reserbook.php',
        data: "book_id="+book_id,
        type: 'post',
        success: function()
        {
            $('.modal-box').text(result).fadeIn(700, function() 
            {
                setTimeout(function() 
                {
                    $('.modal-box').fadeOut();
                }, 2000);
            });
        }
    });
    
    0 讨论(0)
  • 2021-01-11 09:46

    Try this. Edited to the final answer.

    button:

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

    script:

    <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: The change to mysqli is minimal to your code, but strongly recommended.

    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.

    0 讨论(0)
  • 2021-01-11 09:47
    $.ajax
    ({ 
        url: 'reservebook.php',
        data: {
        jqbookID : book_id,
        },
        type: 'post',
        success: function()
        {
            $('.modal-box').text(result).fadeIn(700, function() 
            {
                setTimeout(function() 
                {
                    $('.modal-box').fadeOut();
                }, 2000);
            });
        }
    });
    

    });

    Try this

    0 讨论(0)
  • 2021-01-11 09:52

    You Ajax is bad formed, you need the sucsses event. With that when you invoke the ajax and it's success it will show the response.

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

    Edit:

    Another important point is data: "book_id="+book_id, that should be data: {"book_id":book_id},

    0 讨论(0)
提交回复
热议问题