How do I PHP-unserialize a jQuery-serialized form?
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"
Serialize form data to PHP
In your PHP, you start off with this:
echo "1";
When using AJAX, an echo
is the same as a return
, so this is where your function stops. Remove all unnecessary echo
's or your function will simply not continue.
Furthermore, you are using spaces in your HTML name attributes:
<input type="text" name="first name"> <!-- Incorrect -->
Use underscores instead or you will not be able to retrieve the value properly:
<input type="text" name="first_name"> <!-- Correct -->
Afterwards, $firstName = $_POST["first_name"];
is the correct way to retrieve your values in PHP.
serialize/unserialize in jQuery
jQuery's serialize/serializeArray only works for form elements. I think you're looking for something more generic like this:
http://code.google.com/p/jquery-json/
This plugin makes it simple to convert to and from JSON:
var thing = {plugin: 'jquery-json', version: 2.2};
var encoded = $.toJSON(thing);
//'{"plugin":"jquery-json","version":2.2}'
var name = $.evalJSON(encoded).plugin;
//"jquery-json"
var version = $.evalJSON(encoded).version;
// 2.2
Most people asked me why I would want
to do such a thing, which boggles my
mind. Javascript makes it relatively
easy to convert from JSON, thanks to
eval(), but converting to JSON is
supposedly an edge requirement.This plugin exposes four new functions
onto the $, or jQuery object:
toJSON
: Serializes a javascript object, number, string, or arry into JSON.evalJSON
: Converts from JSON to Javascript, quickly, and is trivial.secureEvalJSON
: Converts from JSON to Javascript, but does so while checking to see if the source is actually JSON, and not with other Javascript statements thrown in.quoteString
: Places quotes around a string, and inteligently escapes any quote, backslash, or control characters.
PHP unserialize jQuery FORM post
Pass the serialized string as the data parameter to $.post not an object whose data parameter is the serialized string
$.post("events.php?action=send", $("#form").serialize() , function(data, error) { }
Now you'll be able to access $_POST['guest']
etc
Can't retrieve jQuery serialized form data with PHP $_POST[] variable
use $(this).serializeArray()
instead of $(this).serialize()
Reference:- https://api.jquery.com/serializeArray/
The difference between both:-https://stackoverflow.com/a/10430571/4248328
You need to do like below:-
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<form id='contact'>
<input type='text' name='modal_name' id='modal_name' />
<input type='email' name='modal_email' id='modal_email' />
<textarea name='modal_message' id='modal_message'></textarea>
<input type="submit" value = "submit">
</form>
<script type="text/javascript">
$('#contact').on('submit', function(e) {
e.preventDefault();
if (modal_name && modal_email && modal_message) {
var data = $(this).serializeArray();
data.push({name: 'action', value: 'send_message'});
$.post('query.php', data, function(response) {
$('.modal').append(response);
});
}
});
</script>
And in php:-
<?php
if(!empty($_POST)){
echo "<pre/>";print_r($_POST);
}
?>
It will output like this:-
<pre/>Array
(
[modal_name] => sdsadsa
[modal_email] => a@gmail.com
[modal_message] => sadada
[action] => send_message
)
Note:- if you want to use serialize()
only then do like below:-
<script type="text/javascript">
$('#contact').on('submit', function(e) {
e.preventDefault();
if (modal_name && modal_email && modal_message) {
var data = $(this).serialize()+ '&action=send_message';
$.post('query.php', data, function(response) {
$('.modal').append(response);
});
}
});
</script>
Retrieving serialize data in a PHP file called using AJAX
Your js should be like this:
var str = $("form").serializeArray();
$.ajax({
type: "POST",
url: "update.php",
data: str,
success: function(value) {
$("#data").html(value);
}
});
With php you should loop your result array.
$box = $_POST['box'];
foreach ($box as $x) {
echo $x;
}
Edit:
You have to use serializeArray function in jQuery. Then it will work with this code.
Related Topics
How to Execute Ssh Commands Via PHP
Osx 10.10 Curl Post to Https Url Gives Sslread() Error
Error in PHP5 ..Unable to Load Dynamic Library
Fatal Error: Class 'Soapclient' Not Found
PHP Variables in Anonymous Functions
PHP Curly Brace Syntax For Member Variable
How to List All Months Between Two Dates
Override PHP Base Dependency in Composer
PHP Convert String to Hex and Hex to String
PHP: How to Check If Image File Exists
PHP Buffer Ob_Flush() Vs. Flush()
Access PHP Var from External JavaScript File
In a PHP/Apache/Linux Context, Why Exactly Is Chmod 777 Dangerous
How to Run PHP Code When a User Clicks on a Link