How to get POST values ​​from serializeArray in PHP?

I am trying to use this new method that I saw serializeArray() .

 //with ajax var data = $("#form :input").serializeArray(); post_var = {'action': 'process', 'data': data }; $.ajax({.....etc 

So, I get these key value pairs, but how do I access them with PHP?

I thought I needed to do this, but this would not work:

 // in PHP  $data = json_decode($_POST['data'], true); var_dump($data);// will return NULL? 

Thanks Richard

+4
source share
7 answers

Like Gumbo, you most likely will not handle the json_decode return value.
Try

 $data = json_decode($_POST['data'], true); var_dump($data); 

If $data does not contain the expected data, then var_dump($_POST); to see what Ajax call sent to your script. Perhaps you are trying to access JSON with the wrong key.

EDIT
In fact, you have to make sure that you really send JSON in the first place :)
JQuery docs for serialization. The .serializeArray () method creates a JavaScript array , ready to be encoded as a JSON string. Ready to be non-JSON encoded. Apparently there is no Object2JSON function in jQuery, so use https://github.com/douglascrockford/JSON-js/blob/master/json2.js as a third-party lib library or use http://api.jquery.com/serialize / instead.

+4
source

The returned JSON structure is not a string. You must use a plug-in or a third-party library to “back up” it. See Further Information:

http://www.tutorialspoint.com/jquery/ajax-serializearray.htm

+4
source

The OP could actually use serializeArray () instead of simple serialize () by making the following changes:

 //JS var data = $("#form :input").serializeArray(); data = JSON.stringify(data); post_var = {'action': 'process', 'data': data }; $.ajax({.....etc // PHP $data = json_decode(stripslashes($_POST['data']),true); print_r($data); // this will print out the post data as an associative array 
+4
source

it can be used with serialize array and json_decode ()

 // js var dats = JSON.stringify($(this).serializeArray()); data: { values : dats } // ajax call //PHP $value = (json_decode(stripslashes($_REQUEST['values']), true)); 

values ​​are accepted as an array

each value can be obtained using $ value [0] ['value'] each html component name is given as $ value [0] ['name']

 print_r($value) //gives the following result Array ( [0] => Array ( [name] => name [value] => Test ) [1] => Array ( [name] => exhibitor_id [value] => 36 ) [2] => Array ( [name] => email [value] => test@gmail.com ) [3] => Array ( [name] => phone [value] => 048028 ) [4] => Array ( [name] => titles [value] => Enquiry ) [5] => Array ( [name] => text [value] => test ) ) 
+3
source

I have a very similar situation and I believe that Ty W has the correct answer. I will give an example of my code, just in case there are enough differences to change the result, but it seems that you can just use the published values, as usual, in php.

 // Javascript $('#form-name').submit(function(evt){ var data = $(this).serializeArray(); $.ajax({ ...etc... // PHP echo $_POST['fieldName']; 

This is a really simplified example, but I think the key point is that you do not want to use the json_decode() method, since it probably produces unwanted output.

+1
source

Does javascript not change the way values ​​are published? If you cannot access the values ​​via PHP, as usual, through $_POST['name_of_input_goes_here']

edit: you can always unload the contents of $ _POST to find out what you get from sending the javascript form using print_r($_POST) . This will give you some idea of ​​what you need to do in PHP to access the data you need.

0
source

You can use this function in php to access serializeArray ().

 <?php function serializeToArray($data){ foreach ($data as $d) { if( substr($d["name"], -1) == "]" ){ $d["name"] = explode("[", str_replace("]", "", $d["name"])); switch (sizeof($d["name"])) { case 2: $a[$d["name"][0]][$d["name"][1]] = $d["value"]; break; case 3: $a[$d["name"][0]][$d["name"][1]][$d["name"][2]] = $d["value"]; break; case 4: $a[$d["name"][0]][$d["name"][1]][$d["name"][2]][$d["name"][3]] = $d["value"]; break; } }else{ $a[$d["name"]] = $d["value"]; } // if } // foreach return $a; } ?> 
0
source

Source: https://habr.com/ru/post/1300875/


All Articles