SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    2 forms on same page

    I'm trying to create a page with Admins and Users. For the log in page, it will have two log in forms. One for Admins and one for regular users.

    Is it possible to create two forms on one page, and then when the user clicks "Login", it only validates that one form they put in data with? i'm trying out a validation script right now and every time I submit the form, it tells me the username and password is missing from my other form.
    ~ Nate L ~

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, that's no problem.
    Code HTML4Strict:
    <form action="admin.php" method="post" id="admin">
      ...
    </form>
     
    <form action="user.php" method="post" id="user">
      ...
    </form>

    Add an event listener for the submit event for each form and make sure it checks only the fields in that particular form.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Jan 2008
    Location
    Shaw AFB
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here's the script i'm using...

    HTML Code:
    <form ACTION="<?php echo $loginFormAction; ?>" id="form1" name="form1" method="POST">
      <label></label>
      <table width="241" border="0" align="center">
        <tr>
          <td colspan="2"><h2 class="style4">Login</h2></td>
        </tr>
        <tr>
          <td width="79"><span class="style3"><strong>
            
          </strong>        
            
          </span>        <span class="style2">
         
          </span>      <div align="right" class="style3"><strong>Username:</strong></div>      </td>
          <td width="152"><input name="username" type="text" id="username" tabindex="1" maxlength="20" /></td>
        </tr>
        <tr>
          <td><span class="style3"><strong>
           
          </strong>        
            
          </span>        <span class="style2">
          
          </span>     <div align="right" class="style3"><strong>Password:</strong></div>      </td>
          <td><input name="password" type="password" id="password" tabindex="2" maxlength="20" /></td>
        </tr>
    
        <tr>
          <td>&nbsp;</td>
          <td><input name="submit1" type="submit" id="submit1" tabindex="4" onclick="MM_validateForm('username','','R');MM_validateForm('password','','R');MM_validateForm('email','','RisEmail');return document.MM_returnValue" value="Login" /></td>
        </tr>
    
      </table>
      
    </form>
    and in the head of my page, I have this validation code:
    Code:
    <script type="text/javascript">
    <!--
    function MM_validateForm() { //v4.0
      if (document.getElementById){
        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=document.getElementById(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>
    I've tried re-naming everything and just can't get it to validate only the form I click "submit" on.

    Also note, there aren't two forms in the code I posted above. Only one because i'm having issues. I would like to just duplicate the code that I currently have for the second form
    ~ Nate L ~


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
  •