0

I'm trying to get my javascript function to use ajax to call a php script. The php script will update a MYSQL table but I've tested the PHP script and it works fine.

This is my function:

    function rate()
{

 $.ajax({
    data: '' ,       
    url: 'update.php',
    method: 'GET', 
    success: function(msg) {
        alert(msg);
    }
});

}

and the function is called later with:

rate();

The php script doesn't need any information given to it, it just needs to be called, can anyone point out where I'm going wrong.

5
  • 4
    Learning to use the Network tab in modern browsers is going to aid you significantly with AJAX. Commented Apr 24, 2014 at 11:33
  • the script does not run Commented Apr 24, 2014 at 11:33
  • 1
    firefox, install firebug module and check in the console what's wrong Commented Apr 24, 2014 at 11:34
  • Does the url exist? If not, the 'success' function will never be called. I also recommend to install firebug (Maxime). Your code should work. Commented Apr 24, 2014 at 11:43
  • What is the error in console.? which script is not running the ajax script or the PHP script..? I am not clear here. Commented Apr 24, 2014 at 12:24

5 Answers 5

1

Just use like in this example:

<script>
  function rate() {
    $.get("update.php");
  }
</script>
Sign up to request clarification or add additional context in comments.

1 Comment

I don't understand sorry, this would go instead of the rate function?
0

If you dont need to pass the data to the PHP page, you can omit 'Data' from the ajax parameters.

<script>
    function rate(){
        $.ajax({      
            url: 'update.php',
            method: 'POST', 
            success: function(msg) {
               alert(msg);
            }
        });
    }

    rate();
</script>

1 Comment

That's true, but isn't relevant to the issue.
0

Have you included jquery library Try this

<script src="http://code.jquery.com/jquery-latest.min.js"
        type="text/javascript"></script>
<script>
      function rate()
{

 $.ajax({
    data: '' ,       
    url: 'update.php',
    method: 'GET', 
    success: function(msg) {
        alert(msg);
    }
});

}


    rate();


</script>

1 Comment

How does waiting for the DOM to load affect this?
0

Here is an example of how I use the ajax call:

$.ajax({
                               type: "POST",
                               url: "page_url",
                               dataType: 'json',
                               data: {
                                   'date1' : date1,
                                   'call': 'function_name'
                               },
                               beforeSend: function(){
                                   $("#loading").show();
                               },
                               success : function(response){    
                                  },
                                complete: function(){ 
                                   $("#loading").hide();
                               }
                      })

and on php part I add:

function function_name($request){
  your code here
}

if (!empty($_POST['call'])) {
    die($_POST['call']($_POST));
}

Comments

0

i show my all working code, try it:

<html>
  <head>
    <script type="text/javascript" src="/js/jquery-1.8.2.js"></script>
    <script>
      function rate() {
        $.get("update.php");
      }
    </script>
  </head>

  <body>
    <button onclick="rate()">Click me</button>
  </body>
</html>

Comments

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.