0

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.

7
  • Is $mail set? If not, I guess, your SQL-query would be invalid. Commented Mar 19, 2016 at 18:46
  • method: 'GET', isn't it type:'GET' ? Commented Mar 19, 2016 at 18:48
  • could you alert JSON.stringify(response) and c Commented Mar 19, 2016 at 18:48
  • From the rest of your code, you should use $mail = $_GET['mail'];. You also need to pass mail to php in your jquery. Commented Mar 19, 2016 at 18:51
  • ahhh yes i didn't see it! ^^ Commented Mar 19, 2016 at 18:53

1 Answer 1

1

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.

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

1 Comment

I have changed what was wrong in my code, but there is still the same final problem : the values of the object replied by the ajax fonction are null. I think the problem is when i create the object in php, and after the object is passed to the ajax as a callback. Any idea about this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.