am trying send an array to php file using $_POST ,still always getting an undefined variable
any idea !!
var all = [];
var textInput = $('input[type=text]');
var checkBox = $('input[type=checkbox]');
var radio = $('input[type=radio]');
textInput.each(function () {
if ($(this).val() != "") {
name = $(this).attr('name');
value = $(this).attr('value');
all[name] = value;
}
});
checkBox.each(function () {
if ($(this).attr("checked")) {
name = $(this).attr('name');
value = $(this).attr('value');
all[name] = value;
}
});
radio.each(function () {
if ($(this).attr("checked")) {
name = $(this).attr('name');
value = $(this).attr('value');
all[name] = value;
}
});
var getSelect = $('#experince');
if (getSelect[0].selectedIndex != 0) {
name = getSelect[0].name;
value = getSelect[0].selectedIndex;
all[name] = value;
}
$.post('test.php', all, function (data) {
alert(data);
$.each(data, function (key, value) {
alert(value);
});
}, 'json');
PHP FILE
if(!isset($_POST['workF'])){
$_POST['workF'] ="undefine";
}
if(!isset($_POST['workP'])){
$_POST['workP']="undefine";
}
if(!isset($_POST['gender'])){
$_POST['gender']="undefine";
}
if(!isset($_POST['experince'])){
$_POST['experince']="undefine";
}
if (!isset($_POST['jobs'])){
$_POST['jobs']="undefine";
}
if(!isset($_POST['location'])){
$_POST['location']="undefine";
}
$work_type_f = $_POST['workF'];
$work_type_p = $_POST['workP'];
$gender = $_POST['gender'];
$experince = $_POST['experince'];
$jobs = $_POST['jobs'];
$location =$_POST['location'];

allshould be an object, not an array:jQuery will do the necessary escaping for you when passing it to
$.post, but it won’t read the object properties of an array.However as Joy stated, you’re just trying to mimic a form here. Make sure the inputs are all contained inside a single form and use
.serialize()to get a string which can be send along with a HTTP request.