Results 1 to 7 of 7

Thread: Form Validation, window.location.href and onsubmit

  1. #1
    Join Date
    Aug 2008
    Location
    phx
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Form Validation, window.location.href and onsubmit

    Hi all,
    I'm fairly new to javascript and ive hit a road block.
    What im trying to accomplish.
    Submit a form to my php page and with the onsubmit validate my form fields and bounce the user to a 'thankyou.html'

    Here's my Javascript
    HTML Code:
     <script type="text/javascript">
    function validateForm() {
    return Spry.Widget.Form.validate(document.myform);
    }
    function redirect() {
     window.location.href = 'http://mydomain.com/thanks.html';
            }
    </script>
    And Here's my form:
    HTML Code:
    <form action="mypage.php" method="post" name="myform" onSubmit="return validateForm();redirect()">
    For some reason when i submit it takes me to mypage.php (which posts straight to a db) and not the thanks.html (where i would like it to go).

    Thanks in advance!
    Last edited by ump; 08-27-2008 at 11:56 PM.

  2. #2
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Could you please show us the involved markup (HTML).
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  3. #3
    Join Date
    Jul 2006
    Location
    Canada
    Posts
    2,581
    Thanks
    13
    Thanked 28 Times in 28 Posts

    Default

    For some reason when i submit it takes me to mypage.php (which posts straight to a db) and not the thanks.html (where i would like it to go).
    That's because your action is "mypage.php".

    Use a PHP redirect from "mypage.php"
    - Mike

  4. #4
    Join Date
    Aug 2008
    Location
    phx
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    That's the problem. I cant change or modify mypage.php. It resides somewhere else. i only used "mypage.php" as an example. I need to submit data to "mypage.php" and then redirect to a thank you page.

    Thanks for the help!

  5. #5
    Join Date
    Aug 2008
    Location
    phx
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Sorry for the delay here's my code. I decided to use dreamweaver's validation and i still dont know where or how to work the redirect. Thanks for the help!!
    Code:
    <script language="JavaScript" type="text/JavaScript">
    <!--
    function MM_findObj(n, d) { //v4.01
      var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
        d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
      if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
      for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
      if(!x && d.getElementById) x=d.getElementById(n); return x;
    }
    
    function MM_validateForm() { //v4.0
      var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
      for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
        if (val) { nm=val.name; if ((val=val.value)!="") {
          if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
            if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
          } else if (test!='R') { num = parseFloat(val);
            if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
            if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
              min=test.substring(8,p); max=test.substring(p+1);
              if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
        } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
      } if (errors) alert('The following error(s) occurred:\n'+errors);
      document.MM_returnValue = (errors == '');
    }
    function MM_goToURL() { //v3.0
      var i, args=MM_goToURL.arguments; document.MM_returnValue = false;
      for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
    }
    //-->
    </script>
    </head>
    
    <body>
    <form action="../vatt-appling.php" method="post" name="1430" id="1430" onSubmit="MM_validateForm('firstname','','R','lastname','','R','address','','R','city','','R','dayphone','','RisNum','email','','RisEmail');return document.MM_returnValue">
      <table width="50%" border="0" cellpadding="2">
        <tr> 
          <td colspan="2">logo </td>
        </tr>
        <tr align="left"> 
          <td colspan="2"><p class="style3"><span class="txtRequired">*</span> Indicates required 
              field</td>
        </tr>
    	<tr align="left">
          <td><span class="txtLabel">First Name: </span><span class="txtRequired">*</span></td>
    <td><input type="text"name="firstname" size="25">
          </td>
        </tr>
    <tr align="left">
          <td><span class="txtLabel">Last Name: </span><span class="txtRequired">*</span></td>
          <td> <span class="txtRequired">
            <input type="text"name="lastname" size="25">
            </span></td>
        </tr>
    <tr align="left">
          <td><span class="txtLabel">Street Address: </span><span class="txtRequired">*</span></td>
    <td><input type="text"name="address" size="35">
          </td>
        </tr>
    <tr align="left">
          <td><span class="txtLabel">City: </span><span class="txtRequired">*</span></td>
    <td><input type="text"name="city" size="25" maxlength="30">
          </td>
        </tr>
    <tr align="left">
          <td><span class="txtLabel">State: </span><span class="txtRequired">*</span></td>
    <td><input type="text" name="state">
          </td>
        </tr>
    <tr align="left">
          <td><span class="txtLabel">Zip Code: </span><span class="txtRequired">*</span></td>
    <td><input type="text" name="Zip">
          </td>
        </tr>
    <tr align="left"><td><span class="txtLabel">Country: </span></td>
    <td><input type="text"name="country" size="25">
    <tr align="left">
          <td><span class="txtLabel">Daytime Phone: </span><span class="txtRequired">*</span></td>
    <td><input type="text"name="dayphone" size="15">
            no dashes or spaces</td>
        </tr><tr align="left">
          <td><span class="txtLabel">Email Address: </span><span class="txtRequired">*</span></td>
    <td><input type="text"name="email" size="25">
          </td>
        </tr>
    <tr align="left">
          <td colspan="2">&nbsp;</td>
    </tr>
    <tr align="left"> 
          <td colspan="2"><input name="Submit" type="submit" value="Submit" />
    </td>
        </tr>
      </table>
    </form>

  6. #6
    Join Date
    Feb 2008
    Location
    Cebu City Philippines
    Posts
    1,160
    Thanks
    17
    Thanked 277 Times in 275 Posts

    Default

    Why make it so hard. You can make it work by choosing either of these three on mypage.php:
    • Use the meta tag:
      Code:
      <meta http-equiv="refresh" content="1;url=http://thisismyTHANKYOUpage.com">
    • Use JS:
      Code:
      <script type="text/javascript">
      window.onload=function(){location.href='http://thisismyTHANKYOUpage.com';}
      </script>
    • Use server-side (PHP) :
      PHP Code:
      <?php
      header
      ("Location: http://www.thisismyTHANKYOUpage.com/"); /* Redirect browser */
      ?>


    Hope that helps.
    Learn how to code at 02geek

    The more you learn, the more you'll realize there's much more to learn
    Ray.ph!

  7. #7
    Join Date
    Aug 2008
    Location
    phx
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    That's where my problem lies. I cannot access "mypage.php". It's on an external site that i cannot edit. I simply used "mypage.php" as a placeholder.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •