Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
320 views
in Technique[技术] by (71.8m points)

javascript - PHP doesnt see posted data from ajax by serializeArray()

I have a form, running through jquery validation which then submits via ajax to a PHP script to handle backend functions. Ajax collects form values through serializeArray() and looks to do the job. Script fires and data is sent through(I think) to PHP. I've tried probably close to 100 combinations to receive the data at the PHP side but with no luck. I'm convinced this must be simple, something I've overlooked. Code for the ajax is below, along with a screenshot of developer tools showing what's being sent.

No matter what I try on the PHP side, I either get an empty array, NULL through $_POST/$_GET. I've tried json_decode, parsing the string, var_dump etc.

var data=$(form).serializeArray();
$.ajax({
  cache: false,
  type: "POST",
  dataType: "JSON",
  url: "process/create_site.php",
  data: data,
  success: function(response) {
    console.log(response);
    //$(form).html("<div id='message'></div>");
    //$('#message').html("<h2>Your request is on the way!</h2>")
    //  .append("<p>someone</p>")
    //  .hide()
    //  .fadeIn(1500, function() {
    //    $('#message').append("<img id='checkmark' src='images/ok.png' />");
    //  });
  }
});

Image of the PHP output from ajax submission Image of the ajax being submitted

question from:https://stackoverflow.com/questions/65934555/php-doesnt-see-posted-data-from-ajax-by-serializearray

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I managed to get to the bottom of this, after an embarrassing amount of time. I'd like to post the simple reason here to help others.

The entire JS block was wrapped in $(document).ready(function(){ which was causing the values to be stripped when posting to the PHP.

I can't find any documentation or answer to a question with a similar scenario - so here it is!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...