user2672288
user2672288

Reputation:

Get result from Ajax Query

Ok, so I'm submitting a form and posting an ajax call as follows:

$('#pForm').on('submit', function(e) {
  e.preventDefault(); //Prevents default submit
  var form = $(this);
  var post_url = form.attr('action');
  var post_data = form.serialize();
  $.ajax({
    type: 'POST',
    url: 'calc.php',
    data: post_data,
    success: function() {
      // show the result
    }
  });
});

This fires off to calc.php which performs a quick calculation based on the values in various fields of the form. The end result is a variable $result in calc.php. How do I access this in my ajax code to display it, just an alert for now is fine...

Thanks

Upvotes: 1

Views: 69

Answers (3)

Faytraneozter
Faytraneozter

Reputation: 875

$('#pForm').on('submit', function(e) {
  e.preventDefault(); //Prevents default submit
  var form = $(this); 
  var post_url = form.attr('action'); 
  var post_data = form.serialize();
  $.ajax({
      type: 'POST',
      url: 'calc.php',
      data: post_data,
      success: function(html) {
        $("someplace").html(html);
      }
  });
});

Upvotes: 1

Nouphal.M
Nouphal.M

Reputation: 6344

$.ajax({
  .......
  success: function(data) { // here data holds your response
        // show the result
        alert(data);
  }
});

Find more info here

Upvotes: 1

Krish R
Krish R

Reputation: 22711

You can get the results using as below,

 success: function(result) {
    // show the result
    console.log(result);
  }

Upvotes: 1

Related Questions