tags:

views:

130

answers:

2

Dear Professors!

I have this little code (part of my regsitration code) :

<?php 
 if (@$_POST['Submit'] == 'Register'){

     if (strcmp(md5($_POST['user_code']),$_SESSION['ckey']))
    { 
         die("Invalid code entered. Please enter the correct code as shown in the Image");
    } 
 } 
 ?> 

<form name="form1" id="signupForm" method="post" action="register.php" style="padding:5px;">
<div class="reg_left">Security code:</div>
<div class="reg_right"><input name="user_code" type="text" size="10"> <img src="pngimg.php" align="middle" width="100" height="40"></div>
<div><input type="submit" name="Submit" class="submit" value="<?php echo $gomb_reg;?>"></div>
</form>

Unfortunatelly this is check if code is valid after post the form data. I would like to check before posting. So i think i must use Jquery validation plugin. (btw i use Jqery to validate the other fields like email, user, password). But as im not an expert in Jqery i need help to write that php code above in Jquery.

Thank you.

+1  A: 

I believe the basic jist would be:

  • Hook a function to the submit element
  • That JS function sends the user_code value to PHP script
  • The PHP script checks the value and and outputs (returns) a bool (or json)
  • The JS function allows the post if a good value is returned

(Note: Since the jQuery AJAX function do not stop the execution of the script, you'll have to stop the form from submitting, then submit the form in the AJAX callback.)

Look at the jQuery docs for .post or .getJSON, use those function to sent the 'user_code' to be checked.

Tim Lytle
Oh, and I guess you could just give jQuery the code (on page load), but that kinda defeats the whole point, in theory. In practice it would still stop most bots (IMO), but just giving the form elements weird (or changing) names, and adding some hidden elements with attractive names will stop most bots.
Tim Lytle
+1  A: 
iangraham