Hi guys!
I’m new to PHP, but have managed to get a basic form to work on my website. There is currently no validation on the form, however, which is what I want to get fixed. I’ve tried several tutorials and looked at a number of discussion threads, but there’s nothing that I’m able to follow to get my form validated.
So… here’s the code I’m using for the form:
<?php
if (isset($_REQUEST['email']))
//if "email" is filled out, send email
{
//send email
$name = $_REQUEST['name'] ;
$email = $_REQUEST['email'] ;
$subject = $_REQUEST['subject'] ;
$message = $_REQUEST['message'] ;
mail( "email@address.com", "$subject", "
Name: $name
Message: $message", "From: $email" );
echo "<p>Thanks, $name.</p><p>Your message was sent successfully and I'll get back to you as soon as possible.</p>";
}
else
//if "email" is not filled out, display the form
{
echo "<div id='messageBubble'></div>
<form id='contactForm' action='index.php#contact' method='post'>
<ul>
<li><input type='text' name='name' id='name' value='Your Name' onfocus='if (this.value == \\"Your Name\\") {this.value = \\"\\";}' onblur='if (this.value == \\"\\") {this.value = \\"Your Name\\";}' /></li>
<li><input type='text' name='email' id='email' value='Your Email' onfocus='if (this.value == \\"Your Email\\") {this.value = \\"\\";}' onblur='if (this.value == \\"\\") {this.value = \\"Your Email\\";}' /></li>
<li><input type='text' name='subject' id='subject' value='Subject' onfocus='if (this.value == \\"Subject\\") {this.value = \\"\\";}' onblur='if (this.value == \\"\\") {this.value = \\"Subject\\";}' /></li>
<li><textarea name='message' id='message' cols='20' rows='5' onfocus='if (this.value == \\"Message\\") {this.value = \\"\\";}' onblur='if (this.value == \\"\\") {this.value = \\"Message\\";}' >Message</textarea></li>
<li><input type='submit' id='submit' value='Press'/><input type='reset' id='reset' value='Do not press'/> </li>
</ul>
</form>";
}
?>
Can someone please give me some pointers to get the form working with some form of validation? I need all fields to be completed, with a valid email address.
Also, the form is currently attracting spam messages which include HTML/hyperlinks - if at all possible, I’d like to reject any messages which have HTML in them. This isn’t essential though, if it’s too complicated.
Thanks,
Tim