0

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'];

enter image description here

6
  • 3
    Use $('#FORMID').serialize() in data option. :) api.jquery.com/serialize Commented Jun 26, 2012 at 11:50
  • can you please post error what you got ?? Commented Jun 26, 2012 at 11:51
  • @Joy , i already tried using serialize() ,same all variable in php file is equal undefined, by the way am getting the values of input when user click on next page NOT when user click on submit button , and am not getting any error its just like , javascript not sending the values or am trying read value in php file in wrong way . Commented Jun 26, 2012 at 12:06
  • If you use .serialize() then you have to access the date with $_POST['FIELD_NAME']. Are all those keys your are using with $_POST[] field names in the form? Commented Jun 26, 2012 at 12:10
  • @Joy , yes , actually am already used this method in same page. Commented Jun 26, 2012 at 12:14

3 Answers 3

1

all should be an object, not an array:

var all = {};

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.

Sign up to request clarification or add additional context in comments.

1 Comment

ok i tried as you sead i have changed (all) variable to an object then send it as $.post('test.php',all,function(data))... still cant see the variable in php page !!
1

You better run JSON.stringify() on the dictionary, then use JSON_decode() in PHP to retrieve the array.

Comments

0

Sorry if I didn't understand well, but why you don't use the html attribute like this?

<input type="text" name="all[nameOfText]" />
<input type="text" name="all[nameOfText2]" />

<input type="radio" name="all[nameOfRadio]" />
...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.