tags:

views:

44

answers:

3
 jQuery(function ($) {
     /* fetch elements and stop form event */
     $("form.follow-form").submit(function (e) {
      /* stop event */
      e.preventDefault();
      /* "on request" */
      $(this).find('i').addClass('active');
      /* send ajax request */
      $.post('listen.php', {
       followID: $(this).find('input').val()
      }, function () {
       /* find and hide button, create element */
       $(e.currentTarget)
         .find('button').hide()
         .after('<span class="following"><span></span>Following!</span>');
      });
     });
    });

i want to know what when i process the mysql query in the listen.php file, how deos it get the success message back, and then perform the change after?

A: 

it's XMLHTTP Object that is responsible behind the scene.

Umair Ashraf
sorry im new to jquery and javascript? i know how to process the listen.php and insert the items in the database!! im just confused
getaway
A: 

your php script should output text... so lets say the query works output "YES" if it doesn't output "NO" then use this function in your $.post():

function (data) {
    if(data=="YES"){
        // worked
    }
    else{
        // didn't work
    }
}
Thomas Clayson
Must be a C(++) developer lol, should be $data but the logic is correct. You should pass a response back
Stoosh
where do i exactly but the data function!!! im so confused lol
getaway
lol objc - but I'm sure its the same for jquery, variable names don't have a $ - thats php. :p
Thomas Clayson
+1  A: 

UPDATE

PHP code:

<?php
if ( $_POST['action'] == 'follow' ) {
 $json = '';
  if ( $_POST['fid'] == "2" ) {
  $json = array(  array( "id" => 1 , "name" => "luca" ),
                  array( "id" => 2 , "name" => "marco" )
               );
    }
 echo json_encode($json);
}
?>

jQuery code:

$.ajax({
    type: 'POST',
    url: 'listen.php',
    data: 'action=follow&fid=' + 2, //$(this).find('input').val(),
    success: function(data) {
    var obj = $.parseJSON(data);
        for ( var i = 0; i < obj.length; i++ ) {
         alert( obj[i].id + ' ' + obj[i].name )
         }
    },
    complete: function(data) {
        //alert(data.responseText);
    },
    error: function(data) {
        alert(data);
    }
});
aSeptik
thanks for that, but what do i do with the php side of things on listen.php, how can i return the complete data!!
getaway
ok... you want to return acctual data? Not just a "YES" or "NO". What you probably need to do then is have the php script **output a JSON or XML feed** and then the variable `data` will be that feed (you can see it if you `alert()` it as in aSeptik's answer). Then use javascript functions to create an array structure of the information you've sent back. JSON would probably be the best bet, as I understand it outputs just like a javascript array.
Thomas Clayson