Jaidan Brown
Jaidan Brown

Reputation: 25

Ajax data won't $_POST to php page

I have some Ajax code on one of my webpages:

  <script type="text/javascript">
  function update(obj) {
      $.ajax({
        url: 'php/process.php',
                method: 'POST',
        data: {
          orderID: obj.id
        },
        success: function (data) {
          console.log(data);
        }
      });
    }
  </script>

I then have a page called "process.php" where I have this simple code:

<?php

  $orderID = $_POST['orderID'];
  echo $orderID;

?>

I have it so that when a button is pressed, the update() function is run using "onclick()". When I do click the button, i get the correct output in the browser console, but the page does not redirect to the process.php page. Any help would be appreciated :)

Upvotes: 1

Views: 43

Answers (1)

Rory McCrossan
Rory McCrossan

Reputation: 337743

You need to use type, not method, to set a POST request. The default is GET, hence why your PHP receives no data. Try this:

$.ajax({
  url: 'php/process.php',
  type: 'POST',
  data: {
    orderID: obj.id
  },
  success: function (data) {
    console.log(data);
  }
});

When I do click the button, i get the correct output in the browser console, but the page does not redirect to the process.php page

That's the whole point of AJAX...

Upvotes: 4

Related Questions