Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free.

I want to pass a JavaScript variable to a PHP file using AJAX and then return the result of the PHP file back to a JavaScript variable.

For passing the JavaScript variable to the PHP file I would assume it was something like how to use ajax to assign javascript variable to php variable

My JavaScript variable will be used to query a MySQL database, I then need the PHP to return a value from the database back to a JavaScript variable.

I know you could do something like:

$(document).ready(function() {
  Quantity = parseInt(str);
  $("#ajaxResult").load($.ajax({url:"yourphp.php",type:"POST",async:false,data:{quantity:Quantity}}););
});

To set the result of the PHP file to show in an HTML element with the id ajaxResult

But how would I set a variable to the result, maybe?

$(document).ready(function() {
      Quantity = parseInt(str);
      var result = $.load($.ajax({url:"yourphp.php",type:"POST",async:false,data:{quantity:Quantity}}););
    });
share|improve this question

4 Answers 4

up vote 2 down vote accepted

I think you may be looking for the success property of $.ajax?

returnVariable = NULL;
$.ajax({
  url:"yourphp.php",
  type:"POST",
  async:false,
  data:{quantity:Quantity},
  success:function(d){
    returnVariable = d;
  }
});

You can also achieve it using .done():

$.ajax({
  //Properties
}).done(function(d){
  returnVariable = d;
});
share|improve this answer
    
The ajax call is asynchronous, so the value will be assigned after the http call and return - which could take seconds. –  Henry Florence Oct 28 '13 at 14:13

You need to assign the result of argument passed to your success handler:

$.ajax({
  url: "yourURL.com",
  data: jsonString,
  dataType: 'json',
  success: function(data){
    var myNewJSVar = data;
  }
});
share|improve this answer

use $.post(); instead...

In your case you can use:

$.post('url-to-post', {'var-name':'var-value'}, function(data) {
    // here 'data' has the data returned by your .php file
});

Here the documentation for $.post() http://api.jquery.com/jQuery.post/

share|improve this answer

Not quite store the result to the ajax query in a global variable:

$(document).ready(function() {
  var quantity = parseInt(str);
  var result = null;
  $.ajax({url:"yourphp.php",
     type:"POST",
     async:false,
     data:quantity,
     success: function(data) { 
        result = data; 
     }
  });
});

The ajax call is asynchronous, so the value will be assigned after the http call and return - which could take seconds. The success callback will be run once the http call has a result from the server. Usually the code to update your html page will be run here maybe $('resultDiv').html(data);. This is what the .load() call does in your previous post.

Jquery provides lots of shortcut methods like .post(), .load(), all the same effects can be achieved using .ajax() although it will involve more typing!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.