SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    form validation not working

    Hi,

    Hoping someone might be able to help me fix up my code...

    My contact form has been validating the name, email and comments box successfully using javascript until I added a captcha code to my contact form today (copy and pasting the captcha code from another page of my site booknow.php). Now the form won't validate and it just posts the form regardless of what values are inputted into the form. Thanks.

    HTML
    Code:
    <form method="post"  onSubmit="return validateForm()" action="sendContact.php" id="contact" name="contact">
    				<fieldset>
    					<label for="name">Name:</label>
    					<input type="text" value="Name" id="name" name="txtName" <?php if(!empty($errors)){ echo "value='".($name)."'"; } ?> />
    					<label for="email">Email address:</label>
    					<input type="text" value="Email address" id="email" name="txtEmail" <?php if(!empty($errors)){ echo "value='".($email)."'"; } ?> />
    					<label for="enquiry">Your enquiry:</label>
    					<textarea id="enquiry" value="Your enquiry" name="enquiry" />Your enquiry</textarea>
    
    		<img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br><br>
    
    		<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small><br><br>
    <label for="input">Captcha code:</label>
    		<input type="text" size="32" id="input" name="txtInput" <?php if(!empty($errors)){ echo "value='".($input)."'"; } ?> /><br><br>		
    
    					<input type="image" alt="Submit" name="submit" id="submit" src="images/submit.jpg" />
    				</fieldset>
    			</form>
    Javascript

    Code:
        	<script type="text/javascript">
    	
    	function validateForm()
    
    	{
    
    		var errorMessage="";
    
    		var name=document.forms["contact"]["txtName"].value;
    
    		var email=document.forms["contact"]["txtEmail"].value;		
    
    		var input=document.forms["contact"]["txtInput"].value;
    
    		var atpos=email.indexOf("@");
    
    		var dotpos=email.lastIndexOf(".");
    
    		if (name==null || name=="")
    
    		{
    
    			errorMessage = "Your Name is required.\n";
    
    		}
    
    		
    		if (email==null || email=="")
    
    		{
    
    			errorMessage += "Email address is required.\n";
    
    		}
    
    		else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
    
    		{
    
    			errorMessage += "Please enter a valid email address.\n";
    
    		}
    		
    
    		if (input==null || input=="")
    
    		{
    
    			errorMessage += "Captcha code is required.\n";
    
    		}
    
    		
    function refreshCaptcha()
    
    	{
    
    		var img = document.images['captchaimg'];
    
    		img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
    
    	}
    	</script>
        
        <?php 
        
        if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['txtInput']) != 0)
    
    	{
    
    	//Note: the captcha code is compared case insensitively.
    
    	//if you want case sensitive match, update the check above to
    
    	// strcmp()
    
    		$errors .= "The captcha code does not match";
    
    	}
        ?>
    SendContact.php
    Code:
    <?php
    if ($_SERVER['REQUEST_METHOD']=='POST') {
    $message = "Hi,
    
    You have a new contact request.
    
    From : ".$_POST['txtEmail']."
    Name : ".$_POST['txtName']."
    Message : ".$_POST['enquiry']."
    
    
    
    Regards,
    Daniela
    Ready 2 Rock";
    
    $headers = 'From: webmaster@ready2rock.com.au' . "\r\n" .
        'Reply-To: webmaster@ready2rock.com.au' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();
    
    
        mail('daniela@ready2rock.com.au','Ready 2 Rock Contact Form',$message,$headers);
    }
    header('location:thankyou.html');
    
    ?>

  2. #2
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi,

    You're missing a curly brace to shut the validateForm function.
    Does inserting that help?

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've added the curley brace and when I did, dreamweaver said "no syntax errors", I uploaded the new file to the server to test it, but it hasn't fixed the problem.

  4. #4
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Shame.
    What is it you are trying to do, exactly?
    Are you trying to revert the form back to its original state, or are you trying to get it working with a captcha?

  5. #5
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Hi again,

    I just had a look at the JavaScript (probably should have done that earlier ), anyways, you're not doing anything with the errorMessage variable, so the form will always submit.

    You're missing something like:
    Code JavaScript:
    if (errorMessage !=""){
      alert(errorMessage);
      return false;
    }

    at the end of the validateForm function.

  6. #6
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not sure I get what you mean. Something like the below...?

    Code:
    	function validateForm()
    
    	{
    
    		var errorMessage="";
    
    		var name=document.forms["contact"]["txtName"].value;
    
    		var email=document.forms["contact"]["txtEmail"].value;		
    
    		var input=document.forms["contact"]["txtInput"].value;
    
    		var atpos=email.indexOf("@");
    
    		var dotpos=email.lastIndexOf(".");
    
    		if (name==null || name=="")
    
    		{
    
    			errorMessage = "Your Name is required.\n";
    
    		}
    
    		
    		if (email==null || email=="")
    
    		{
    
    			errorMessage += "Email address is required.\n";
    
    		}
    
    		else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length)
    
    		{
    
    			errorMessage += "Please enter a valid email address.\n";
    
    		}
    		
    
    		if (input==null || input=="")
    
    		{
    
    			errorMessage += "Captcha code is required.\n";
    
    	}
    	
    if (errorMessage !=""){
    			errorMessage += "errorMessage".\n";
      return false;
    }
    
    	}

  7. #7
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Exactly, just copy and paste this:

    Code JavaScript:
    function validateForm(){
      var errorMessage="";
      var name=document.forms["contact"]["txtName"].value;
      var email=document.forms["contact"]["txtEmail"].value;    
      var input=document.forms["contact"]["txtInput"].value;
      var atpos=email.indexOf("@");
      var dotpos=email.lastIndexOf(".");
      if (name==null || name==""){
        errorMessage = "Your Name is required.\n";
      }
     
      if (email==null || email==""){
        errorMessage += "Email address is required.\n";
      } else if (atpos<1 || dotpos<atpos+2 || dotpos+2>=email.length){
        errorMessage += "Please enter a valid email address.\n";
      }
     
      if (input==null || input==""){
      errorMessage += "Captcha code is required.\n";
      }
     
      if (errorMessage !=""){
        alert(errorMessage);
        return false;
      }
    }

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Excellent, that worked, thanks very much.

    One other thing remains to fix, the catchpa code submits even if the wrong code is submitted, I have copy and pasted the following PHP code form the original page on which I have the catchpa code into the contact page head section , but it's not working, any chance you would be able to help with this, or is it better to post this in another section of the forums?

    PHP
    Code:
            <?php 
        
        if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['txtInput']) != 0)
    
    	{
    
    	//Note: the captcha code is compared case insensitively.
    
    	//if you want case sensitive match, update the check above to
    
    	// strcmp()
    
    		$errors .= "The captcha code does not match";
    
    	}
        ?>

  9. #9
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...could the same kind of function perhaps be done with Javascript?

  10. #10
    Grüße aus'm Pott gold trophysilver trophybronze trophy
    Pullo's Avatar
    Join Date
    Jun 2007
    Location
    Germany
    Posts
    6,096
    Mentioned
    219 Post(s)
    Tagged
    12 Thread(s)
    Quote Originally Posted by daniela View Post
    ...could the same kind of function perhaps be done with Javascript?
    I'm always wary of form validation that relies solely on JavaScript, as you can get around it simply by turning JavaScript off.

    Regarding the captcha code, this is being processed server side, right?
    It's probably better to start a new thread in the PHP forum about this.

  11. #11
    SitePoint Enthusiast
    Join Date
    May 2003
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok no worries, thanks again for your help.


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
  •