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 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    31
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Validate THEN submit form - problem

    Hey,

    I have a small problem, bear with me if this is ridiculously simple I just can't seem to spot the problem.

    I am trying to get a form to check for validation and then send it on if it is ok and if not, to stay on the form with the page so that the user can fill out the missing parts.

    Right now, it alerts the user of the missing fields but continues to send the form further.

    This is my javascript:
    Code:
    <script language="JavaScript" type="text/JavaScript">
    
      function checkForm(thisform)
      {
        if(thisform.name.value=='')
        {
          alert("Name field is required. Please fill it in.");
          return false;
        }
    	
    	if(thisform.email.value=='')
        {
          alert("Email field is required. Please fill it in.");
          return false;
        }
        
             if(thisform.number.value=='')
        {
          alert("Phone Number field is required. Please fill it in.");
          return false;
        }
    	    if(thisform.message.value=='')
        {
          alert("Message field is required. Please fill it in.");
          return false;
        }
    
        //if all is OK submit the form
    
        thisform.submit();
      }
      </script>
    This is my php send form
    Code:
    <?php
      $name = $_REQUEST['name'] ;
       $email = $_REQUEST['email'] ;
          $number = $_REQUEST['number'] ;
    	   $message = $_REQUEST['message'] ;
    
    
      mail("my@emailaddress.com", "trial",
        "Sender: $name , Netfang: $email, S&#237;mi: $simi, Texti: $message");
    header( "Location: http://www.mydomain.com" );
    ?>
    and finally this is my form
    Code:
    <form name="contact_form" action="sendemail.php" method="post">
          <table border="0" cellspacing="5">
                  <tr>
                    <td >Nafn</td>
                    <td><input type="text" size="30" name="name"></td>
                  </tr>
               
                  <tr>
                    <td>Netfang</td>
                    <td><input type="text" size="30" name="email"></td>
                  </tr>
                  
                  <td >S&iacute;mi</td>
              <td><input type="text" size="30" name="number"></td>
                  </tr>
                  <tr>
                    <td valign="top"></td>
                    <td><textarea name="message" rows="6" cols="30"></textarea></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input name="submit" type="submit" value="Senda" onclick="checkForm(this.form)" />
                  </td>
    </td>
                  </tr>
                </table>
              </form>
    Can anyone figure out what I am doing wrong?

    Best,
    Figurine

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,862
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Try
    Code:
    <form name="contact_form" action="sendemail.php" method="post" onsubmit="return checkForm(this);">
          <table border="0" cellspacing="5">
                  <tr>
                    <td >Nafn</td>
                    <td><input type="text" size="30" name="name"></td>
                  </tr>
               
                  <tr>
                    <td>Netfang</td>
                    <td><input type="text" size="30" name="email"></td>
                  </tr>
                  
                  <td >S&iacute;mi</td>
              <td><input type="text" size="30" name="number"></td>
                  </tr>
                  <tr>
                    <td valign="top"></td>
                    <td><textarea name="message" rows="6" cols="30"></textarea></td>
                  </tr>
                  <tr>
                    <td>&nbsp;</td>
                    <td><input type="submit" value="Senda"  />
                  </td>
    </td>
                  </tr>
                </table>
              </form>
    PS: Don't use tables to format a form, it's not semantic, see http://www.alistapart.com/articles/p...ccessibleforms
    Last edited by abduraooft; 08-05-2008 at 04:51 PM.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • Users who have thanked abduraooft for this post:

    Figurine (08-05-2008)

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    31
    Thanks
    19
    Thanked 0 Times in 0 Posts

    THanks!!

    Thanks! It works

    REALLY appreciate all the help! And thanks for the link. I'll drop the table.

    Best,
    Figurine


  •  

    Posting Permissions

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