samedi 21 mars 2015

How to display error if email doesn't exist in DB, otherwise redirect user to new page?

I'm 95% done, just need a little help! My site has 2 simple forms, 1 for registration and 1 for login. I'm only concerned with the login form. Specifically, I want the form to display an error within it IF the email address that the user tries to login with, is NOT REGISTERED. If the email address IS registered, it should redirect them (grant access) to private.php.


enter image description here


Currently when a user submits an email address, the form calls on login.php to determine whether the email is registered or not. Login.php echoes "redirect" if the email exists in my database, otherwise it echoes "That email address is not registered". The problem is that the user should never be redirected to login.php, they should be redirected to private.php ONLY IF that email is registered. If it's not, there is no redirect, and the error message will be displayed within the login form.


You can see my ajax code has window.location = "http://ift.tt/1DIQsPZ"; but it keeps redirecting me to login.php whether registered or not. How do I adapt the code so that it does what I need? Thanks in advance.


ajax code:



<script>
$(document).ready(function() {
$("#loginform").submit(function() {
$.post('login/login.php', {email: $('#email').val(), loginsubmit: 'yes'}, function(data)
{
if (formResponse === "redirect")
{
window.location = "http://ift.tt/1DIQsPZ";
}

else {
$("#formResponse").html(data).fadeIn('100');
$('#email').val(''); /* Clear the inputs */
}
, 'text');
return false;
}

});
});
</script>


login.php code:



<?php
$emailaddress = $_POST["email"];
?>
<?php

// First we execute our common code to connection to the database and start the session
require("common.php");

// This if statement checks to determine whether the login form has been submitted
// If it has, then the login code is run, otherwise the form is displayed
if(!empty($_POST))
{
// This query retrieves the user's information from the database using
// their email.
$query = "
SELECT
email
FROM users
WHERE
email = :email
";

// The parameter values
$query_params = array(
':email' => $emailaddress
);
try
{
// Execute the query against the database
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch(PDOException $ex)
{
die("Failed to run query");
}
// This variable tells us whether the user has successfully logged in or not.
// We initialize it to false, assuming they have not.
// If we determine that they have entered the right details, then we switch it to true.
$login_ok = false;

// Retrieve the user data from the database. If $row is false, then the email
// they entered is not registered.

$row = $stmt->fetch();
if($row) {
$login_ok = true;
}
// If the user logged in successfully, then we send them to the private members-only page
// Otherwise, we display a login failed message and show the login form again
if($login_ok)
{

// This stores the user's data into the session at the index 'user'.
// We will check this index on the private members-only page to determine whether
// or not the user is logged in. We can also use it to retrieve
// the user's details.
$_SESSION['user'] = $row;

// Redirect the user to the private members-only page.
echo 'redirect';
}
else
{
// Tell the user they failed
echo 'That email address is not registered';
}
}

?>


Login form (if needed):



<aside class="lightbox">
<input type="checkbox" class="state" id="lightbox-two" />
<article class="content">
<a href="#" class="closest"><img src="img/x.png" class="btn_close" title="Close Window" id="closer2" alt="Close Contact Window" /></a>
<form id="loginform" name="loginform" method="POST" class="login" action="login/login.php">
<h1>Welcome Back!</h1>
<input name="email" id="email" type="email" class="feedback-input" placeholder="My Email" required pattern="[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)" required title="Whoops! Invalid email"/>
<div id="formResponse" style="display: none;"></div>
<button type="submit" name="loginsubmit" class="loginbutton">Login</button>

</form>

</article>
<label class="backdrop" for="lightbox-two"></label>
</aside>

Aucun commentaire:

Enregistrer un commentaire