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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Help with Form Validation using Javascript & PHP

    I'm trying to get this form to validate - everything is working except for the validation for the CAPTCHA code. When I include that, the form doesn't validate anything and just sends the e-mail. It's probably something easy, as I'm a beginner at this, but I'd really appreciate any help you can give me.

    Thanks!

    The code is:

    HTML/Javascript/AJAX:
    Code:
    <style>
    <!--
    .style1 {
    	font-family: Arial, Helvetica, sans-serif;
    	font-weight: bold;
    }
    .style2 {
    	font-family: Arial, Helvetica, sans-serif;
    	font-style: italic;
    	font-weight: bold;
    }
    body {
    	background-color: #B79E6C;
    }
    .style3 {
    	font-family: Arial, Helvetica, sans-serif;
    	font-style: italic;
    }
    .error {
    	font-family: Arial, Helvetica, sans-serif;
    	color: #FF0000;
    	display: none;
    }
    -->
    </style>
    
    <form onSubmit="return checkForm();" action="http://website.com/FormToEmail.php" method="post">
    <input type="hidden" name="recipient" value="email@domain.com" />
    
    <script>
    function checkCAPTCHA() {
    
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      alert ("Your browser does not support AJAX - please upgrade your browser to send an e-mail through this page.");
      return;
      } 
    
    code = document.getElementById("code").value;
    var url="http://www.freedom-academy.com/checkcaptcha.asp";
    url=url+"?code2="+code;
    url=url+"&sid="+Math.random();
    
    xmlHttp.open("GET",url,true);
    xmlHttp.send(null);
    
    xmlHttp.onreadystatechange=function()
    	{
    	if(xmlHttp.readyState==4)
      		{
      		document.getElementByID("code2").value=xmlHttp.responseText;
      		}
    	}
    }
    
    function checkForm() {
    name = document.getElementById("name").value;
    email = document.getElementById("email").value;
    comments = document.getElementById("comments").value;
    code = document.getElementById("code").value;
    code2 = checkCAPTCHA();
    
    if (name == "") {
    hideAllErrors();
    document.getElementById("nameError").style.display = "block";
    document.getElementById("name").select();
    document.getElementById("name").focus();
    return false;
    } else if (email == "") {
    hideAllErrors();
    document.getElementById("emailError").style.display = "block";
    document.getElementById("email").select();
    document.getElementById("email").focus();
    return false;
    } else if (email.indexOf("@") < 1 || email.indexOf(".") < 1) {
    hideAllErrors();
    document.getElementById("emailError2").style.display = "block";
    document.getElementById("email").select();
    document.getElementById("email").focus();
    return false;
    } else if (comments == "") {
    hideAllErrors();
    document.getElementById("commentsError").style.display = "block";
    document.getElementById("comments").select();
    document.getElementById("comments").focus();
    return false;
    } else if (code == "") {
    hideAllErrors();
    document.getElementById("codeError").style.display = "block";
    document.getElementById("code").select();
    document.getElementById("code").focus();
    return false;
    } else if (code2 == "incorrect") {
    hideAllErrors();
    document.getElementById("code2Error").style.display = "block";
    document.getElementById("code2").select();
    document.getElementById("code2").focus();
    return false;
    }
    return true;
    }
     
    function hideAllErrors() {
    document.getElementById("nameError").style.display = "none"
    document.getElementById("emailError").style.display = "none"
    document.getElementById("emailError2").style.display = "none"
    document.getElementById("commentsError").style.display = "none"
    document.getElementById("codeError").style.display = "none"
    document.getElementById("code2Error").style.display = "none"
    }
    
    
    </script>
    
    
    <table border="0" cellspacing="5" width="600">
    <tr><td><span class="style1">Name*:</span></td>
    <td><input type="text" name="name" size="43" maxlength="60" style="width:350px; border:1px solid #999999" /><div class=error id=nameError>Please enter your name<br /></div></td></tr>
    <tr><td><span class="style1">Email address*:</span></td>
    <td><input type="text" name="email" size="43" maxlength="60" style="width:350px; border:1px solid #999999" /><div class=error id=emailError>Please enter your email address<br /></div>
    <div class=error id=emailError2>Please enter a valid email address<br /></div></td></tr>
    <tr><td><span class="style1">Phone number:</span></td>
    <td><input type="text" name="phone" size="43" maxlength="43" style="width:350px; border:1px solid #999999" /></td></tr>
    <tr><td valign="top"><span class="style1">Comments and questions*:</span></td>
    <td><textarea name="Comments" id="Comments" rows="7" cols="40" style="width:350px; border:1px solid #999999"></textarea><div class=error id=commentsError>Please enter your comments<br /></div></td></tr>
    
    <tr><td colspan="2" align="center"> <br />
    <table border="0" cellpadding="0" cellspacing="0">
    <tr><td> <span class="style2">Enter web form code*:</span>
    <input id="code" name="code" type="text" style="width:80px; border:1px solid #999999" maxlength="6" /><div class=error id=codeError>Please enter the code from the box<br />
    </div>
    <div class=error id=code2Error>The code is not correct - please try again<br /></div>
    </td>
    <td><img id="captcha" alt="Web Form Code" style="margin-left:20px; border:1px solid #999999"
    src="http://website.com/securimage/securimage_show.php" /></td></tr></table>
    
    <tr><td>&nbsp;</td><td align="center"><font face="Arial, Helvetica, sans-serif"><a href="#" onclick="document.getElementById('captcha').src = 'http://website.com/securimage/securimage_show.php?' + Math.random(); return false">Load New Image</a></font>
    </td></tr>
    <br />
    </td></tr>
    <tr><td>&nbsp;</td><td><input type="submit" value="Send"><font face="arial" size="1"></font></td></tr>
    <tr><td><i>*required fields</i></td></tr>
    </table>
    </form>
    PHP Code:
    PHP Code:
    <?php

    header
    ("Cache-Control: no-cache, must-revalidate");
     
    // Date in the past

    header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

    //get the code entered from URL
    $webcode=$_GET["code2"];

    include_once 
    'securimage/securimage.php';

    $securimage = new Securimage();

    if (
    $securimage->check('$webcode') == false) {

      
    $response="incorrect";

    }

    else {

    $response="correct";

    }

    //output the response

    echo $response;

    ?>

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,849
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    if ($securimage->check('$webcode') == false) {

    $response="incorrect";

    }
    You'd need to use double-quotes to wrap a PHP variable, to parse it correctly. (Or just remove the quotes, if there is nothing else along with it)
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks so much. But I'm so sorry to report that didn't fix it. I'm still getting the same problem. I think it's something in the Javascript since none of the errors are processing. Any ideas?

  • #4
    New to the CF scene
    Join Date
    Aug 2008
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Kate,

    Thanks for that explanation. In the code above I'm trying to use a mix of Javascript and server-side coding, but it's not working. Any ideas what could be causing it? I'm thinking it's somewhere in the Javascript, maybe how I'm calling the function checkCAPTCHA to set the result of the server side process equal to a variable. Or it's in the function checkCAPTCHA. When I comment out those lines, everything works fine.

    Thanks!


  •  

    Posting Permissions

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