Reputation: 149
I am trying to redirect to Homepage from SignIn Page after the validation of user credentials is done. The response is working perfectly fine. On getting a successful response, I want to redirect to Homepage. I am using Javascript for client side and PHP on server side.
I have tried all the possible solutions provided elsewhere on this site.
Here is the script for redirection on SignIn page:
$(document).ready(function(){ $("#SignInForm").submit(function(){
var em = document.getElementById("Email").value; var ps = document.getElementById("Password").value; var obj; $.ajax({ url: 'WebService/SignIn.php', type: 'post', async: false, data: { Email: em, Password: ps }, dataType: 'json', success: function(data) { var str = JSON.stringify(data); obj = JSON.parse(str); if(obj.status == 1) { window.location="Homepage.html"; } else { alert("Invalid Username/Password") } }, error: function(xhr){ //alert("An error occured: " + xhr.status + " " + xhr.statusText ); alert("An error occured. Please Try Again"); } }) }); }); </script>
A simple
window.location="Homepage.html";
should be suffice. But it isn't working.
Any help is appreciated.
Upvotes: 2
Views: 859
Reputation: 7446
The problem is related to the submit action:
By default, in fact, the submit action redirects to the url provided in the action (form parameter): https://developer.mozilla.org/en/docs/Web/API/HTMLFormElement/submit
Hence, in order to be able to perform an AJAX request, you have to first prevent the default action performed by the submit.
Just replace:
$("#SignInForm").submit(function(){
var em = document.getElementById("Email").value;
With:
$("#SignInForm").submit(function(e){ // <-- note the e
e.preventDefault();
var em = document.getElementById("Email").value;
Live example of what happens WITHOUT preventing the default action:
Live example of what happens by PREVENTING the default action:
http://jsfiddle.net/hhjqqeza/1/
Upvotes: 0