views:

10627

answers:

2

Dear all I am using JQuery and javascript, I need to Call the JQuery function inside my Javascript.

My jQuery:

     function display(id){
           $.ajax({
             type:'POST',
             url: 'ajax.php',
             data:'id='+id  ,
             success: function(data){
                     $("#response").html(data);            


                        }//success
       }); //ajax

           //Here i need to Return the ajax.php salary
           //return ?
         }

My ajax.php has

          <?php

           session_start(); 
            ....

           echo "$salary"; 
          ?> 

        My Javascript function has

        my.js

         function showName(){
         var id=12;

         var a=display(id); //here i need to call the jQuery function display()


        }

How to call the JQuery function inside javascript and how to return the PHP Values to JQuery?

A: 

You call showName from the anonymous function you assigned to the key 'success' in the ajax request. The anonymous function you assigned to success is your call back function.

Review the documentation on the ajax class: http://docs.jquery.com

jacobangel
+3  A: 

I really think you need to separate both things: 1) A function to trigger the AJAX call. 2) A function that receive the result from the ajax call and do whatever you need.

Like:

function display(id){
  $.ajax({
    type:'POST',
    url: 'ajax.php',
    data:'id='+id  ,
    success: anotherFunction; //ajax response
  });
}

Then in your myJS code:

display(2);
function anotherFunction(response){
  // Here you do whatever you need to do with the response
  $("#response").html(data);
}

Remember that display() will trigger your AJAX call and the code will continue, then when you get your response (maybe some seconds or ms later) anotherFunction() will be called. Thats why AJAX is ASYNC. If you need SYNC calls, check the docs about jQuery and AJAX technique.

Ricardo Vega
on that line$("#response").html(data);isn't that supposed to be $("#response").html(response);???
trace