I have a working program but I just learned about the 'serialize' option in JQuery and PHP that I think can clean up my code. Problem is that after reading a few great posts on here im still not getting it to work. Can someone take a look if ya dont mind?
Here is what I am sending to the PHP page (serialized form data):
contForm is the name of my form dispAdd is the callback which Im not worried about getting to just yet
var formData = $('#contForm').serialize();
$.post('functs.php',formData,dispAdd);
function dispAdd(status) {
if (status=='added') {
$('#main').html('<div>Your have been added to the mailing list</div>');
} else {
if ($('#fail').length==0) {
$('#main'.prepend('<div>This email address is already subscribed to our mailing list</div>');
}
}
}
I know it is sending the string over but I am having a problem OUTPUTTING the results. I plan on using the output in a PDO to access a Database. I was trying parse_str but its just not working. Here is how I was formatting:
function addMember()
{
$params = array();
parse_str($_POST, $params);
Thats as far as I can get. I am trying to figure out how to access the new variables (the serialized form data). Any tips?
1 Answer 1
Your request gets sent via AJAX as application/x-www-form-urlencoded and is decoded into $_POST.
If you have an <input name="test" /> inside your form, after posting it would become available as:
$_POST['test']
5 Comments
$_POST['an-input-name'] should just work.jQuery.serialize() takes all the input elements in your form and creates a string that's submitted to PHP; this in turn is decoded into $_POST.
$_POSTshould already contain your data as an array.unserializeit in your PHP code? (php.net/manual/en/function.unserialize.php)