After bending over backwards, I managed to finally get my form validation done, which seems working perfectly well.
After failing to work with e-mail, I wonder if anyone around could help me little towards e-mail so that as soon as someone presses "Submit" button, I get the entire form's message sent to me through an e-mail.
I've typed the e-mail related code right at the bottom of the form, although, I disabled the code so at least you can see the form since right now, it's showing some error message with the use of curly bracket ({).
I guess this e-mail related code should go somewhere up around the comment:
"// If there's NO error at all, along with displaying the filled fields"
I've typed in the comment all over the form, so as to make it easier to read & understand & knowing what's happening where.
I wish to receive the comment submitted on "j4mes_bond25@yahoo.co.uk".
Following is my "contact.php" code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Personal Website</title>
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<?php // include("menu.inc");?>
<div id="centerContent">
<p>
<?php
// If the form has been posted, analyse it:
if ($_POST) {
foreach ($_POST as $field => $value) {
$value = trim($value);
}
// Creating Variables
$inquiry=$_POST['inquiry'];
$title=$_POST['title'];
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$message=$_POST['message'];
$reply=$_POST['reply'];
$contact=$_POST['contact'];
// Create empty ERROR variables
$error = ""; // for fields left BLANK
$errorflag = ""; // for fields with INVALID data entered
// Check for field/fields that is/are left BLANK
if (($first_name == "") || ($last_name == "") || ($email == "") || ($phone == "") || ($message == "")) {
$error = "Please fill in all fields!";
}
else {
// Validate First Name (including ERRORS such as (1) field left BLANK (2) field with INVALID data entered
if (ctype_alpha($first_name) == FALSE) {
$error = "Please enter a valid name <span class='italic'>(Alphabets only)</span>";
$errorflag= "first_name";
}
// Validate Last Name (including ERRORS such as (1) field left BLANK (2) field with INVALID data entered
else if (ctype_alpha($last_name) == FALSE) {
$error = "Please enter a valid last name <span class='italic'>(Alphabets only)</span>";
$errorflag="last_name";
}
// Validate E-mail (including ERRORS such as (1) field left BLANK (2) field with INVALID data entered
else if ((strpos($email, "@") == FALSE)||
(strpos($email, ".") == FALSE) ||
(strpos($email, " ") != FALSE)) {
$error = "Please enter a valid e-mail address";
$errorflag="email";
}
// Validate Contact No. (including ERRORS such as (1) field left BLANK (2) field with INVALID data entered
else if (is_numeric($phone) == FALSE) {
$error = "Please enter a valid contact number <span class='italic'>(must contain numbers only)</span>";
$errorflag="phone";
}
}
// Confirmation Message seen AFTER filling the form and pressing "Submit" button (whether there's an error or not)
if ($error != "") { echo "<b><span class='colorText'>Error Occured: </b>" . $error."</span>" ; } // If there's an error along with displaying the list of flagged error/errors
// If there's NO error at all, along with displaying the filled fields
else { echo "<p>Thanks for your comment and time. We will be in touch with you shortly.<br/><br/>";
echo "<b>Nature of Inquiry:</b> ". $inquiry . "<br/>";
echo "<b>Title:</b> ". $title . "<br/>";
echo "<b>First Name:</b> ". $first_name . "<br/>";
echo "<b>Last Name:</b> ". $last_name . "<br/>";
echo "<b>E-mail:</b> ". $email . "<br/>";
echo "<b>Contact No.:</b> ". $phone . "<br/>";
echo "<b>Message:</b> ". $message . "<br/>";
echo "<b>Reply:</b> ". $reply . "<br/>";
echo "<b>Contact Method:</b> ". $contact . "<br/></p>";
}
}
// DON'T KNOW WHY THERE'S "ELSE" CODE HERE
else {
$inquiry = "";
$title = "";
$first_name = "";
$last_name = "";
$email = "";
$phone = "";
$message = "";
$reply = "";
$contact = "";
$errorflag = "";
}
?>
</p>
<p class="first-letter">Please fill the following form in for any enquiries that you may have:</p>
<table id="contactTable">
<tr id="contactTr">
<td id="contactTd"><form method="post" action="<?php echo $_SERVER['PHP_SELF'] ?>">
Nature of Enquiry:</td>
<td id="contactTd"><select name="inquiry">
<option <?php if ($inquiry == "General Inquiry") echo "Selected"; ?> value = "General Inquiry">General Inquiry</option>
<option <?php if ($inquiry == "Price Quotation") echo "Selected"; ?> value = "Price Quotation">Price Quotation</option>
<option <?php if ($inquiry == "Comments") echo "Selected"; ?> value = "Comments">Comments</option>
<option <?php if ($inquiry == "Other") echo "Selected"; ?> value = "Other">Other</option>
</select></td>
</tr>
<tr id="contactTr">
<td id="contactTd">Title:</td>
<td id="contactTd"><select name="title"/>
<option <?php if ($title == "Mr") echo "Selected"; ?> value = "Mr">Mr</option>
<option <?php if ($title == "Miss") echo "Selected"; ?> value = "Miss">Miss</option>
<option <?php if ($title == "Ms") echo "Selected"; ?> value = "Ms">Ms</option>
<option <?php if ($title == "Mrs") echo "Selected"; ?> value = "Mrs">Mrs</option>
<option <?php if ($title == "Other") echo "Selected"; ?> value = "Other">Other</option>
</select></td>
</tr>
<tr id="contactTr">
<td id="contactTd"><?php if ($errorflag == "first_name") { echo "<span class='colorText'>First Name:</span>"; }
else { echo "First Name:"; } ?></td>
<td id="contactTd"><input type="text" size="30" maxlength="30" name="first_name" value="<?php echo $first_name; ?>"/></td>
</tr>
<tr id="contactTr">
<td id="contactTd"><?php if ($errorflag == "last_name") { echo "<span class='colorText'>Last Name:</span>"; }
else { echo "Last name:"; } ?></td>
<td id="contactTd"><input type="text" size="30" maxlength="30" name="last_name" value="<?php echo $last_name; ?>"/></td>
</tr>
<tr id="contactTr">
<td id="contactTd"><?php if ($errorflag == "email") { echo "<span class='colorText'>E-mail:</span>"; }
else { echo "E-mail:"; } ?></td>
<td id="contactTd"><input type="text" size="30" maxlength="30" name="email" value="<?php echo $email; ?>"/></td>
</tr>
<tr id="contactTr">
<td id="contactTd"><?php if ($errorflag == "phone") { echo "<span class='colorText'>Contact No.:</span>"; }
else { echo "Contact No.:"; } ?></td>
<td id="contactTd"><input type="text" size="30" maxlength="20" name="phone" value="<?php echo $phone; ?>"/></td>
</tr>
<tr id="contactTr">
<td id="contactTd">Message:</td>
<td id="contactTd"><textarea rows="10" cols="50" wrap="physical" name="message"/><?php echo $message; ?>
</textarea></td>
</tr>
<tr id="contactTr">
<td id="contactTd">Reply Required:</td>
<td id="contactTd"><input type="radio" name="reply" value="Yes" checked="checked"/>Yes
<input type="radio" name="reply" value="No" <?php if ($reply="No") echo "checked='checked'"; ?>/>No</td>
</tr>
<tr id="contactTr">
<td id="contactTd">How would you like to be contacted (if required)?<br/><br/></td>
<td id="contactTd"><input type="radio" name="contact" value="Email" checked="checked"/>E-mail
<input type="radio" name="contact" value="Telephone" <?php if ($contact="Telephone") echo "checked='checked'"; ?>/>Telephone
</td>
</tr>
<tr id="contactTr">
<td id="contactTd"></td>
<td id="contactTd"><input type="reset" name="reset" value="Reset">
<input type="submit" name="submit" value="Submit"></td>
</form>
</tr>
</table>
<?php
/*
if ($_POST) {
if (mail("j4mes_bond25@yahoo.co.uk", $_POST['inquiry'], stripslashes($_POST['message'], "From: " .$_POST['email']."r\n")) {
echo "E-mail sent successfully";
}
elseif {
echo "E-mail NOT sent";
}
}
*/
?>
</div>
</body>
</html>