0

I have built multiple forms that collect data from a user and compile it into a JS object, which i JSON.stringify() and pass through the following AJAX code as "post_data":

$(document).ready(function() {
  $('#php_post_show').click(function() {
    console.log(post_data);
    $.ajax({
      type: "POST",
      url: "http://localhost:80/php_beginner/index.php",
      data: {
        'post_data': post_data
      },
      success: function(data, textStatus, jqXHR) {
        console.log(data); //if it returns any data
        console.log(textStatus); //or alert(textStatus);
      },

      error: function(jqXHR, textStatus, errorThrown) {
        console.log("There is some error");
        console.log(errorThrown);
      }
    })
  })
});

I have the following PHP:

<div id="if_sent">

<?php
if (isset($_POST['post_data'])) {

  //Retrieve the string, which was sent via the POST parameter "user"
  $post_data = $_POST['post_data'];

  //Decode the JSON string and convert it into a PHP associative array.
  echo $post_data;

  //var_dump the array so that we can view it's structure.
  var_dump($_POST);
}

var_dump($_POST);

echo "this works"
?>

</div>

When executed I get a 'success' in the console, as well as log of the correct PHP code I want to display returned in the console. The problem is my browser displaying the PHP does not update.

What is going on here? Why does my success message type out exactly what should be displayed but the page does not display the proper PHP.

The echo "this works" works, and var_dump($_POST) returns an empty array. WHY?

For context I am using XAMPP if that has anything to do with it.

Phil
  • 157,677
  • 23
  • 242
  • 245
Austin
  • 103
  • 10
  • what do you get for `console.log(post_data);`? if its `undefined` it may cause the `data: {'post_data': ` to become unset essentially posting nothing. Sanity check what actually being sent by looking at network tab – Lawrence Cherone Aug 03 '20 at 22:46
  • console.log(post_data) works as intended, it gives me my long JSON string. In Network tab it shoes the whole index.php file as it should be displayed in my browser...but why isn't my browser updating? – Austin Aug 03 '20 at 22:49
  • _"The problem is my browser displaying the PHP does not update"_ are you saying you're opening `http://localhost:80/php_beginner/index.php` directly in your browser? – Phil Aug 03 '20 at 22:52
  • @Phil, I am going to that address to test it, yes. – Austin Aug 03 '20 at 22:55
  • As you are sending raw data `file_get_contents("php://input")` is probably the way to go. – DreiDe Aug 03 '20 at 23:31

0 Answers0