How to create PHP contact form | Demo and Tutorial

PHP contact form is used to communicate the site owner with customers. The site owner can know the thought of customers regarding their service with the contact form. We have created simple contact form using HTML and PHP. The webmaster can easily pluck this contact form in the new or existing web pages.

Process of contact form:

  • The contact form should have the basic fields like name, email, contact number, message.
  • Once user filled the information, the submitted form is validated by PHP backend program.
  • If there is any validation issue exists then redirect the user into contact form with validation issues mentioned.
  • If there is no validation issue then send the mail to site owner with the submitted data.
Required files:
  • php_contact_form.php
  • php_contact_form_submit.php
PHP functionalities used:
  • session
  • global function
  • PHP function
  • XSS prevention
  • Header function

Create the PHP file named “php_contact_form.php” and save the file in your desired folder, we have placed the file in the path of /demo/php_contact_form.php. This file is used for the frond-end process page as well as result/output page which contain both HTML and PHP coding. HTML is used for designing the page and PHP is used for dynamic process handling.

When user click the Submit button, the form action page “php_contact_form_submit.php” (<form action="php_contact_form_submit.php" method="post">) will be executed, that is the backend process for php_contact_form.php.


Create the PHP file named “php_contact_form_submit.php” and save the file in the same folder, in our case the file path is /demo/ php_contact_form_submit.php. This file is used for the backend process against user input form fields. Where we have to do the process of validation, XSS prevention and send mail to site owner.

If the input values pass the validation then we will send email and redirect the user to the previous page php_contact_form.php with the success message. Otherwise we will redirect the user to the previous page with the error message. Both success and error messages are handled by PHP at php_contact_form.php.

Click the below button to see the demo.

Please follow and like us:

Leave a Reply

Your email address will not be published. Required fields are marked *