weixin_33704591 2015-07-28 00:30 采纳率: 0%
浏览 33

SQL查询不刷新

I've read all the related posted, watched videos, and read tutorials... But I still can't figure this out. I just want to run a mysqli_query insert without a refresh.

No inputs, no variables, just a pre-defined sql insert without a refresh.

Here is the main doc:

<html>
    <head>
        <script src="inc/scripts/jquery-1.11.3.min.js"></script>
        <script>


                $("#click").click( function()
            {
                $.ajax({
                url: "click.php",
                type: 'POST',
                success: function(result) { 
                //finished
            }
            });
            });

        </script>
    </head>
    <body>
        <input type="button" id="click" value="Click">
    </body>
</html>

Click.php (Has been tested standalone):

<?php 
$db = mysqli_connect("localhost","root","","mytable") 
or die("Error " . mysqli_error($db));

mysqli_query($db,"INSERT INTO items VALUES 
('','test','test','total test','test','test','test','test')");
?>  

This has been driving me crazy... I've read tutorials and watched many videos about ajax... but I can't figure this out. Thank you for any advice.

  • 写回答

4条回答 默认 最新

  • weixin_33725126 2015-07-28 00:55
    关注

    You're binding the event $('#click').click() before there is an element to bind to (since $('#click') isn't loaded yet).

    Just move your <script> tag with the click binding event into the <body> underneath the input button and it will work as expected.

    You might also want to wrap in a jQuery document ready enclosure like:

    $(function() {
    
    });
    

    to make sure it runs when DOM ready.

    评论

报告相同问题?