SupaOden
SupaOden

Reputation: 742

Posting data to php file

I have the following variable in Javascript. I want to know how to pass this data to a PHP so that I can display the contents of the data once redirected.

    postData = {
        'dates_ranges': datesAndRanges,
        'action':'build',
        'output_type': output_type,
        'form_html': formHtml,
        'width': formBuilder.width(),
        'rules':validationRules,
        'theme': theme,
    };

Upvotes: 0

Views: 125

Answers (3)

thecodeparadox
thecodeparadox

Reputation: 87083

you can use:

$.post("YOUR_URL", postData, function(response) {
    // handle with response
});

OR:

$.ajax({
  url: YOUR_URL,
  data: postData,
  type: 'post',
  success: function(response) {
    // handle with response
  }
});

And In your PHP file:

if(isset($_POST) && !empty($_POST)) {
   $d = $_POST;
   echo $d['date_range']; // and so more
}

Upvotes: 0

VisioN
VisioN

Reputation: 145478

Use JQuery post method to pass data to PHP file:

$.post("/path/to/script.php", postData, function(result) {
    // work with result
});

In PHP use $_POST global to get the variables:

print $_POST['dates_ranges'];
print $_POST['action'];
// ...

Upvotes: 1

Varol
Varol

Reputation: 1858

using jquery it goes easy & clean like this:

$.post('script.php', postData, function(response){ 
    // process/display the server response 
});

Upvotes: 0

Related Questions