AJAX Contact Form with CAPTCHA, Realtime Validator and PHP Backend

Posted on February 8, 2010, Filled under AJAX, JQuery, PHP,  Bookmark it

Back in the autumn of 2008, I have written a tutorial about how you can create a basic AJAX Contact Form that works with PHP Back-end. The post is one of the most popular here on Bit {Repository} having over 4,000 monthly page views and over 200 comments so far. Since many people asked me how they can do this and even hired me to offer them assistance with this application, I have decided to write a more advanced script with many features including CAPTCHA and RealTime Validation. Unlike many AJAX Captchas out there that rely on JavaScript as an Anti-spam measure and have a medium security level, the one I have chosen to implement uses PHP to securely generate and register the required code in the background. Moreover, you can specify whether to show letter, numbers or both on the verification image.

Premium AJAX Contact Form

Features list

  • Realtime Form Validation that makes the form completion process interactive (can be enabled/disabled)
  • Both JavaScript & PHP Validation
  • Powerful CAPTCHA (the option to enable/disable it)
  • Auto Responder (customizable)
  • Ability to integrate the values from the form (beside the actual message) into the mail body
  • Tableless Form (does not use tables)
  • The e-mail templates (message, subject) are customizable
  • The errors and the notifications can be easily changed
  • A help page (documentation) that will guide you through the script setup

Server Requirements

  • PHP4+ Support (any decent server should have it)
  • GD Library Enabled (if want to use CAPTCHA)

Browser Compatibility

  • This script is compatible with all major browsers. It was tested in FireFox 3.5, IE 6, IE 7 & IE 8, Google Chrome 3.0, Opera 10, Safari 4.0, SeaMonkey 2.0, Flock 2.5.6

