Hello everyone
I am having this simple contact form and i want to make the name , email fields to be required.Also i want to add a thank you message after someone press the sumbit button.
Your help will be much appreciated.



if ($_POST['name'] == '' || $_POST['subj'] == '' || $_POST['mail'] == '' || $_POST['message'] == '')

define('TO', 'email@yourdomain.com');
define('SUBJECT', 'Contact Form!');

function sendEmail($to, $from, $subj, $body)
    $date = date( 'r' );
    $phpversion = phpversion();
    $boundary = md5( time() );

    $headers = <<<END
From: $from
Date: $date
MIME-Version: 1.0
Content-Type: multipart/related; boundary="$boundary"

    $body = <<<END
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: 7bit



    mail(trim($to), trim($subj), $body, $headers );

sendEmail(TO, trim($_POST['mail']), SUBJECT, 'E-Mail from: '.trim($_POST['name']).'<br /><br/>Subject: '.nl2br(trim($_POST['subj'])).'<br /><br/>Message: '.nl2br(trim($_POST['message'])));

header('Location: contact.html');



<form action="mail.php" method="post">
                    <input class="inp" name="name" type="text" value="Name:" onfocus="if (this.value == 'Your Name') this.value = '';" onblur="if (this.value == '') this.value = 'Your Name';" />
                    <input class="inp" name="mail" type="text" value="Email:" onfocus="if (this.value == 'Email') this.value = '';" onblur="if (this.value == '') this.value = 'Email';" />
                    <input class="inp" name="subj" type="text" value="Subject:" onfocus="if (this.value == 'Subject') this.value = '';" onblur="if (this.value == '') this.value = 'Subject';" />
                    <textarea class="mess" name="message" cols="" rows="" onfocus="if (this.value == 'Message') this.value = '';" onblur="if (this.value == '') this.value = 'Message';" >Message:

Recommended Answers

All 4 Replies

So with regard to validating, there are several options.

1) use the required attribute (HTML5). However, only newer browsers will support this attirubute. If I'm not mistaken, no support for Safari. So this may not be very viable.

2) use javascript. A simple function to get the value of the name and email input fields and check if they are null or have an empty sting would work. However, even if you use javascript, you should always check server side as well because the user may have javascript disabled.

3) check server side in your mail.php page. This ensures that you catch the issue. However, unless you also use javascript, this approach forces the post to check and if you have issues, you have to take some action. Its best to try to catch the issue client side first with javascript.

Sample code you can use to check if name and email input elements are filled out. you can adapt this to your code.

<!DOCTYPE html>
function validateForm()
var x=document.forms["form1"]["name"].value;
var y=document.forms["form1"]["email"].value;
if ((x==null || x=="") || (y==null || y==""))
  alert("Name and email are required.");
  return false;

<form name="form1" action="mail.php" onsubmit="return validateForm()" method="post">
First name: <input type="text" name="name">
Email: <input type="text" name="email">
<input type="submit" value="Submit">


With regard to your second question.. about a thank you message...

How do you want to display this? In an alert box, or on the mail.php page? send a thank you email? you need to be more specific.

Thank you for your answer.
Regarding the thank you message i was think to send him to a thank you page which i will create.

Ok, so after the form is submitted and the page transfers to mail.php, take care of the mailing part, then just redirect the user again to your thankyou.php page, or just integrate the thank you in mail.php.

I will give it a try and if need anything i will come back

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.