0

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);
   }
4
  • #1 Learn indentation; #2 Console notices errors much faster and easier than we do, especially with semi-readable code Commented Jun 21, 2013 at 9:21
  • if (rtn) { //window.location="test.dev"; //return rtn; } Commented Jun 21, 2013 at 9:24
  • one thing you do it var url = "test.deve"; then alert(url). Copy url in alert box. Paste into your browser. Then you will know it work or not. Commented Jun 21, 2013 at 9:46
  • Use chrome developer tools to debug it, with breakpoints it will be much easier to know if you're really executing the window.location Commented Jun 21, 2013 at 10:20

6 Answers 6

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

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

Comments

0

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.

1 Comment

I've put an alert there and it shows me the alert for about 2 seconds and then it redirects me to test.dev. If I don't put the alert there, it doesn't make the redirect! I don't understand
0
 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"

Comments

0

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?

Comments

0

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>

Comments

0

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;}}});

Comments

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.