views:

2878

answers:

2

I am working on a comment script using ajax, json and jquery.

I have most of it done except the hardest part, If a user post X ammount of comments in X ammount of time, my php script will return a "captcha" trigger to my javascript code below, telling it that this user needs to enter a captcha code before we will post there comment to the DB

Below you will see I have 3 options for a returned value: success, error, and captcha

when captcha is returned, I want to open up a popup dialog on the screen and load my captcha script, so far everything works up to this point.

Now I need to make the captcha script submit, not only the captcha value but also my users comment, so I just need to populate a hidden form field with the comment value. Easy enough I just havent done it yet. Now if a user get the wrong captcha value, it needs to reload the screen but popup the captcha screen again and still have the comment hidden in a form value. That is where the tricky part comes in.

After reading my plan and viewing my code below, do you think I am on the right track? Any suggestions on a better way?

<script type="text/javascript">
$.ajax({
    type: "POST",
    url: "process.php",
    data: dataString,
    dataType: "json",
    success: function(data) {
       if (data.response === 'captcha') {
            //they are mass posting so we need to give them the captcha form
      // I can send the users sanitized comment through the captcha script somehow
      // 'data.comment' is holding the comment value
            jQuery.facebox(function() {
          // This opens up a facebox dialog on the screen and popolates it with the captcha image and script
                jQuery.get('captchabox.php', function(data) {
                    jQuery.facebox( '' + data + '')
                    data.comment;
                })
            })
       } else if (data.response === 'success') {
            // success append the sanitized-comment to the page
            $('#comments').prepend(data.comment);
       } else if (data.response === 'error') {
            // just an alert for testing purposes
            alert('sorry there was an error');
       };
    }
});
</script>

UPDATE:

After more thought it seems I need to make my captcha use ajax as well or else there will be a page reload =(

A: 

Have you looked into just using recaptcha? It seems like you're trying to reinvent the wheel here, if I am reading your question correctly.

Matt Ball
IMHO the user experience with recapatcha is one of the worst. I would highly recomend other implementations with more readable text.
scunliffe
No I have captcha, I am trying to make it work with my ajax comment script to prevent reloading the screen when they enter a captcha
jasondavis
@scunliffe: I've had next to no problems with recaptcha. At the very worst, if you can't read a word, you just get a new pair.@jason: what is captcha? you have *a* captcha?
Matt Ball
A: 

I ended up doing it this way and it works great. Recaptcha, how the heck the others thought that was related is unknown

jasondavis