douliwang6896 2013-12-12 12:34
浏览 73
已采纳

jquery onclick中的ajax帖子

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 same click for value sent from the button to be posted to a php function, that to run and the modal box to still fade in and out.

I only have this to let my site know what js to use:

 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" ></script>

I'm still new so sorry for a rookie question, but will that allow ajax to run, or is it only for jquery?

The current script I'm trying is: (Edited to be correctly formed, based on replies, but now nothing happens at all)

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

    var book_id = $(this).parent().data('id'),
    result = "Book #" + book_id + " has been reserved.";

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

Though with this the modal box doesn't even happen.

The php is, resersebook.php:

<?php

session_start();

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

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

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

?>

and to be thorough, the button is:

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

I'm new to this and I've looked at dozens of other similar questions on here, which is how I got my current script, but it just doesn't work.

Not sure if it matters, but the script with just the modal box that works has to be at the bottom of the html body to work, not sure if for some reason ajax needs to be at the top, but then the modal box wouldn't work, just a thought.

  • 写回答

4条回答 默认 最新

  • douyong1905 2013-12-12 13:03
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器