1

I am fairly new to JavaScript and PHP, and it's the first time to encounter and use JSON. I think my AJAX request is fine, but even so, the $_POST array is empty.

Here is the AJAX call:

$( "#submit" ).click( function() {
    var submit_basic = $.post( 'index.php/submit_data/pass_basic_data',
                                get_experience_data()
                             );
    submit_basic.done( function(data) {
       alert(data);  // for debugging purposes
    });
});

And this is the function that takes the data from a table:

function get_experience_data() {

    var temp, data_exp = [];
    $( "#exptable tbody tr" ).each( function() {

        temp = {
            company: $(this).find('td').eq(0).html(),
            position: $(this).find('td').eq(1).html(),
            datestart: $(this).find('td').eq(2).html(),
            dateend: $(this).find('td').eq(3).html(),
            description: $(this).find('td').eq(4).html()
        };

        data_exp = data_exp.concat(temp);

     });

     return data_exp;
}

And for reference, the destination controller function that only prints the $_POST array (by the way I am using CodeIgniter):

public function pass_basic_data() {
    var_dump($_POST);        
}

Can you please pinpoint the error I've made, since I can't find it. Thanks a lot!

UPDATE: I am getting this message in particular:

array(1) {
  ["undefined"] =>
  string(0) ""
}

UPDATE:

Thanks for all the help guys! I already solved it. It made me dance all around the room. I wrapped the return value in a {name : value} pair.

$( "#submit" ).click( function() {
    var post_vars = get_experience_data();
    var submit_basic = $.post( 'index.php/submit_data/pass_basic_data',
                                {object: post_vars}
                             );
    submit_basic.done( function(data) { 
       alert(data);  // for debugging purposes
    });

});
Sri Harsha Chilakapati
  • 11,744
  • 6
  • 50
  • 91

5 Answers5

0

I would suggest doing the following for a start :

var submit_basic = $.post('index.php/submit_data/pass_basic_data', get_experience_data());
Laurent S.
  • 6,816
  • 2
  • 28
  • 40
  • Thanks for pointing out, that was a typo, but still I am getting this error which makes me think that the $_POST is empty: array(1) { ["undefined"]=> string(0) "" } – Oliver Acera Galleguez May 10 '13 at 10:22
  • You're actually sending 'undefined' to the server because your function call to get_experience_data() is failing (or the result is undef). – deed02392 May 10 '13 at 10:41
0

Try this:

var submit_basic = $.post('index.php/submit_data/pass_basic_data', get_experience_data());
// you need to add parentheses here --------------------------------------------------^

When you pass a function to $.post() it assumes it is a callback that is to be called after the response is received. What you want to do is call the function and pass it's return value in to $.post().

By the way, this line:

data_exp = data_exp.concat(temp);

could be replaced with:

data_exp.push(temp);

No need to be creating a new array every time if you're just adding a value to the end.

nnnnnn
  • 147,572
  • 30
  • 200
  • 241
0

You need to execute the method get_experience_data, otherwise you are passing the function not executing it

acj
  • 134
  • 4
0

try troubleshooting the actual jquery, not the php part. but heres a suggestion:

var post_vars = get_experience_data();
var submit_basic = $.post( 'index.php/submit_data/pass_basic_data', post_vars );
ppseprus
  • 538
  • 1
  • 8
  • 26
0

here is a conclusion to my previous answer plus the comments, but im still not sure if you could stingify arrays in arrays..

var submit_basic = $.ajax({
    type: "POST",
    url: 'index.php/submit_data/pass_basic_data',
    dataType: 'json',
    async: false,
    data: JSON.stringify(get_experience_data()),
    success: function (response) {
        alert(response);
    }
});

UPDATE: modify your get_experience_data function according to this jsfiddle script

like this:

temp = '{ ';
temp+= '"company":"'  +$(this).find('td').eq(0).html()+  '", ';
temp+= '"position":"'  +$(this).find('td').eq(1).html()+  '", ';
temp+= '"datestart":"'  +$(this).find('td').eq(2).html()+  '", ';
temp+= '"dateend":"'  +$(this).find('td').eq(3).html()+  '", ';
temp+= '"description":"'  +$(this).find('td').eq(4).html()+  '"';
temp+= ' }';
Joe
  • 15,205
  • 8
  • 49
  • 56
ppseprus
  • 538
  • 1
  • 8
  • 26