0

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.

8
  • Try changing type to method in the ajax request. Are you getting anything back from it? Go to debug mode in the browser to see if the ajax request gets an error. First we just need to make sure the ajax request is functioning as it should. Commented Jul 28, 2015 at 0:35
  • What problem are you having? Commented Jul 28, 2015 at 0:35
  • Nothing happens, no feedback ;( Commented Jul 28, 2015 at 0:36
  • Echo something in the php script and console.log(data) in the ajax success function. Also add an error: function() { console.log() } method to the ajax request and console.log('anything') there to see if error is being fired. Commented Jul 28, 2015 at 0:37
  • where do I add "console.log('anything')"? Commented Jul 28, 2015 at 0:45

4 Answers 4

1

To refresh a part of a page you got to bind the success function to a div in the html so add a div with an Id

<div id="myDiv"></div>

And then

$('#like$id').click(function()
    {
    $.ajax({
    url: 'inc/scripts/liker_ajax.php?like=$id',
    type: 'GET',  
    success:function(result){   
       $('#like$id').addClass('green');  
       $('#dislike$id').removeClass('red');    
       $('#myDiv').html(result); 
    } 
    });
    }); 
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you very much for this insight - it's clearly something I overlooked. I do have a question though... What I need updated(refreshed) is just a number (mysql row count echo) and when I put that number in a span/div and do ('#NumDiv').html(result); it just disappears until I refresh my page and I can see the proper result. Any ideas?
I suppose my real question is: If I have a php variable '$likes' - how can I refresh/update it immediately when my DB changes without hitting F5?
Do a print_r($_GET) on your php and see what data you are getting there , it should refresh your div without hitting f5. Or use chrome console to see the how is the url beeing called to see what data you are actually sending. I think your $id variable is not gettiing a value.
0

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.

4 Comments

Didn't change anything ;(
I ran the code on my local server. What is your expected result? You're sure the PHP code is working OK?
Yeah - when I run the contents of click.php by itself - I get an clean insert in my database. I just need an insert
Did you use the console to see what was returned by the ajax request? Did you get the file or a 404? Any errors returned from PHP?
0

Try this.

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


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


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

1 Comment

missing a bracket there (close runAjax())
0

You are binding the event to the element when the element is not exixting yet. You have 2 options here.

  • Either move your script block to just below the end of body tag after the element.
  • Encase your code inside the script block under $(document).ready(function() { // your code here });

Also use the console tab under your developer tools to find the root cause if any errors are present.

1 Comment

Uncaught SyntaxError: Unexpected token return

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.