简体   繁体   中英

Nothing's in $_POST. Is there something wrong with my code?

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
    });

});

我建议从以下步骤开始:

var submit_basic = $.post('index.php/submit_data/pass_basic_data', get_experience_data());

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.

您需要执行方法get_experience_data,否则您将传递不执行它的函数

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 );

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+= ' }';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM