dimanche 22 mars 2015

How to redirect user on successful ajax call? Otherwise display error message in form

Thanks for reading - My login form calls on login.php to check whether the user has entered a registered email address or not. If the address is registered, it echoes back "redirect", if it is not registered, it echoes "Email not registered". My current code incorrectly causes the form to redirect to login.php upon submission, and if the email is registered it simply displays the text "redirect" otherwise it displays "Email not registered".


How do I:


Redirect the user to page private.php if the php has echoed "redirect" - otherwise how do I display "Email not registered" within the form, if it echoes "Email not registered"? My login form contains a div to display the error if necessary.


ajax:



<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>


PHP:



...
if($login_ok)
{
echo 'redirect';
}
else
{
echo 'That email address is not registered';
}
...


login form:



...
<input name="email" id="email" type="email" class="feedback-input" placeholder="My Email" required/>
<div id="formResponse" style="display: none;"></div>
<button type="submit" name="loginsubmit" class="loginbutton">Login</button>
...

Aucun commentaire:

Enregistrer un commentaire