Reputation: 509
I have the following js code
$('document').ready(function () {
$('#filterSub').click(function (evt) {
var data = $('form').serialize();
var gender = $('#gender').html();
$.post('data.php', {
data,
gender: gender
}, function (resultp) {
$('#List').html(resultp);
});
$.post('filter.php', {
data,
gender: gender
}, function (result) {
$('.addFilter').html(result);
});
// alert(gender);
//return false;
});
});
I am using
parse_str($_POST['data'],$_POST);
Which works perfectly fine and I am getting the form variables.
However, I am trying to get the variable 'gender' in my PHP script with
$_POST['gender']
in PHP. But it is not getting anything. (setting an alert in the script for 'gender' pops up the correct value)
Also, is there any way I can send both the post queries in one command to the different files? The data being sent is the same.
I am bad at JS so I feel there is an elementary mistake that I am making in the $.post request format.
Any help will be appreciated as I already have spent an entire day on this without any success.
Upvotes: 1
Views: 53
Reputation: 782509
The problem is that parse_str($_POST['data'], $_POST);
replaces the $_POST
array with the result of parsing the data
parameter -- any existing entries in the array are lost. So you're losing $_POST['gender']
.
You could parse it into a different array:
parse_str($_POST['data'], $data);
or you could extract the gender
parameter into a variable before you call parse_str()
.
$gender = $_POST['gender'];
parse_str($_POST['data'], $data);
Another option is to get rid of the data
parameter entirely, just post the serialized fields as separate parameters.
var data = $('form').serialize();
var gender = $('#gender').html();
var params = data + '&gender=' + encodeURIComponent(gender);
$.post('data.php', params, function (resultp) {
$('#List').html(resultp);
});
Upvotes: 3