James Clifton
James Clifton

Reputation: 175

How do i tell if ajax is working?

Currently I am trying to get javascript code working that I didn't write (and I'm only just learning it), after a lot of research and playing around I have guessed that the following code is the problem -

$("#btnLogin").click(function(){
        // Check the player login in DB
 //alert("This code is good so far.");
         $.post("fetch.php", {"action": "login", "login" : $("#txtLogin").val()},
            function(data){
              }, "json")            
      })

I have edited out the code in function(data) in this example.

If I have the alert active the alert button does work - so I deduce that the click(function) works. But none of the functions ever work :(

Now if I change it to fetch2.php and then make that page have a simple statement of

echo "I am here.";

then that should appear, am I correct? Nothing happens when I click the log in button.

What test can I run to make sure this is the code that is causing me grief and (if it is the problem) how can I fix it?

Upvotes: 1

Views: 5422

Answers (2)

Mithun Satheesh
Mithun Satheesh

Reputation: 27855

make your php output as a json array :

  $output['alert'] = "I am here.";
  echo json_encode($output);

this works if php is > 5.2

and in post :

$.post("fetch.php", {"action": "login", "login" : $("#txtLogin").val()},
        function(data){
           if(data){
             alert(data.alert);
           }
          }, "json");

Upvotes: 2

Theil
Theil

Reputation: 101

You should wite some code inside the function, try this bit of code:

   $("#btnLogin").click(function(){
        // Check the player login in DB
        //alert("This code is good so far.");
         $.post("fetch.php", {"action": "login", "login" : $("#txtLogin").val()},
            function(data){
               if(data){
                 alert(data);
               }
              }, "json")            
      });

according to the echo on your php code, it should alert "I am here.".

Upvotes: 0

Related Questions