1

I'm building a login form and want to instantly check to make sure the email address is one that's in my database.

I've looked around and tried a bunch of different things to make this work, but I can't seem to get this working correctly.

Here's the Form

<form>
<input type="text" id="email" name="email" placeholder="Email Address" onblur="checkEmail(this.value);" onchange="checkEmail(this.value);"/>
<input type="password" id="password" name="password" placeholder="Password"/>
<input type="button" id="login-signup" value="Login / Sign Up"/>
</form>

Here's the Javascript. This is where I think the problem is.

function checkEmail(email) {
    // check to see if the email exists in the database using PHP and MySQL
    $.ajax({                                      
        url: 'login.php',               //the script to call to get data 
        type: 'post',  
        data: {
            email: $('#email').val()
        }, 
        dataType: 'json',               //data format      
        success: function(response) {       //on reception of reply
            if(response == 'match') {
                console.log('match');
            } else if (response == 'no match') {
                console.log('no match');
            } else if (response == 'error') {
                console.log('error');
            } else {
                console.log('who knows');
            }
        } 
    });

}

And here's login.php If I navigate to mywebsite.com/login.php?email=email then everything works correctly, so I know this is doing what I need it to. I imagine the problem is in my ajax

$db_host = "host";
$db_user = "user";
$db_pass = "pass";
$db_name = "name";


$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name, $db_user, $db_pass);


if(isset($_GET['email'])) {
    $email = $_GET['email'];
    echo $email;
}

// Using prepared statements almost eliminates the possibility of SQL Injection.
$preparedQuery = $db->prepare("SELECT * FROM `members` WHERE `email` = :email");
$preparedQuery->bindValue(":email", $email);
$preparedQuery->execute();

// Retrieve the results from the database
$user = $preparedQuery->fetch(PDO::FETCH_ASSOC);

// If there is a user record print the user & pass... 
if($user != ''){
    echo 'match';
    $_SESSION['email'] = $email;
} else if ($user == '') {
    echo 'no match';
} else {
    echo 'error';
}
11
  • 1
    How are you calling your checkEmail() function? Are you getting errors? Have you watched the request / response in the browser's console? Commented Mar 23, 2015 at 19:50
  • 2
    change the $_GET to $_POST Commented Mar 23, 2015 at 19:50
  • 1
    You're also specifying a datatype of JSON and you're not returning JSON. Commented Mar 23, 2015 at 19:54
  • 1
    Remove "dataType: 'json'," from your javascript Commented Mar 23, 2015 at 19:56
  • 1
    Also, @Jay Blanchard, changing $_GET to $_POST was correct. Thank you guys! Commented Mar 23, 2015 at 20:01

2 Answers 2

2

@user4035 suggested removing the data type from the AJAX request, and that got the request to return results. @Banik suggested changing $_GET to $_POST and that worked as well. Now the entire thing works perfectly

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

1 Comment

FIFY @Banik the only suggestion I made was changing the actaul data because it was already being passed in.
1

If you're trying to send a json object, you'll need to read the raw data.

$request_body = file_get_contents('php://input');
$email = json_decode($request_body, true);

Then you'll have the email data in an array. You could also remove the datatype in the ajax call and read the data in $_POST['email']. You're sending in post data and trying to access it in $_GET

1 Comment

The OP isn't sneding a JSON object, just a value.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.