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

How To Create Contact Form Using PHP

Caffrey

Tech User Experience Designer
C Rep
0
0
0
Rep
0
C Vouches
0
0
0
Vouches
0
Posts
75
Likes
120
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
Related Code: User Registration Form Using PHP

In this tutorial, we are going to learn on How To Create Contact Form Using PHP. We have a contact form in the example below for the user to send their message, suggestions, questions, feedback, and clarifications about your work or your website.

We have three fields as full name, email, and message in our contact form. This is a very simple tutorial, hope you will find this useful.

Contact Form - HTML

This source code for our contact form.

  1. <form

    name

    =

    "frmContact"

    method

    =

    "post"

    action

    =

    ""

    >

  2. <div

    class

    =

    "aler_message"

    ><?php if(

    isset(

    $message)

    )

    {

    echo $message; }

    ?></

    div

    >

  3. <table

    border

    =

    "0"

    cellpadding

    =

    "10"

    cellspacing

    =

    "1"

    width

    =

    "500"

    align

    =

    "center"

    >
  4. <tr

    class

    =

    "tableheader"

    >
  5. <td

    colspan

    =

    "2"

    >

    Contact Form</

    td

    >
  6. </

    tr

    >
  7. <tr

    class

    =

    "tablerow"

    >
  8. <td

    >

    Full Name<br

    /

    >

    <input

    type

    =

    "text"

    class

    =

    "text_input"

    autofocus=

    "autofocus"

    name

    =

    "fullname"

    ></

    td

    >
  9. <td

    >

    Email<br

    /

    >

    <input

    type

    =

    "text"

    class

    =

    "text_input"

    autofocus=

    "autofocus"

    name

    =

    "email"

    ></

    td

    >
  10. </

    tr

    >
  11. <tr

    class

    =

    "tablerow"

    >
  12. <td

    colspan

    =

    "2"

    >

    Message<br

    /

    ><textarea

    name

    =

    "user_message"

    class

    =

    "text_input"

    autofocus=

    "autofocus"

    cols

    =

    "60"

    rows

    =

    "6"

    ></

    textarea

    ></

    td

    >
  13. </

    tr

    >
  14. <tr

    class

    =

    "tableheader"

    >
  15. <td

    colspan

    =

    "2"

    ><input

    type

    =

    "submit"

    class

    =

    "btn_submit"

    name

    =

    "submit"

    value

    =

    "Submit"

    ></

    td

    >
  16. </

    tr

    >
  17. </

    table

    >

  18. </

    form

    >

Data Table

This is the data where the user message shows in this table.

  1. <table border="1" class="table_data" cellspacing="5" cellpadding="5">
  2. <tr style="color:blue;">

  3. <th>
  4. Full Name
  5. </th>
  6. <th>
  7. Email
  8. </th>
  9. <th>
  10. Message
  11. </th>
  12. </tr>
  13. <?php
  14. $conn

    =

    mysql_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    ;
  15. mysql_select_db

    (

    "contact_form"

    ,

    $conn

    )

    ;
  16. $result

    =

    mysql_query

    (

    "select * from tbl_contact order by tbl_contact_id DESC "

    )

    or die

    (

    mysql_error

    (

    )

    )

    ;
  17. while

    (

    $row

    =

    mysql_fetch_array

    (

    $result

    )

    )

    {
  18. $id

    =

    $row

    [

    'tbl_contact_id'

    ]

    ;
  19. ?>
  20. <tr style="text-align:center; color:blue;">
  21. <td style="width:200px;">
  22. <?php

    echo

    $row

    [

    'fullname'

    ]

    ;

    ?>
  23. </td>
  24. <td style="width:200px; color:red;">
  25. <?php

    echo

    $row

    [

    'email'

    ]

    ;

    ?>
  26. </td>
  27. <td style="width:200px; color:blue;">
  28. <?php

    echo

    $row

    [

    'user_message'

    ]

    ;

    ?>
  29. </td>
  30. </tr>
  31. <?php

    }

    ?>
  32. </table>

And, we have CSS style.
  1. table

    {
  2. border

    :

    yellow

    5px

    solid

    ;
  3. }
  4. .table_data

    {
  5. margin-left

    :

    291px

    ;
  6. margin-top

    :

    20px

    ;
  7. }
  8. .tableheader

    {
  9. background-color

    :

    maroon

    ;
  10. color

    :

    white

    ;
  11. font-weight

    :

    bold

    ;
  12. font-size

    :

    25px

    ;
  13. }
  14. .tablerow

    {
  15. background-color

    :

    black

    ;
  16. color

    :

    white

    ;
  17. font-size

    :

    20px

    ;
  18. font-weight

    :

    bold

    ;
  19. }
  20. .aler_message

    {
  21. color

    :

    #FF0000

    ;
  22. font-weight

    :

    bold

    ;
  23. text-align

    :

    center

    ;
  24. width

    :

    100%

    ;
  25. cursor

    :

    pointer

    ;
  26. }
  27. .text_input

    {
  28. font-size

    :

    18px

    ;
  29. border

    :

    blue

    1px

    solid

    ;
  30. background

    :

    azure

    ;
  31. text-indent

    :

    10px

    ;
  32. cursor

    :

    pointer

    ;
  33. }
  34. .btn_submit

    {
  35. font-size

    :

    18px

    ;
  36. width

    :

    100px

    ;
  37. background

    :

    azure

    ;
  38. border

    :

    blue

    1px

    solid

    ;
  39. padding

    :

    6px

    ;
  40. border-radius

    :

    8px

    ;
  41. color

    :

    blue

    ;
  42. cursor

    :

    pointer

    ;
  43. }
  44. .btn_submit

    :

    hover

    {
  45. font-size

    :

    18px

    ;
  46. width

    :

    100px

    ;
  47. background

    :

    azure

    ;
  48. border

    :

    re 1px

    solid

    ;
  49. padding

    :

    6px

    ;
  50. border-radius

    :

    8px

    ;
  51. color

    :

    red

    ;
  52. cursor

    :

    pointer

    ;
  53. }

Contact Form Input Into Database

This is PHP query where our data store in the database. First, create a database table, name into "contact_form".

  1. $conn

    =

    mysql_connect

    (

    "localhost"

    ,

    "root"

    ,

    ""

    )

    ;
  2. mysql_select_db

    (

    "contact_form"

    ,

    $conn

    )

    ;
  3. mysql_query

    (

    "INSERT INTO tbl_contact (fullname, email, user_message) VALUES ('"

    .

    $fullname

    .

    "', '"

    .

    $email

    .

    "','"

    .

    $user_message

    .

    "')"

    )

    ;

After executing the query code above it will be added to the database, this is the result.
add_2.png

data_show.png

Related Code: User Registration Form Using PHP

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.


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

452,292

323,526

323,535

Top