I have a form and I validate the fields in javascript functions. After the validation, I want to redirect to another page. I am trying this for the form:
<form action="" method="post" name="form" onsubmit="return validate()">
User Name : <input type="text" name="realname" size="19"><span id="realnameerror" ></span>
<br>
E-Mail : <input type="text" name="email" size="25"><span id="emailerror" ></span>
<br>
PhoneNo : <input type="phoneno" name="phoneno" maxlength="10" size="25"><span id="phonenoerror" ></span>
<br>
<input type="submit" value="Submit">
</form>
And this is the code for validation:
<script type="text/javascript">
var hasFocus = false;
function checkName(form) /* for name validation */
{...}
function checkEmail(form) /* for email validation */
{...}
function validPhone(form) /* for phone validation */
{...}
function validate()
{
hasFocus = false;
var form = document.forms['form'];
var ary=[checkName,checkEmail,validPhone];
var rtn=true;
var z0=0;
for (var z0=0;z0<ary.length;z0++)
{
if (!ary[z0](form))
{
rtn=false;
}
}
if (rtn)
{
window.location="http://test.dev";
return rtn;
}
else return rtn;
}
</script>
The point is that all the javascript functions are working correctly, I get error messages if there are any, but it just doesn't make my redirect. The weird thing is that if I put the redirect into another script, and don't make the validation, it works. I don't know what am I doing wrong. I have tried to put my redirect into another function and just call it like this:
if (rtn) { Redirect(); }
but it still doesn't work. Instead of window.location I also tried window.location.href and document.location.href. I really think there something that I'm missing inside the script... If you notice something, please let me figure it out. Thank you!
I HAVE TRIED TO PUT AN ALERT INSIDE MY IF STATEMENT AND IT APPEARS FOR 2 SECONDS AND THEN IT MAKES THE REDIRECT. IF I DON'T PUT THAT ALERT, MY REDIRECT DOESN'T WORK. HERE IS THE CODE:
if (rtn) {
window.location="http://test.dev";
alert(rtn);
}
alert something inside this code
if (rtn)
{
alert('you here');
window.location="http://test.dev";
return rtn;
}
if alert come out, you got here. If not, your first condition is wrong. That we split out where to have problem.
if (rtn)
{
window.location="http://test.dev";
return rtn;
}
else return rtn;
Replace the above with
return rtn;
The form does not submit if we return false and submits otherwise.
Also, the form will submit to "action", so make sure your "action" property of form is set to "http://test.dev"
Set form tag's action
to be http://test.dev
.That's all.
Edit: You never think about the form data may not be posted to http://test.dev
if you used window.location.href
?
How doing this?
<form action="http://test.dev" method="post" name="form" onsubmit="return validate()">
<script type="text/javascript">
var hasFocus = false;
function checkName(form) /* for name validation */
{...}
function checkEmail(form) /* for email validation */
{...}
function validPhone(form) /* for phone validation */
{...}
function validate()
{
hasFocus = false;
var form = document.forms['form'];
var ary=[checkName,checkEmail,validPhone];
var rtn=true;
var z0=0;
for (var z0=0;z0<ary.length;z0++)
{
if (!ary[z0](form))
{
rtn=false;
}
}
if (rtn)
{
return rtn;
}
else return rtn;
}
</script>
Try jquery ajax to redirect true statement $.ajax({url: 'search.php',data: "check_qc=" + qc,async:false, success: function(response) {if(response==1){window.location="http://google.com"; return false;}}});
if (rtn)
{
alert('hello there');
window.location="http://new/new.php";
return true;
}else{
return false;
}
when true condition first then page redirect to given url and for condition second not redirect.