Log in

View Full Version : Resolved PHP form Validation



john0611
05-10-2009, 03:05 PM
Hi all,

I have a problem i've been tryin to workout?

Here is the syntax:


<?php

// process the email
if (array_key_exists('submit', $_POST)) {
ini_set("sendmail_from", "info@test.com");
$to = 'info@test.com'; // use your own email address
$subject = 'Feedback from website';

// list expected fields
$expected = array('name', 'email', 'tel', 'from', 'dep');
// set required fields
$required = array('name', 'email', 'tel');
// create empty array for any missing fields
$missing = array();

// process the $_POST variables
foreach ($_POST as $key => $value) {
// assign to temporary variable and strip whitespace if not an array
$temp = is_array($value) ? $value : trim($value);
// if empty and required, add to $missing array
if (empty($temp) && in_array($key, $required)) {
array_push($missing, $key);
}
// otherwise, assign to a variable of the same name as $key
elseif (in_array($key, $expected)) {
${$key} = $temp;
}
}

// go ahead only if all required fields OK
if (empty($missing)) {
// build the message
$message = "Name: $name\n\n";
$message .= "Email: $email\n\n";
$message .= "Contact No.: $tel\n\n";
$message .= "Arrive: $from\n\n";
$message .= "Depart: $dep\n\n";


// limit line length to 70 characters
//$message = wordwrap($message, 70);

// send it
$sendmail_from = mail($to, $subject, $message);
if ($sendmail_from) {
// $missing is no longer needed if the email is sent, so unset it
unset($missing);
}
}
}
?>
<?php
if ($_POST && isset($missing)) {
?>
<p class="main_warning">Please complete the missing item(s) indicated. Your message has not been sent.</p>
<?php
}
elseif ($_POST && !$sendmail_from) {
?>
<p class="main_warning">Sorry, there was a problem sending your message. Please try later.</p>
<?php
}
elseif ($_POST && $sendmail_from) {
?>

// form begins

<span class="confmsg"><strong>Thank you for your enquiry. We will contact you &amp; confirm your booking.</strong></span>
<?php } ?>
<form id="topnavform" name="topnavform" method="post" action="">
<label id="cal"><abbr title="Enter the date when you will be arriving">From:</abbr>
<input type="text" name="from" id="from" maxlength="10" />
<a href="javascript:showCal('Calendar1')"><img src="img/calimg.jpg" width="17" height="17" alt="Calendar" title="Calendar" /></a></label>
<?php
if (isset($missing) && in_array('name', $missing)) { ?>
<span class="warning">Enter your name.</span>
<?php } ?>
<label><abbr title="Enter your name."><font color="#FF0000">*</font>Name:</abbr>
<input type="text" name="name" id="name" maxlength="35" />
</label>
<label id="calb"><abbr title="Select the date when you will be departing.">To:</abbr>
<input type="text" name="dep" id="to" maxlength="10" />
<img src="img/calimg.jpg" width="17" height="17" alt="Calendar" title="Calendar" /> </label>
<?php
if (isset($missing) && in_array('email', $missing)) { ?>
<span class="warning">Enter your email address.</span>
<?php } ?>
<label><abbr title="Enter your email address."><font color="#FF0000">*</font>Email:</abbr>
<input type="text" name="email" id="email" maxlength="40"/>
</label>
<?php
if (isset($missing) && in_array('tel', $missing)) { ?>
<span class="warning">Enter your contact no.</span>
<?php } ?>
<label><abbr title="Enter your contact no."><font color="#FF0000">*</font>Tel:</abbr>
<input type="text" name="tel" id="tel" maxlength="25" />

<input type="submit" name="submit" id="submit" class="submit" value="Send" title="Send" />
<input type="reset" name="reset" id="reset" class="reset" value="Reset" title="Reset" />
</label>
</form>
</div>

All working fine, but I'm trying to add a validation in the email field (@ must be present in the filed), so that visitors cannot enter bogus emails.

You help and suggestions are much appreciated, thank you

forum_amnesiac
05-10-2009, 03:40 PM
This is one way to validate the email address:-


if(!eregi("^[_&a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+
(.[a-z0-9-]+)*(.[a-z]{2,4})$", $email)) {

echo 'Invalid format of email address';

}
Replace '$email' with the field name you are using, if it fails then return to the "Enter Email Address" input.

However, this a request that has been made many times on this forum so a search should turn up many other examples for you.