PHP + Jquery - pass value through ajax to php and check variable

I can’t get the following PHP + jQuery to work - all I want to do is script pass the value via ajax and get php to grab it, check if it matches and add 1 for evaluation.

This is the code I wrote:

<?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 changed GET to POST in both cases, and no joy - there is something else wrong.

+4
source share
5 answers

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

I can’t understand what you are trying to do ... Let me modify your code so that it at least makes sense and documents what is happening. It seems like the problem is that you are calling location.reload from your success handler.

//ajax.php - returns 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> 
+17
source

You are using POST in your jQuery, but you are trying to get GET in php.

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

+2
source

Replace $_GET with $_POST , and you are there. Basically, POST and GET are two different ways of passing variables into a script. The get method in php can also be attached at the end of the URL: script.php?variable=value , and it's really easy to crack. While the post method can be sent using forms or ajax calls, and it is pretty safe, at least more than get. I also suggest that you check to see if any GET or POST variable is set before calling it so that you can prevent stupid notification errors.

Just use the following code:

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

You can also delete

 }else{ // do nothing } 

part of the script, since the else condition is optional.

0
source

You are sending data using Ajax POST, but trying to read it from GET. Either use type: "GET" in your Ajax call, or $_POST['name'] in your PHP.

0
source

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

You can fix your problem by changing $ _GET ['name'] to $ _POST ['name']

0
source

All Articles