• We just launched and are currently in beta. Join us as we build and grow the community.

Contact Form with Captcha Confirmation using PDO in PHP

xd hanieldarrison

Platform Strategist
X Rep
0
0
0
Rep
0
X Vouches
0
0
0
Vouches
0
Posts
111
Likes
184
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 500 XP
We are going to create Contact Form with Captcha Confirmation using PDO in PHP. These simple tutorial, you can download for free. I will teach you in step by step on how to create a contact form with captcha confirmation using PDO in PHP. Also, you can learn how to generate a random code and you can use this to protect your page from random spammers. You can use this also as a type of a simple test that the response is generated by a human being and it's a very common on other websites before submitting the information in the form field.

You will learn:

  • The user learns to generate a random code.
  • The user learns the basics of CSS styling.
  • The user learns to create contact form using HTML.
  • The user learns to create stylish contact form using CSS.

Creating Markup and Captcha Random Code

This simple source code, we are going to create our contact form where the user can type their message, full name, and email. Also, we construct the captcha random code for the confirmation before they submit their message.

  1. <form

    class

    =

    "form-horizontal"

    method

    =

    "POST"

    action

    =

    "contact_query.php"

    >

  2. <p

    class

    =

    "full_name"

    >
  3. <input

    type

    =

    "text"

    name

    =

    "full_name"

    id

    =

    "full_name"

    placeholder=

    ""

    autofocus=

    "autofocus"

    required/

    >
  4. <label

    for

    =

    "full_name"

    >

    Your Name</

    label

    >
  5. </

    p

    >

  6. <p

    class

    =

    "email"

    >
  7. <input

    type

    =

    "email"

    name

    =

    "email"

    id

    =

    "email"

    placeholder=

    ""

    required/

    >
  8. <label

    for

    =

    "email"

    >

    Your Email Address</

    label

    >
  9. </

    p

    >

  10. <p

    class

    =

    "message"

    >
  11. <textarea

    name

    =

    "message"

    placeholder=

    ""

    required></

    textarea

    >
  12. <label

    for

    =

    "message"

    >

    Your Message</

    label

    >
  13. </

    p

    >

  14. <div

    class

    =

    "control-group"

    style

    =

    "float:left; margin-left:-185px;"

    >
  15. <div

    class

    =

    "controls"

    >

  16. <img

    src

    =

    "generatecaptcha.php?rand=<?php echo rand(); ?>

    " name="captcha_img" id='image_captcha' >
  17. <a

    href

    =

    'javascript: refreshing_Captcha();'

    ><i

    class

    =

    "icon-refresh icon-large"

    ></

    i

    ></

    a

    >
  18. <script

    language

    =

    'JavaScript'

    type

    =

    'text/javascript'

    >
  19. function refreshing_Captcha()
  20. {
  21. var img = document.images['image_captcha'];
  22. img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
  23. }
  24. </

    script

    >
  25. </

    div

    >
  26. </

    div

    >

  27. <br

    /

    >
  28. <br

    /

    >
  29. <br

    /

    >

  30. <div

    class

    =

    "control-group"

    style

    =

    "margin-left:-181px;"

    >
  31. <div

    class

    =

    "controls"

    >
  32. <input

    id

    =

    "code"

    name

    =

    "code_confirmation"

    type

    =

    "text"

    placeholder=

    "Enter the Code Above"

    required></

    td

    >
  33. </

    div

    >
  34. </

    div

    >
  35. <div

    class

    =

    "control-group"

    style

    =

    "margin-left:-181px;"

    >
  36. <div

    class

    =

    "controls"

    >
  37. <button

    type

    =

    "submit"

    name

    =

    "send_message"

    class

    =

    "btn btn-primary"

    ><i

    class

    =

    "icon-ok icon-large"

    ></

    i

    >

    Submit</

    button

    >
  38. </

    div

    >
  39. </

    div

    >

  40. </

    form

    >

PHP Source Code in PDO

This is our contact query where they can save all the data from the users. Save it as "contact_query.php".

  1. <?php
  2. session_start

    (

    )

    ;
  3. include

    (

    'db.php'

    )

    ;

  4. if

    (

    isset

    (

    $_POST

    [

    'send_message'

    ]

    )

    )

    {

  5. $full_name

    =

    $_POST

    [

    'full_name'

    ]

    ;
  6. $email

    =

    $_POST

    [

    'email'

    ]

    ;
  7. $message

    =

    $_POST

    [

    'message'

    ]

    ;
  8. $code_confirmation

    =

    $_POST

    [

    'code_confirmation'

    ]

    ;

  9. if

    (

    strcmp

    (

    $_SESSION

    [

    'code_confirmation'

    ]

    ,

    $_POST

    [

    'code_confirmation'

    ]

    )

    !=

    0

    )

    {
  10. echo

    "<script>alert('The captcha code does not match!!'); window.location='index.php'</script>"

    ;
  11. echo

    "<script>javascript:self-history.back() </script>;"

    ;
  12. }

    else

    {

  13. $conn

    ->

    setAttribute

    (

    PDO::

    ATTR_ERRMODE

    ,

    PDO::

    ERRMODE_EXCEPTION

    )

    ;
  14. $insert_query

    =

    "INSERT INTO tbl_contact (full_name, email, message, code_confirmation)
  15. VALUES (?, ?, ?, ?)"

    ;

  16. $insert

    =

    $conn

    ->

    prepare

    (

    $insert_query

    )

    ;
  17. $insert

    ->

    execute

    (

    array

    (

    $full_name

    ,

    $email

    ,

    $message

    ,

    $code_confirmation

    )

    )

    ;

  18. echo

    "<script>alert('Successfully send your message!'); window.location='index.php'</script>"

    ;
  19. }
  20. }
  21. ?>

Output

This is the form where you can enter your full name, email, message and the captcha confirmation to contact any certain websites.

1_26.jpg


The user enter their information and message to contact any websites.

2_12.jpg


This is the result after enter all information, message, and the captcha confirmation as you can see in the image below.

result_3.jpg


And, that's it. This is the steps on how to create simple Contact Form with Captcha Confirmation using PDO in PHP.

Kindly click the "Download Code" button below for full source code. Thank you very much.

Hope that this tutorial will help you a lot.

If you are interested in programming, we have an example of programs that may help you even just in small ways.

Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much. Enjoy coding.


Download
You must upgrade your account or reply in the thread to view hidden text.
 

450,695

323,213

323,222

Top