Live Preview

  • » View this form in action
  • Get it now

    The premium ‘AJAX Contact Form’ is free for our email readers. To get a copy just enter your name and e-mail address and you will receive the script as an attachment, immediately after you confirm your subscription.

    What else do I get as a subscriber?

    • Latest posts delivered to your inbox
    • Opportunities to make money online
    • News regarding web development in general

    Sponsors

    69 Replies to "AJAX Contact Form with CAPTCHA, Realtime Validator and PHP Backend"

    1. hi .
      how to download it . i was subscribed to newsletter already .

      1. I will send today a newsletter especially for those who didn’t get the script. They will get access to the application.

        PS: I’ve just sent you a mail containing the script (attachment).

        1. thank you Gabriel . I’ve received it .

        2. hey man it is the most complete jquery contact form that i see . thank you . :D

        3. hi Gabriel , I’ve not using this good script in any project now . when i used it in a live project i will come back and give you a feedback . thank you man . your site is a good reference for me .

    2. hi
      i downloaded the script and i integrated it on a wordpress theme
      when i send a message the form shows that it is sent but i dont recive it in my mail box i configured the common.php with my gmail box
      note that i use the script on a free webhosting server

      please tell me what’s the problem because i need it .
      thanks

      1. @Sakari, I’ve just re-tested the same script to make sure that I didn’t miss something. It works fine. You said that you’re using a free web hosting service. That could be the problem. Did you use any other contact forms on your website? Perhaps the mail() function is not properly working on your web hosting service.

        Consider making some test with the sample codes from this address:
        http://www.php.net/manual/en/function.mail.php

        Did you check your SPAM folder? Although I quite doubt that the message was marked as spam you never know how it was filtered (I don’t know what content did you use to submit the form).

        PS: Send a mail to your web hosting support.

    3. hi Gabriel,
      I’ve not using yet this nicely script in my project, because my hoster not provide PHP.
      But soon I hope to change host company, and I will use this script in new project.
      Thanx much, Gabriel.

    4. hi
      it works perfectly
      great job men !
      you win an other follower for your blog
      thanks !

    5. Similar issue as sakari, automated mail received in test email but not the main message email …(I’m hosted on a paid server)

    6. Thank You Very Much This Is An Amazing Form

    7. hi…its very nice and working also fine, beauty, perfect. Thank u for this script.

    8. very nice application. Thanks

    9. The contact form is just what I needed. I’ve tested it on a virtual apache server only a few times (it’s functionality) and it seems that the captcha isn’t working the way is expected to work but I hope that on a real server it will work just like in the demo. I think it’s great however!
      Thanks!

      1. I’m glad to hear that. The CAPTCHA should work on the virtual apache server too, if everything is configured properly. It just requires the GD library to be enabled in order to generate the verification image. Note that this script is using sessions too (cookie-based).

    10. Hello. I had problems with character encoding when the body of the message was sent but I resolved.
      In line 86 of parse.php I replaced “charset = iso-8859-1″ with “charset = utf-8″.
      I had no other problems regarding the setup of the form.
      I hope I helped you to refine the form
      thanks!

    11. nice job =)

    12. Great Work! Is just what I needed.
      Thanks!

    13. This is a fantastic form…
      Really Awesome!,
      Thank you.

    14. Thank you for the script.
      I have read your implementation of ajax contact form and is good.
      Thank you.

    15. Kuddo’s for the follow up e-mail. It’s working fine. Code looks solid, haven’t found any bugs. Would be nice to be able to integrate it with html instead of havin a separate php ;)

      I will definitely give it a try on my next project.

      One suggestion is skins. Since it’s a plug and play form it would make sense to offer some different skins with it. If I make one I’ll sent it your way.

    16. Hi Gabriel :)

      I have a little problem with Your script. When I try to send message with polish special characters (i.e. ą, ć) I get weird
      signs that are not polish charakters (i.e. Ä) in the message that comes on my inbox. I tried to change the charset into
      parse.php file, from iso-8859-1 to iso-8859-2 and utf-8 but it does not give any results :( do You have any solution for this?

      Waiting for message from You…

      Sorry for my english ;)

      1. @Gregory,

        I will investigate this and give you an update. I’ve made tests too with charset ‘iso-8859-2′ but the results were the same.

        PS: Did you test other contact forms? How did you get the characters?

        1. Hi Gabriel,
          i don’t know where was the problem, but i have probably changed something in the source code. When i tried to send a message from an unchanged form (only charset changed) it works great :) so now i did less changes and it is OK :)

          THANKS!

    17. I did not get it to work on blue host :(

      I got the follow support feedback..
      You will need to make a change to this file: /home3/public_html/contact-app/parse.php
      line 87: $senderHeaders .= ‘From: ‘.$sender_email. “\r\n”;

      I did change it to a email using:

      $senderHeaders .= ‘From: ‘.”myemail@hotmail.com”. “\r\n”;

      Still did not work :(

      1. @Jack,

        I don’t know how your hosting account is configured. However, I’ve made an upgrade to the script by integrating the well known PHPMailer Class. Consider downloading the new script and let me know how it works.

    18. Really Awesome!,
      Thank you.

    19. Hi Gabriel, Thank you so much for the awesome contact form. :)

      I integrated it on my contact form and I’ve got an error message such as “The mail cannot be sent due to an internal error. Please retry later!”…

      1. Thats the same error msg I got!

      2. @Aco,

        You usually get this error if the mail cannot me sent. This is most probably because the mail() function is not activated on your hosting account OR the (mail) server is not properly configured.

        1. Thanks for your support Gabriel!
          I changed hosting server and it works well! Thanks again for providing such a great script. :)

    20. Hey Gabriel,

      One thing I found confusing is that after you’ve sent a message, be it successful or not, you don’t return to the form. This I believe is bad practice and it looks rather weird. It should reset itself after a successful message. So instead of removing the form I propose a “success” message in the top “please correct” box without removing the form.

      Also when you start typing your e-mail the “please correct” message shouldn’t be popping up. Only when the extension is incorrect should the message appear.
      It even appears when I tab from the name field to the e-mail field.

      A layout thing: I think the captcha and the security field should be on the same line next to each other. There is no reason for the captcha to take up two lines.

      Gideon

      P.s

      It’s a great script but there are some areas that need improvement from a usability point of view. If you’ve stopped supporting this let me know and I can sent you my updated code.

      1. Forgot a last usability thing. To make it easier for people to integrate with their own layout the labels would be better inside the fields instead of next to it. This is really easy modification and makes it look a lot more professional and easier to integrate in a third party lay out.

        1. http://i.imgur.com/s1Xow.png

          To show you what I mean. I am scrutinizing I know, but since you all ready put in so much effort making these small improvements will top that cake with a cherry.

        2. I agree that the labels inside the fields are looking professional. However, users must be able to choose between this in-field label feature and the default one. The latter is much more used than the former.

          1. True that. However it would be great to see support for in-field labels. Great to see you tackled the tab bug.

            More pressing is the fact that the current form is breaking the layout. Whilst you are using Ajax for the right things you are contradicting it’s function by changing the lay out after a submit or error. Not refreshing the page is great but the way you use it now it does exactly the opposite. What do you think?

            Please don’t mistake my critique for something I can assure you it’s not. I have the best intentions and I think you have been doing a great job. Specially releasing it to the community is impressive and admirable. I for one never take the time to turn development scripts in something for the community.

            Gideon

          2. ALSO:

            gideon@whatever.hsnkf

            is now a valid e-mail address. ;)

      2. Hi,
        Have you managed to add a success message? This is a fantastic script but I cannot use it without a success message.. :( :(

        1. @Akshay, The script already has a successful message notification. Do you really believe I made it without such a notification :) ? Perhaps you didn’t install the script correctly. Keep trying ;-)

          1. ooops sorry… It is working when I run it from the downloaded package but it doesn’t work when I insert the form into my page… I am running a couple other jquery plugins and they might be the cause.. :( I will keep trying…
            Fantastic job by the way.. :) :)

            1. It’s working :) :)
              You can take a look at this page to see the working script !! –
              http://weboreviews.com/contact.php
              Can this script be modified to build a bit complex forms like this one? – http://weboreviews.com/quote.php

              Thanks a million !!

    21. Very nice! We went through 8 different scripts, only to be frustrated with broken code, links, and tutorials, to finally find one so simple, beautiful and it works! We are avid php programmers, but are not yet too versed with ajax, so this solution was perfect! Thanks~

    22. Hey Gabriel,
      first of all, thanks for providing such a great script.
      it was so easy to implement in my existing layout.

      The is i think only one little bug in the script.
      When you tab through the form (from name to email) the error msg for email pop up straight away. Even before you get a chance to enter your email.

      Is that known or am I the only one?

      Best Regards,

      Jan

      1. Read two comments up ;)

        1. ooops :)
          so i am not the only one :)
          Let’s hope there is a simple solution for that!

          1. I’ve updated the script. You can download it again from the same location. Now when you tab through the form the error will not pop-up anymore. You can see the demo! ;)

    23. Hey that is what i call support :)
      unfortunately now the live validation doesn’t work anymore :(

      It was great that when you tabbed out of a field it got checked. this is not anymore the case.
      hope i make sense…

      Jan

      1. Every time I make updates, I test the script again and again. It does work fine for me. You can check the live demo. Make sure you refresh the page too ;) .

    24. Great script here – its wonderful to see that people still share their talents with the world.

      Well done for all yor hard work.

    25. Can someone help! I keep on getting this error message when I try sending message:

      “The mail cannot be sent due to an internal error. Please retry later!”

      1. See if your hosts php is configured properly.

    26. Thank you for this great script! Works beautifully

    27. I have the form running as provided but I want to add more form elements such as requiring the user to add a phone number.

      How do I go about adding this to the form without causing errors?

      I tried to copy a text field and renamed it sender_phone and then went through the code to add this wherever I saw the sender_subject but I couldn’t get it to work.

      TIA

      1. @Jeanne, do you want to add the phone number as a required field?

        1. Yes, I would like to add the phone number field and make it required. I would also like to know how to add fields that aren’t required for future reference.

          TIA!

          1. I would like to know too… :)

    28. Works good :)

    29. First I’d like to say THANK YOU for this great Code.

      The form is working just fine and it was very simple to edit.

      The implementation of a “skin/theme” is the next step, or even a “backoffice/administration” for manipulation the form and it’s fields.

      Best Regards

    30. Thanks this is an awesome form and saved me a ton of time.

    31. Thanks for providing the nice script.

    32. Thanks Gabriel,

      Did like it! Is a good piece of application.

    33. Propably, the best script I have ever seen for forms!

    34. Well, I mean what else can I say; it was fantastic and the codes really worked well.

      I edited some part of the codes and used it with mine, I just couldn’t believed how wonderful it worked.

      Nice job to you my guru……

      Thank you.

    35. Hi
      regarding your mail how i like your form here my answer

      Basicly when you need a very simple form yes it works but when comes up that you need more fields and different elements like radio checkboxes ect the script is far away from to be usefull i spend now allready 4 days to modify the script to my nessasaries and think about to change for a other better solution you can see your script here (still buggy) http://www.usenetbrasil.com.br/signup.php i include checkbox validation creditcard validation and some other functions by example hide some fields when is not nessasary select a other country than brasil will hide the cpf field select a other payment than creditcard will hide cvv and cardnumer field

      finaly you are far away from a proper working solution for complex forms take a look here http://livevalidation.com/examples maybe you can get here some ideas

      best regards mike

      1. Hi Mike,
        Can you please share your modified script? You can contact me through my website :)

    36. Gabriel,

      This script was awesome. I had to make it fit within my own application layout, so I had to modify several paths and change the CSS up a little bit, but the script works wonderfully. I now have a favorite new contact form for website developments.

    37. Hi,

      the script is good one.It is very helpfull.Can you please provide me script for other ajax usefull scripts?

      Amlan

    38. really nice and excellent ajax forum.. i got many from web.. but nice like this… but one thing why they many files..??

      one more thing. i tried to use without include mode.. i just copy all code and paste my page.. ( in my page have other code too ) then now work send mail .. no idea why…

      anyway your code really nice.. i like it much

      thanks u soo much

    39. Its possible to have validate the email field , i mean to check before submit if the email its a existent email or not, as Php mailer FE do…

      also, i guess what the best way to have the form in 2 language… i have to duplicate almost all script or theres a simple way?

    Leave a Reply


    * = required fields

    (will not be published)


    XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>


      

    CommentLuv Enabled