Reputation:
I have a small problem maybe because i am a beginner in ajax programming, i make a function in ajax jquery that calls a php file, which makes a request to the database for informations about a player. When the php file replies to the ajax function, i get an object with null values as an answer.
Is there a line i've missed in my code? or something i forgot to do?
Here is my code, AJAX function:
$.ajax({
method: 'GET',
url: "webservices/get_infos.php",
timeout: kTimeout,
success: function(response) {
alert(response);
},
error: function() {
alert('error');
}
});
And the php file :
<?php
include("connexion_bdd.php");
$_GET['mail'] = $mail;
$req = $bdd->prepare('SELECT * FROM joueurs WHERE mail = ?');
$req->execute(array($mail));
$reponse = $req->fetch();
$return = array();
$return["user_name"] = $reponse["nickname"];
$return["profile_pic"] = $reponse["profile_pic"];
$return["user_id"] = $reponse["id"];
print(json_encode($return));
?>
In the success of the ajax function, i get this :
{"user_name":null,"profile_pic":null,"user_id":null}
Although the database is not null. Where do you think is my mistake? php file or ajax function? or both?
Thanks for helping :)
Edit : I've changed my code according to the remarks i had on the way i pass the variable AJAX->PHP. I've tested my sql query on my database and it works fine, but i still have the problem of null values after i pass the object from my php file to the succes function of the AJAX/JS file. Any ideas about what's wrong with my code?
Thanks again.
Upvotes: 0
Views: 284
Reputation:
You have two problems here.
First, you are not sending the mail parameter in your jQuery AJAX request. You need to append the GET parameter to the end of the URL under the url
key:
$.ajax({
method: 'GET',
url: "webservices/[email protected]",
timeout: kTimeout,
success: function(response) {
alert(response);
},
error: function() {
alert('error');
}
});
The second problem is that you have your $mail
variable assignment in your PHP script backwards. It should be
$mail = $_GET['mail'];
$_GET['mail']
is automatically set by PHP when you call the script with a GET request. But since you are referencing $mail
in your prepared SQL statement, you want to assign the value of $_GET['mail']
to $mail
.
Upvotes: 1