2

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?

3
  • 1
    Your example should work, var_dump should return the string 'count_g'. Try clearing the cache. Commented Oct 8, 2011 at 23:29
  • What do you see when you do a var_dump($_POST)? Commented Oct 8, 2011 at 23:34
  • 2
    @James Allardice: Undelete your answer, the quotes on the value were the problem, the quotes on the keys was just a side issue. Commented Oct 8, 2011 at 23:47

1 Answer 1

2

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.

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

2 Comments

Yeah, you're right. I was just about to edit. In fact, in some cases, quotes are required (I don't think the underscore is one of those cases though). In fact that means my answer is completely wrong... $_POST["start_g"] should contain "countg", and definitely should not be null. Deleting...
yeah not sure why it was returning null...but removing the quotes around it fixed the problem...odd..i must have had something else that was wrong that got fixed.

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.