views:

66

answers:

2

I have a signup form on my home page which uses server side validation and ajax so my page doesn't refresh while validating.

When all validation is passed my page is taken to a registered.html which is a temporary page for now. Later on it will be an account area.

What I want

I want to have 1 last step after the actual form validation which will be my reCaptcha validation. Instead of submitting the form after validation I want reCaptcha to popup twitter style or just replace the form fields facebook style.

Only after successful captcha validation will the form be submitted. I have provided the code below and would really appreciate if someone could advise me on the best way to implement this. Right now I'm at a stage where I'm confused.

Error checking and at the very bottom you can see the echo statement that sends a user to registered.html after successful validation

<?php

// we check if everything is filled in

if(empty($_POST['first_name']) || empty($_POST['last_name']) || empty($_POST['email']) || empty($_POST['password']))
{
 die(msg(0,"All the fields are required"));
}


// is the sex selected?

if(!(int)$_POST['sex'])
{
 die(msg(0,"You have to select your sex"));
}


// is the birthday selected?

if(!(int)$_POST['day'] || !(int)$_POST['month'] || !(int)$_POST['year'])
{
 die(msg(0,"You have to fill in your birthday"));
}


// is the email valid?

if(!(preg_match("/^[\.A-z0-9_\-\+]+[@][A-z0-9_\-]+([.][A-z0-9_\-]+)+[A-z]{1,4}$/", $_POST['email'])))
 die(msg(0,"You haven't provided a valid email"));



// Here I must put your code for validating and escaping all the input data,
// inserting new records in your DB and echo-ing a message of the type:

// echo msg(1,"/member-area.php");

// where member-area.php is the address on my site where registered users are
// redirected after registration.




echo msg(1,"registered.html");


function msg($status,$txt)
{
 return '{"status":'.$status.',"txt":"'.$txt.'"}';
}
?>

The javascript

$(document).ready(function(){

 $('#fhjoinForm').submit(function(e) {

  register();
  e.preventDefault();

 });

});


function register()
{
 hideshow('loading',1);
 error(0);

 $.ajax({
  type: "POST",
  url: "submit.php",
  data: $('#fhjoinForm').serialize(),
  dataType: "json",
  success: function(msg){

   if(parseInt(msg.status)==1)
   {
    window.location=msg.txt;
   }
   else if(parseInt(msg.status)==0)
   {
    error(1,msg.txt);
   }

   hideshow('loading',0);
  }
 });

}


function hideshow(el,act)
{
 if(act) $('#'+el).css('visibility','visible');
 else $('#'+el).css('visibility','hidden');
}

function error(act,txt)
{
 hideshow('error',act);
 if(txt) $('#error').html(txt);
}

Now I'm thinking there must be a simple way to implement reCaptcha ajax api into my current form. At first I tried to add onclick="showRecaptcha('recaptcha_div');" to the end of my form where my input type=submit is located this didn't work.

The end of my form

    <td><input type="submit" class="joinButton" value="Join Us" /><img id="loading" src="<?php echo base_url()?>images/join/ajax-loader.gif" alt="working.." /></td>
    <div id="recaptcha_div"></div>
    </tr>

I've follows the instructions on the recaptcha website and no luck so far. Code snippets, advice anything will help. I appreciate it.

Thanks

A: 

Have you looked into their JS lib API reference? http://wiki.recaptcha.net/index.php/Overview#AJAX_API

Recaptcha.create("apikey",
  "placeholder_div",
  {
    theme: "red",
    callback: function(){
      // what to do on success, like maybe submit your form
    }
  }
);

You can just create the reCAPTCHA element where desired and add some callback to do whatever you want.

Regarding your comment about where to put the recaptcha creation code, you could put it in the callback from your AJAX request

   if(parseInt(msg.status)==1)
   {
     Recaptcha.create("apikey",
       "placeholder_div",
       {
         theme: "red",
         callback: function(){
           // what to do on success
           window.location=msg.txt;
         }
       }
     );
   }
BBonifield
Yep, I started implementing it. I'm stuck at the last step. Not quite sure where to put the code for the reCaptcha so that after validation on the form is passed it goes to that function rather than "echo msg(1,"registered.html");
Psychonetics
@Psychonetics note my edited post. You don't necessarily need to change how your PHP code is generating the response.
BBonifield
I have no idea where I'm going wrong. I keep getting a "missing } after property list" error in netbeans.. I want to solve this error before I continue tweaking the code I have just added. I have provided the code below.
Psychonetics
I meant above..
Psychonetics
@Psychonetics you didn't copy/paste the code as I wrote it. Compare the two.
BBonifield
Thanks that sorted it. The captcha comes up for a split second then i'm taken to the registered page. I'm wondering how I could add some code to the function that stops it from going on to the "window.location=message.txt;" and make's the user pass the validation for the captcha first and if that is fine the
Psychonetics
then the the process that forwards the user to the registered.html page is allowed to run
Psychonetics
It works now. reCaptcha comes up.. Now all I need to do is make the captcha verify correctly. I was reading the documentation and I have to put some code in the same place my field validation is created. Going to look into this more. Until this is solved as this post could help someone else in future.
Psychonetics
A: 

Here is the code I am getting the error with.. on the line "window.location=msg.txt};"

function register()
{
    hideshow('loading',1);
    error(0);

    $.ajax({
        type: "POST",
        url: "submit.php",
        data: $('#fhjoinForm').serialize(),
        dataType: "json",
        success: function(msg){

            if(parseInt(msg.status)==1)
            {

                        Recaptcha.create("apikey", "placeholder_div",
                        {
                        theme: "red",
                        callback: function(){
                        // what to do on success

                window.location=msg.txt};
            }
            else if(parseInt(msg.status)==0)
            {
                error(1,msg.txt);
            }

            hideshow('loading',0);
        }
    });

}
Psychonetics