How to create a simple contact form in the website and applying validation over it using jQuery with PHP

As you now the Website Contact form comes on the best priority for any organization as this may lead to establish one to one communication with their target audiences.

Here in the article, where we make you learn about “How to create a simple contact form in the website and applying validation over it using jQuery with PHP”.

We have created a simple HTML form with four fields- User Name, Email id, Contact No, and Message box for the query.

We have used following PHP mail () function with four parameters to send email on respective email id as a query of the user from the website.

 

 

[cc-code]
Mail($to, $subject, $header, $message);
[/cc]

$to variable is required for Specifying the identity of the receiver email
$subject specifies the subject of the email. This parameter can not contain any newline characters
$header: this is Optional. Specifies additional headers, like From, Cc, and Bcc.
Note: When sending an email, it must contain a from header. This can be set with this parameter or in the php.ini file.
$message: It defines the message to be sent. Each line should be separated with a (\n). Lines should not exceed 70 characters.

 

 

Contact_us.html

[cc-code]

How to create a simple contact form in the website and applying validation over it using jQuery with PHP

Simple jQuery Contact Form With Validation

Contact Form









 

[/cc]

 

 

Mail.php

[cc-code]

// Fetching Values from URL.
$name = $_POST[‘user_name’];
$email = $_POST[’email’];
$contact = $_POST[‘contact’];
$msg = $_POST[‘message’];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
if (!preg_match(“/^[0-9]{10}$/”, $contact)) {
echo “* Your contact number is wrong. *”;
} else {
$subject = $name; //specifies the subject of the email. This parameter can not contain any newline characters
$headers = ‘MIME-Version: 1.0’ . “\r\n”;
$headers .= ‘Content-type: text/html; charset=iso-8859-1’ . “\r\n”;
$headers .= ‘From:’ . $email. “\r\n”;
$headers .= ‘Cc:’ . $email. “\r\n”; // this is Optional. Specifies additional headers, like From, Cc, and Bcc.
$message = ‘

Hello ‘ . $name . ‘,

. ‘
Thank you…! For Contacting with Us.’
. ‘Name:’ . $name . ‘

. ‘Email:’ . $email . ‘

. ‘Contact No:’ . $contact . ‘

. ‘Message:’ . $msg . ‘


. ‘This is the user information.’
. ‘

. ‘We will be in touch with you .

‘;
$sendmessage = ”

” . $message . “

“;
$sendmessage = wordwrap($sendmessage, 70);
mail(“manglesh@phphurdles.com”, $subject, $sendmessage, $headers);
echo “Your Query has been received, We will contact you soon.”;
}
} else {
echo “* invalid credentials *”;
}
?>
[/cc]

For downloading working Demo : Download Here

For a extended view on Jquery Validation you can also read our tutorial FORM VALIDATION USING JQUERY VALIDATION PLUGIN.

Hope you like it, Keep reading our other articles.

Thank you!

Attachments

4 Comments

  1. ss raj said:

    Jquery is the perfect choice for the validation purposes. The length of code reduces in Jquery as we get so many classes and the library & it is an open source as well. According to me, Javascript is going to make new benchmarks in the web development. We can consider the code you have just shown, it is a bit complicated but very much effective!!! Looking forward to reading this kind of JAVASCRIPT executions in the future.

    June 13, 2017
    Reply
    • Saurabh Dubey said:

      Yes Mate,

      Will surely post some good topics to help our developer mates

      Cheers!

      June 13, 2017
      Reply
  2. PHP Freelancer said:

    Great tutorial. This tutorial was really helpful to create a contact form on my business site. This is useful to beginners. Thanks.

    July 5, 2017
    Reply
    • Saurabh Dubey said:

      Thank You

      July 5, 2017
      Reply

Leave a Reply

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