To get the POST values from serializeArray in PHP, use the serializeArray() method. The serializeArray( ) method serializes all forms and form elements like the . serialize() method but returns a JSON data structure for you to work with.
The unserialize() function converts serialized data back into actual data.
To serialize a FormData object into a query string, pass it into the new URLSearchParams() constructor. This will create a URLSearchParams object of encoded query string values. Then, call the URLSearchParams. toString() method on it to convert it into a query string.
jQuery serialize() Method The serialize() method creates a URL encoded text string by serializing form values. You can select one or more form elements (like input and/or text area), or the form element itself. The serialized values can be used in the URL query string when making an AJAX request.
Provided that your server is receiving a string that looks something like this (which it should if you're using jQuery serialize()
):
"param1=someVal¶m2=someOtherVal"
...something like this is probably all you need:
$params = array();
parse_str($_GET, $params);
$params
should then be an array modeled how you would expect. Note this works also with HTML arrays.
See the following for more information: http://www.php.net/manual/en/function.parse-str.php
You shouldn't have to unserialize anything in PHP from the jquery serialize
method. If you serialize the data, it should be sent to PHP as query parameters if you are using a GET method ajax request or post vars if you are using a POST ajax request. So in PHP, you would access values like $_POST["varname"]
or $_GET["varname"]
depending on the request type.
The serialize
method just takes the form elements and puts them in string form. "varname=val&var2=val2"
// jQuery Post
var arraydata = $('.selector').serialize();
// jquery.post serialized var - TO - PHP Array format
parse_str($_POST[arraydata], $searcharray);
print_r($searcharray); // Only for print array
// You get any same of that
Array (
[A] => 1
[B] => 2
[C] => 3
[D] => 4
[E] => 5
[F] => 6
[G] => 7
[H] => 8
)
parse_str($_POST['whatever'], $searcharray);
In HTML page:
<script>
function insert_tag()
{
$.ajax({
url: "aaa.php",
type: "POST",
data: {
ssd: "yes",
data: $("#form_insert").serialize()
},
dataType: "JSON",
success: function (jsonStr) {
$("#result1").html(jsonStr['back_message']);
}
});
}
</script>
<form id="form_insert">
<input type="text" name="f1" value="a"/>
<input type="text" name="f2" value="b"/>
<input type="text" name="f3" value="c"/>
<input type="text" name="f4" value="d"/>
<div onclick="insert_tag();"><b>OK</b></div>
<div id="result1">...</div>
</form>
on PHP page:
<?php
if(isset($_POST['data']))
{
parse_str($_POST['data'], $searcharray);
$data = array(
"back_message" => $searcharray['f1']
);
echo json_encode($data);
}
?>
on this php code, return f1
field.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With