algorithmicCoder
algorithmicCoder

Reputation: 6789

Why is this POST variable sent via AJAX Null? (jquery/php)

This javascript is for a "load more" functionality. That grabs a fixed number of elements from load.php when a button #moreg is clicked.

$(function(){
    $("#moreg").click(load);
    var countg = -1;
    load();


    function load() {
        var num = 1;
        countg += num;
        $.post( "load.php", 
                {'start_g': 'countg', 'name':'<?=$name?>' }, 
                function(data){
                    $("#posts").append(data);
                }
        );
    }
});

in load.php simply doing a var_dump($_POST['start_g']); yields a null variable.

Not too helpful...what am I doing wrong?

Upvotes: 2

Views: 392

Answers (1)

James Allardice
James Allardice

Reputation: 165971

In the map of parameters that get sent with the POST request, the keys do not have to be quoted (but it doesn't make any difference):

$.post("load.php", {
    start_g: countg, 
    name: '<?=$name?>' 
}, function(data) {
    $("#posts").append(data);
});

And I've also removed the quotes from countg, because you're trying to use the value of a variable. If it's quoted, you're simply going to pass the string "countg" rather than the value of the variable named countg.

Upvotes: 2

Related Questions