[LEFT]Hi people. This is my first time posting. I guess I explain the problem and paste the code? Cheers.
I've got a working PHP form happening and field validation. But what I can't get working is email address validation.
Here is the code for the contact page, including field validation and form:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="stylesheet.css" />
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="stylesheet2.css" />
<![endif]-->
<!-- [if IE6]>
<link rel="stylesheet" type="text/css" href="" />
<![endif]-->
<title>Company name</title>
<style type="text/css">
#please {
position: absolute;
font-size: 10px;
font-family: Verdana, Arial Geneva, sans-serif;
position: absolute;
left: 488px;
margin-top: 6px;
z-index: 4;
color: red;
}
</style>
</head>
<body>
<div id="wrapper">
<div id="time">
<?php
echo date("j F, Y");
?>
</div>
<div id="links">
<img id="logo" src="logos/cr8logo34.png" width="600" height="192" alt="Company logo" />
<img id="navigation" src="images/nav.png" width="342" height="14" alt="Coloured navigation" />
<ul>
<li><a href="index.php">HOME</a></li>
<li><a href="index.php">GALLERY</a></li>
<li><a href="index.php">ABOUT</a></li>
<li><a href="contact.php">CONTACT</a></li>
</ul>
</div>
<div id="navline">
<div id="details">
<p style="font-weight: bold; font-size:12px">Management</p>
<p><strong>Tel:</strong> 1300 123 123 (9am - 5pm Mon - Fri)</p>
<p><strong>Fax:</strong> 1300 123 456</p>
<p><strong>Email:</strong> enquiries@company.com.au</p><br />
<p>After hours:<br /></p>
<p><strong>John Citizen</strong></p>
<p><strong>Tel:</strong> 0406 898 456</p>
</div> <!-- end of details div -->
<div id="contactimage">
<img src="images/contact.jpg" width="380" height="285" alt="Network of people connected" />
</div>
// Email validation include
<?php
include('emailvalidation.inc.php');
?>
// Email validation include
//Field validation code
<?php
if (isset($_POST['submit']))
{
//get form data
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$company = $_POST['company'];
$subject = $_POST['subject'];
$telephone = $_POST['telephone'];
$email = $_POST['email'];
$message = $_POST['message'];
$errorstring = ""; //default value for errorstring
if (!$fname)
$errorstring = $errorstring."[First name:] ";
if (!$lname)
$errorstring = $errorstring."[Last name:] ";
if (!$company)
$errorstring = $errorstring."[Company:] ";
if (!$subject)
$errorstring = $errorstring."[Subject:] ";
if (!$telephone)
$errorstring = $errorstring."[Telephone:] ";
if (!$email)
$errorstring = $errorstring."[Email:] ";
if (!$message)
$errorstring = $errorstring."[Message:] ";
if ($errorstring!="")
echo "<div id='please'>Please fill out the following fields: $errorstring</div>";
else
include('contact.inc.php'); [B]//Mail function include[/B]
}
?>
//Field validation code
<div id="form">
<form name="form" method="post" action="contact.php" >
First name: <input name="fname" type="text" value="<?php echo $fname; ?>" /><br/><br />
Last name: <input name="lname" type="text" value="<?php echo $lname; ?>" /><br/><br/>
Company: <input name="company" type="text" value="<?php echo $company; ?>" /><br/><br/>
Subject: <input name="subject" type="text" value="<?php echo $subject; ?>" /><br/><br/>
Telephone: <input name="telephone" type="text" value="<?php echo $telephone; ?>" /><br/><br/>
Email: <input name="email" type="text" id="email" value="<?php echo $email; ?>"/><br/><br/>
Enquiry: <textarea name="message" rows="5" cols="40"><?php echo $message; ?></textarea><br/><br/>
<a href="http://www.company.com/contact.php"><input type="reset" value="Reset" id="button2" name="reset" /></a>
<input type="submit" value="Send" id="button" name="submit" />
</form>
</div>
</div> <!-- end of navline div -->
</div> <!-- end of wrapper div -->
</body>
</html>[/LEFT]
Here is the code for the email validation which isn't working properly (I won't bother posting the code for the mail function as it works fine).
It says "Your email was sent successfully" as soon as you enter the page, and still sends if you type jibberish in the email field and click 'submit'.
If the email is invalid, I want it to warn the user and not send anything:
<?php
$email = $_POST['email'];
$errorString.= "<div id='please'>That is not a valid email address. Your email has not been sent.</div>";
function isValidInetaddress($data, $strict = false)
{
$regex = $strict?
'/^([.0-9a-z_-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i' :
'/^([*+!.&#$¦\'\\%\/0-9a-z^_`{}=?~:-]+)@(([0-9a-z-]+\.)+[0-9a-z]{2,4})$/i';
if (preg_match($regex, trim($data), $matches)) {
return array($matches[1], $matches[2]);
}
else {
return false;
}
}
if (isset($_POST['submit']))
{
if (!($_POST['email']) || !isValidInetaddress($_POST['email']))
{
echo $errorString;
}
}
else
include('contact.inc.php'); //M
?>