1

I'm new to AJAX, and can't get a php function to run.

The AJAX post request is working as it should, here's the code:

function thumbs(i) {
    $('.thumbs-up' + String(i)).click(function(){
        $(this).addClass('up');
        $.ajax({
            type:"POST",
            url:"item.php",
            data:'act=up&function' + String(i) + '=true&user=' + email,
            success: function(){
            }
        });
    });

for (var i = 1; i <= count; i++) {
    thumbs(i);
}

The above works, I receive all the correct values.

Then the PHP code that should run is here:

for ($i = 1; $i <= $count; $i++) {

    if ($_POST['function' . $i] == 'true') {
        //code that should run but does not
    }
}

The count variable is the same. Is there anything wrong here?

1
  • The problem was that I didn't include the full url in my ajax post request. It was a dynamic url, so i just had to add the GET variable to the end of the url string. I apologize for not including this, there's no way anyone would have known. Commented Mar 4, 2015 at 16:58

1 Answer 1

2

You're using get parameters instead of sending post parameters. See the example in the docs

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
})
.done(function( msg ) {
    alert( "Data Saved: " + msg );
});
Sign up to request clarification or add additional context in comments.

4 Comments

The data method is way better than trying to smack together something using String, though it would be nice if your example here was based on the original question.
I think it's easily replicated, don't you?
Exactly why I'm saying a better answer would have done that. Examples are great, but demonstrations are even better.
This turns out to be not true. jQuery does POST the data as part of the request body and not over the Querystring.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.