I'm trying to understand Ajax form validation...

Discussion in 'JavaScript' started by apollo911, Jun 7, 2010.

  1. #1
    The relevant code is below, the problem is that the 'Recaptch.reload' code is repeated when there is an error on the form, i.e. the first time the form is posted with an error the recaptcha,reload runs once, the next time it is posted with an error the reload happens twice and so on..


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Astrology, Horoscopes, Birthcharts, Zodiac, Life, Loves, Future, Compatibility Reports, Forecasts, Paros, Greece</title>
    <meta name="description" content="" />
    <meta name="keywords" content="" />
    <link href="basic.css" rel="stylesheet" type="text/css" />
    <link rel="stylesheet" type="text/css" href="chrometheme/chromestyle4.css" />
    
    <script type="text/javascript" src="drop_down.js"></script>
    <!-- JQuery -->
    <script type="text/javascript" src="jquery-1.2.6.min.js"></script>
    <script type="text/javascript" 
     src="http://api.recaptcha.net/js/recaptcha_ajax.js"></script>
    
     <script type="text/javascript">                                         
       // we will add our javascript code here           
       
    $(document).ready(function(){
    $("#ajax-contact-form").submit(function(){
    
    var str = $(this).serialize();
    
       $.ajax({
       type: "POST",
       url: "contact.php",
       data: str,
       success: function(msg){
        
    $("#note").ajaxComplete(function(event, request, settings){
    
    if(msg == 'OK') // Message Sent? Show the 'Thank You' message and hide the form
    {
    result = '<div class="notification_ok">Your message has been sent. Thank you!</div>';
    $("#fields").hide();
    }
    else
    {
    result = msg;
    Recaptcha.reload();
    }
    
    $(this).html(result);
    
    });
    
    }
    
     });
    
    return false;
    
    });
    
    });
    
     </script>  
    
    <link rel="stylesheet" type="text/css" href="style.css" />
    
    </HEAD>
    
     <BODY>
     <div id="headblock">
    <div id="header">
    <div id="datetime">
    <? putenv('TZ=Europe/London'); ?>
    <? echo date('l d F Y H:i') ; ?>
    </div>
    </div>
    </div>
    <div id="menublock">
    <?php require("menuvote.php"); ?>
    </div>
    <div id="wrapper">
    
    <div id="container">
    <div id="side-a">
    &nbsp;
    </div> 
    <div id="contentc">
    
    
    
    
    
    <fieldset class="info_fieldset"><legend>Contact</legend>
    
    <div id="note"></div>
    <div id="fields">
    
    <form id="ajax-contact-form" action="javascript:alert('success!');">
    <label>Name</label><INPUT class="textbox" type="text" name="name"  size="35" maxlength="50" value=""><br />
    
    <label>Email</label><INPUT class="textbox" type="text" name="email"  size="35" maxlength="50" value=""><br />
    
    <label>Confirm Email</label><INPUT class="textbox" type="text"  size="35" maxlength="50" name="emailc" value=""><br />
    
    <label>Subject</label><INPUT class="textbox" type="text" name="subject"  size="35" maxlength="50" value=""><br />
    
    <label>Comments</label><TEXTAREA class="textbox" NAME="message" ROWS="5" COLS="35"></TEXTAREA><br />
    
    <label></label><div id="security"><? require_once('recaptchalib.php');
    $publickey = "6LepZggAAAAAAEqFYQnRL1PeYG0udKFagv9pQ8J2"; // you got this from the signup page
    echo recaptcha_get_html($publickey); ?>
    </div>
    
    <label>&nbsp;</label><INPUT class="button" type="submit" name="submit" value="Send Message">
    </form>
    </div>
    
    </fieldset>
    
    </div>
    
    
     
    <div id="side-b">
    &nbsp 
    </div>
    </div>
    <div id="bcurve"></div>
    <?php require("footer.php"); ?>
    </div>
    <?php require("googleurchin.html"); ?>
    </body>
    </html>
    Code (markup):

     
    apollo911, Jun 7, 2010 IP