Using $('#form').serialize(), I was able to send this over to a PHP page. Now how do I unserialize it in PHP? It was serialized in jQuery.
Thank you !!
Using $('#form').serialize(), I was able to send this over to a PHP page. Now how do I unserialize it in PHP? It was serialized in jQuery.
Thank you !!
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"
I'm unable to pull the data via POST without somehow deserializing. If you send the data via the jQuery serialize through one variable then you need to parse the data once it gets to php.
jQuery('form').submit(function() {
var loadUrl = "mail.php";
var formData = jQuery(this).serialize();
jQuery.post(
loadUrl,
{ formdata: formData },
function(data){
if (data) {
alert(data);
}
else {
alert('no data');
}
},"html");
return false;
});
Produces the equivalent of:
$_POST['formdata'] = 'name=Joe&[email protected]&message=something+to+say';
So, you can't just try:
$name = $_POST['name'];
$email = $_POST['email'] ;
$message = $_POST['message'];
You'd need to:
function parse_query($var) {
$var = html_entity_decode($var);
$var = explode('&', $var);
$arr = array();
foreach($var as $val)
{
$x = explode('=', $val);
$arr[$x[0]] = $var = urldecode($x[1]);
}
unset($val, $x, $var);
return $arr;
}
$formdata=parse_query($_POST['formdata']);
$name = $formdata['name'];
$email = $formdata['email'] ;
$message = $formdata['message'];
Unless there is an easier way I'm unaware of.
good function but if you serialize checkbox ... you don't get the unserialized array ...
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
Hope that's helpful. Good luck!
I don't know which version of Jquery you are using, but this works for me in jquery 1.3:
$.ajax({
type: 'POST',
url: your url,
data: $('#'+form_id).serialize(),
success: function(data) {
$('#debug').html(data);
}
});
Then you can access POST array keys as you would normally do in php.
Just try with a print_r()
.
I think you're wrapping serialized form value in an object's property, which is useless as far as i know.
Hope this helps!