This is the jQuery code i'm using to send the form details to a php function:
jQuery(document).ready(function($) {
jQuery('.submit').click(function(){
var str = $("#ajaxForms").serialize();
var data = {
action: 'myajax-submit',
serialize: str,
beforeSend: function(){
alert('Sending...');
}
};
jQuery.post(MyAjax.ajaxurl, data, function(response) {
alert('Got this from the server: ' + response);
});
return false;
});
});
and this is the php function:
function myajax_submit() {
$whatever = $_POST['serialize'];
echo $whatever;
die();
}
Everything works, but when the alert box appears the text displays a string of values from my html form #ajaxForms
. I believe this is because the php function echos the $_POST['serialize']
.
In my form i have an input box such as:
<input id="postID" name="postID" value="First Name" type="text" />
but when i try echo the $_POST['postID']
variable in the php it doesn't display anything in the alert box.
I thought by sending the form data serialized to the php function i could use the $_POST variable associated with the form inputs?
Help appreciated. :)
By serializing the form inputs with jQuery's serialize you create a string like:
a=1&b=2&c=3&d=4&e=5&postID=10
In order to get the postId you need to de-serialize the $_POST['serialize']. In order to do so you should do something like:
parse_str($_POST['serialize'], $whatever);
Now on $whatever['postID']
is what you are searching for.
Edit: Fixing parse_str() :)
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