Tech

Dasumo.com – Solutions of Web Development

The Dasumo Blog – Free Resources: Downloads, Tutorials, Security Tips and much more…

Contact Form in PHP with validation (Simple and Very Basic). – Dasumo Tips

Hello everyone, this time Dasumo Blog presents this simple tutorial to create a simple and very easy to implement contact form, made in PHP.

To make our form a bit more interesting, I will add JavaScript validation of empty fields + Shipment confirmation or error message in case the form cannot be sent.

The form consists of 3 files:

  1. contacto.html – HTML page that people who visit the form will see on your website.
  2. validar.js – JavaScript to validate that the fields on our form are not empty
  3. contacto.php – The PHP script that collects the variables from our form and sends the contact E-Mail.
    Dasumo

Source code of the contact.html file : 
HTML code:

<html> 
<head> 
<title> PHP Contact Form by Dasumo </title> 
<! - Validation file placed inside HEAD / HEAD -> 
<script type = "text / javascript" src = "validate. js " > </script> 
<! - End Validation File -> 
</head> 
<body> 
<! - Contact form placed within BODY / BODY -> 
<div align = " center " > 
< form method = "post" action = " contact.php " onsubmit = "return Validate (this);" > 
    <
    "name" name = "name" type = "text" size = "27" >  </label> <br> <br> 
    <label for = "email" > Email: <br> 
    <input id = "email" name = "email" type = "text" size = "27" >  </label> <br> <br> 
    <label for = "subject" > Subject: <br> 
    <input id = "subject" name = "subject "type = "text" size = "27" > 
    </label> <br> <br>
    <label for = "message" > Message: <br> 
    <textarea name = "message" id = "message" rows = "5" cols = "30" > </textarea> 
    </label> <br> <br> 
    <input type = "submit" name = "submit" value = "Send" /> <br> 
</form> 
</div> 
<! - End Contact Form -> 
</body> 
</html>

Source code of the validar.js validation file :

// Function that verifies empty form fields
function Validate (f) {
if (f.name.value == "") {
    alert ("Please enter your full Name");
    f.name.focus ();
    return false;
    }
if (f.email.value == "") {
    alert ("Please enter your email address \ xF3n");
    f.email.focus ();
    return false;
    }
if (f.subject.value == "") {
    alert ("Please write the subject of your Message");
    f.subject.focus ();
    return false;
    }
if (f.message.value == "") {
    alert ("Please write your Message.");
    f.message.focus ();
    return false;
    }
}

Source code of the contact.php file : 
Note: The @ character has been added before the PHP variables or functions to prevent PHP from displaying errors on the screen, instead, it will display our custom error message. 
PHP code:

<? php 
// We import the form variables 
@ $ name  =  addslashes ( $ _POST [ 'name' ]);
@ $ email  =  addslashes ( $ _POST [ 'email' ]);
@ $ subject  =  addslashes ( $ _POST [ 'subject' ]);
@ $ message  =  addslashes ( $ _POST [ 'message' ]); 
// We prepare the contact message 
$ headers  =  "From:  $ email \ n"  // The person who sends the mail
  . "Reply-To:  $ email \ n" ; 
$ subject  =  " $ subject " ; // The subject
 $ email_to  =  "[email protected]" ; // change your email
 $ content  =  " $ name  has sent you the following message: \ n"
 . "
. " $ message \ n"
 . "\ n" ; 
// We send the message and check the result
 if (@ mail ( $ email_to ,  $ subject  , $ content  , $ headers  )) { 
// If the message is sent it shows a confirmation 
die ( "Thank you very much, your message was sent correctly" );
} else { 
// If the message is not sent it shows the error message 
die ( "Error: Your message could not be sent, try later" );
} 
?>

That’s it, to install, we upload the 3 files via FTP to our website and place the 3 files inside the same folder.

Note: The contact form on Dasumo is based on this simple example, so it will be up to you to adapt it to the style and needs of your website.

Dasumo cutestat

Dasumo cutestat

Dasumo.com website outlook Stats

dasumo.com stats and valuation

Charlie Wilson

I am Charlie Wilson, an enthusiast who loves to travel and explore the world. Not only travelling is what I love in fact, I write travel blogs too, in order to entertain people and show them how important travelling is. I am a passionate writer and by profession

Related Articles

Leave a Reply

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

Subscribe To Our Newsletter
Subscribe to our email newsletter today to receive updates on the latest news, tutorials and special offers!
Thanks for signing up. You must confirm your email address before we can send you. Please check your email and follow the instructions.
We respect your privacy. Your information is safe and will never be shared.
Don't miss out. Subscribe today.
×
×
WordPress Popup Plugin
Close
Close