1

I'm trying to put together a working demo (including mysql) of a form that uses jquery to serialize form data and php to retrieve the data so that it can be passed to a mysql query.

The form seems to be posting data correctly but I'm not sure how to set up the processing script which sees $_POST to unserialize the data so that I can pass it to mysql. You can see the demo at http://www.dottedi.biz/demo/code/ajax/serialize .

I have tried using:

$data=unserialize($_POST['data']);

to unserialize the data but it comes back empty. A simple print_r ($_POST); returns the array data from the form. You can see that if you test the demo. Suggestions please?


Added info - the contents of the script.js file:

$(document).ready(function() {
  $('form').submit(function(evt) {
    evt.preventDefault();
    $.each(this, function() {

      var input = $(this);
      var value = input.val();
      var column = input.attr('name');
      var form = input.parents('form');
      var linked_col = form.find('#associated').attr('name');
      var linked_val = form.find('#associated').val();

      // var serializedData = $(this).serialize(); 

      $("#Status").html( "" );

      $.ajax({
        url: "update.php",
        data: {
          val: value,
          col: column,
          id: linked_col,
          id_val: linked_val
        },
        type: "POST",
        success: function(html) {
          $("#Status").html( html );
        }

      });
    });
  });
});

9/22 - shortened script.js

$(document).ready(function() {
  $('form').submit(function(evt) {
    evt.preventDefault();
     $.each(this, function() {

      $("#Result").html( "" );

      $.ajax({
        url: "update.php",
        data: $('form').serialize(), // works to post data
        type: "POST",
        success: function(html) {         
          $("#Result").html( html );
        }

      });
    });
  }); 
});

Comment - I tested and it seems that the same data is posted using serialize as above vs creating a variable like var serializedData = $(this).serialize() and posting the variable, but this is shorter.

Bob M
  • 393
  • 1
  • 3
  • 15
  • PHP `unserialize()` is not the reverse of a javascript `serialize()` The data in `$_POST` should be processed just like it came from a simple set of form inputs – RiggsFolly Sep 20 '16 at 22:18
  • Storing an Array in mysql you need to serialize it. But passing it to javascript you have to unserialize and parse it into JSON. –  Sep 20 '16 at 22:32

1 Answers1

0

Perhaps you should

$('form').submit(function(evt) { 
 // Serialize the data in the form
var serializedData = $(this).serialize();
//send off serializedData in your ajax
 }

THEN

php script will have

$data=json_decode($_POST['data']);

NEW WAY

  $.ajax({
    url: "update.php",
    data: {'data': $('form').serialize()}, // works to post data
    type: "POST",
    success: function(html) {         
      $("#Result").html( html );
    }
  • Okay I get that I need to use json_decode in the php script and add a new variable in the script.js, but my javascript/jquery knowledge is limited. How do I use it and what exactly do I change in the script to make it work? – Bob M Sep 20 '16 at 23:47
  • I got serialize working to pass data to the processing php script, but I am not getting anything out of a $data variable. $_POST returns an ordinary array. # $data=json_decode($_POST['data']); # json_decode is still firing blanks. Is there another way to get this to work? Should this work? – Bob M Sep 22 '16 at 19:46
  • @BobM just var_dump($_POST); what do you get? – Gavindra Kalikapersaud Sep 23 '16 at 01:15
  • Take a look at the demo and you'll see `var_dump ($_POST): array(3) { ["id"]=> string(1) "1" ["state"]=> string(11) "Connecticut" ["fname"]=> string(15) "Charlie Chaplin" }` print_r and var_dump both show good _POST data, but there is nothing that populates a $data variable which the post where I found the tutorial discussed. I am wondering if that was a miscommunication? – Bob M Sep 23 '16 at 14:14
  • Option #2 which I've already tested and is workable is to create a foreach loop on $_POST variables and process that way. – Bob M Sep 23 '16 at 14:19
  • either way you have your form data – Gavindra Kalikapersaud Sep 23 '16 at 23:04
  • I went back through the other post where I saw the references to the $data variable in the PHP file (http://stackoverflow.com/questions/24858331/update-form-using-ajax-php-mysql) and I didn't see it work there. Regardless, I now know of and how to use serialize and retrieve serialized data in php - and that was big. The 2 week's googling and testing solutions was well worth it. With that knowledge in hand I successfully created a listview with checkboxes (select/unselect all, etc) and got that to pass the data to PHP where I can process it. Very happy camper here. :) – Bob M Sep 26 '16 at 15:01