4

I can't get the following PHP + jQuery to work - all I want the script to do is pass the value through ajax, and get the php to grab it, check it matches and add 1 to score.

This is the code I've written:

<?php
$score = "1";

$userAnswer = $_POST['name'];

if ($_POST['name'] == "145"){
    $score++;
}else{
    //Do nothing
}

echo $score;

?>


<script type="text/javascript">

$(document).ready(function() {

    $("#raaagh").click(function(){

    var value = "145";

    alert(value);

    $.ajax({
        url: 'processing.php', //This is the current doc
        type: "POST",
        data: ({name: value}),
        success: function(){
            location.reload();
       }
    });


    });
});

</script>

<p id="raaagh">Ajax Away</p>

Thanks for the help, I've changed GET to POST in both instances, and no joy - there's something else wrong.

2
  • And what exactly is the problem? Commented Nov 18, 2010 at 18:13
  • Questions without description of the problems are kind of hard to answer. "It doesn't work" is not a good enough description. Commented Nov 18, 2010 at 18:27

5 Answers 5

16

First of all: Do not go back to the dark ages... don't use the same script to generate HTML and to respond to an ajax request.

I can't make any sense of what you are trying to do... Let me change your code so it at least makes some sense and document what's going on. It seems like the problem is the fact that you are calling location.reload from your success handler.

// ajax.php - Outputs 2 if the name parameter is 145, 1 otherwise (????)

<?php
$score = "1";    
$userAnswer = $_POST['name'];    
if ($_POST['name'] == "145"){
    $score++;
}       
echo $score;    
?>

// test.html

<script type="text/javascript">  
$(document).ready(function() {    
    $("#raaagh").click(function(){    
        $.ajax({
            url: 'ajax.php', //This is the current doc
            type: "POST",
            data: ({name: 145}),
            success: function(data){
                // Why were you reloading the page? This is probably your bug
                // location.reload();

                // Replace the content of the clicked paragraph
                // with the result from the ajax call
                $("#raaagh").html(data);
            }
        });        
    });
});

</script>

<p id="raaagh">Ajax Away</p>
2
  • You've just saved me a huge headache - I'm still using the self reload thing, so that wasn't the bug - but whatever else has changed has fixed it. THANK YOU!
    – Richard
    Commented Nov 18, 2010 at 19:36
  • 1
    "Whatever else has changed" is the fact that you're not outputting the html part of the page for the ajax call now. Commented Nov 18, 2010 at 20:19
2

You use POST in your jQuery, but you try and get a GET in you php.

BTW it is good practice to check if a GET/POST variable is set before reading it. using the isset() function.

1

Replace $_GET with $_POST and there you are. Basically POST and GET are two different way to pass variables to a script. The get method in php can also be attached at the end of a url : script.php?variable=value and it is really easy to hack. While the post method can be submitted with forms or ajax calls and it is pretty safe, at least more than the get. Also i'd suggest you to check whatever a GET or POST variable is set before calling it, so that you can prevent stupid notice errors.

Just use the following code:

if (isset($_POST['var']) and !empty($_POST['var'])) { // do something } 

You can also delete the

}else{
// do nothing
}

part of the script, since the else clause it is not necessary always.

1
0

You're submitting the data with an Ajax POST, but trying to read it out of a GET. Either use type: "GET" in your Ajax call or $_POST['name'] in your PHP.

0

The problem is you are using jQuery to POST your value, yet you are reading it with GET.

You should be able to fix your problem by changing your $_GET['name'] to $_POST['name']

Not the answer you're looking for? Browse other questions tagged or ask your own question.