Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Dec 2003
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts

    contact form with validation

    have a contact form with validation wanting to use drop down box to pass the info to different php files.. something isn't right.

    Code:
    <html>
    <head>
    <title>Contact us at Casair</title>
    <SCRIPT LANGUAGE="JavaScript">
    <!-- Begin
    function formHandler(f){
       if (f.site.selectedIndex>0) {
         f.action=f.site.options[f.site.selectedIndex].value;
         f.submit();
      }
    }
    // End -->
    </SCRIPT>
    <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 == '');
    }
    //-->
    </SCRIPT>
    </head>
    <body bgcolor="#000000">
    <form action="" method="post" name="form" onSubmit="MM_validateForm('name','','R','address','','R','state','','R','zip','','RisNum','phone','','RisNum','email','','RisEmail');return document.MM_returnValue">
    <table width="200" border="0">
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Full
          Name</font></td>
        <td><fieldset>
          <input name="name" type="text" id="name" size="35">
        </fieldset></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Company</font></td>
        <td><input name="company" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Address</font></td>
        <td><input name="address" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">State</font></td>
        <td><input name="state" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Zip</font></td>
        <td><input name="zip" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Phone</font></td>
        <td><input name="phone" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Fax</font></td>
        <td><input name="fax" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Email</font></td>
        <td><input name="email" type="text" size="35"></td>
      </tr>
      <tr>
        <td><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Department</font></td>
        <td><select name="site" ID=Select2>
            <option value="http://www.4dn.com/casair/html/sales.php">Sales
            <option value="support.php">Support </select></td>
      </tr>
      <tr>
        <td valign="top"><font color="#FFFFFF" size="2" face="Verdana, Arial, Helvetica, sans-serif">Comments</font></td>
        <td><textarea name="textarea" cols="26" rows="8"></textarea></td>
      </tr>
      <tr>
        <td><input type="button" value="Submit" onclick="formHandler(this.form)"></td>
        <td><div align="right">
            <input type="button" value="Close" onClick="window.close()">
          </div></td>
      </tr>
    </table>
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Jun 2002
    Location
    Round Rock, Texas
    Posts
    443
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I didn't study your code, or try to run it; but boolean operators are always suspect. Forgive me if I'm preaching to the choir.

    Things to keep in mind:
    Javascript defines as true anything that is not 0(zero), ""(empty string), null, undefined, false.

    && - "shortcut" AND operator. If the LEFT side evaluates to 'false' then the right side is never evaluated (executed!), because the expression can't be true.

    || - "shortcut" OR operator. If the LEFT side evaluates to 'true', the expression is true, so it doesn't matter what the right side is, thus the right side is never evaluated.

    Logical operator precidence is:
    ! && ||

    further, ! is a unary operator.

    thus:
    ! x && y
    will evaluate like this:
    (!x) && y

    if
    x = true; y=false;
    then
    !x && y is false
    !(x && y) is true

  • #3
    New Coder
    Join Date
    Dec 2003
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    to be honest i have no idea what you just said lol i have a feeling the form isn't working because the submit button is pointing to a javascript thus the validation won't run because theres no form submit, but this doesn't explain why the redirect to the php files isn't working.

  • #4
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,043
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Calling formReference.submit() doesn't trigger the onload event handler. Don't ask me why, I don't know.

    So you must call the onsubmit explicitly.
    Code:
    function formHandler(f){
       if (f.site.selectedIndex>0) {
         f.action=f.site.options[f.site.selectedIndex].value;
         f.onsubmit();
         if (!document.MM_returnValue) return false;
         f.submit();
      }
    }

  • #5
    New Coder
    Join Date
    Dec 2003
    Posts
    22
    Thanks
    0
    Thanked 0 Times in 0 Posts
    its ok. it was too complicated, so i've turned to a php solution.

    thanks guys.


  •  

    Posting Permissions

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