PDA

View Full Version : Required Fields - Contact Form



GGMcGee
June 6th, 2008, 12:53 AM
Hi,

I've set up a contact form for a website I'm making.

The pages involved is contact.html which is the page the HTML form is on, and mailer.php which contains the PHP script that sends an email.

The form works fine, but I really want to add in a script of sorts that sets every input box (Name, Email and Enquiry in this case) as a required field, and doesn't send the email unless each field has something type in it.

I've tried a couple of Javascript scripts I found online, but they are not working...

Can anyone give me a 100% guaranteed working script? I don't care if it's Javascript or PHP, whatever works.

Thanks a lot for your help.



<form method="post" action="mailer.php">
<p>&nbsp;</p>
<table width="298" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td width="56" align="right" valign="top"><span class="style1">Name</span></td>
<td width="267" align="left" valign="top"><input name="name" type="text" id="name" size="30" /></td>
</tr>
<tr>
<td align="right" valign="top"><span class="style1">Email</span></td>
<td align="left" valign="top"><input name="email" type="text" id="email" size="30" /></td>
</tr>
<tr>
<td align="right" valign="top"><span class="style1">Enquiry </span></td>
<td align="left" valign="top"><span class="style1">
<textarea name="enquiry" cols="30" rows="9" id="enquiry"></textarea>
</span></td>
</tr>
<tr>
<td align="right" valign="top"><span class="style1"></span></td>
<td align="left" valign="top"><span class="style1">
<input name="submit" type="submit" id="submit" value="Send" />
</span></td>
</tr>
</table>
</form>




<?php
if(isset($_POST['submit'])) {

$to = "thisemail@somewebsitethatthisemailisgoingto.com";
$subject = "Website - Enquiry";
$name = $_POST['name'];
$email = $_POST['email'];
$enquiry = $_POST['enquiry'];

$body = "Name: $name\n
Email: $email\n
Enquiry: $enquiry\n
";

echo "Your enquiry has been submitted to Some Website That This Email Is Going To!";
mail($to, $subject, $body);

} else {
echo "An error has occured. Please try again later.";
}
?>

djheru
June 6th, 2008, 01:16 AM
I would use javascript for this. It won't be 100%, because a very small percentage of people have javascript turned off, but it works well enough for me.

Change your form tag thusly:


<form method="post" action="mailer.php" name="mailer" id="mailer" onsubmit="return verify();">

Then, in the head of the page, create a javascript file to check the form fields:



<script type="text/javascript">
function verify()
{
var form = document.mailer;
var error = '';
if(form.name.value == '') { error += "Please enter your name before pressing submit\n"; }
if(form.email.value == '') { error += "Please enter your email before pressing submit\n"; }
if(form.enquiry.value == '') { error += "Please enter your enquiry before pressing submit\n"; }
if(error != '') { alert(error); return false; }
else { return true; }
}
</script>


This should work. If you really want to make sure that you are not getting garbage, you can check the email on the server side after it is submitted to make sure it's valid using regular expressions

GGMcGee
June 6th, 2008, 01:23 AM
Thanks!

Works perfectly.

Much appreciated. :D