Hi,
Complete newbie here, please don't laugh 🙂
I have written a "contact me" page, and everything works fine, if all the fields are filled, the form emails me, if all of the fields are empty, it raises alerts and goes back, if one field is empty it raises the alert, etc.
However, the problem I am having is that the form actually refreshes if the name and email fields are full, but the message field is empty. The same doesn't happen if the message and name fields are full, but not the email.
This leads me to think there is something wrong with the way I am evaluating this
if(empty($contact_name) || empty($contact_email) || empty($contact_message)) {
as the || op is left precedent
but I could be wrong, or the might be something wrong with my code. Can someone help me find the mistake? It would be very much appreciated, as I have spent all day looking at it, and my eyes are square!
<html>
<head>
<script language="JavaScript">
function emailvalidation(entered, alertbox)
{
with (entered)
{
apos=value.indexOf("@");
dotpos=value.lastIndexOf(".");
lastpos=value.length-1;
if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2)
{if (alertbox) {alert(alertbox);} return false;}
else {return true;}
}
}
function emptyvalidation(entered, alertbox)
{
with (entered)
{
if (value==null || value=="")
{if (alertbox!="") {alert(alertbox);} return false;}
else {return true;}
}
}
function formvalidation(thisform)
{
with (thisform)
{
if (emptyvalidation(FullName,"Please enter your name")==false) {FullName.focus(); return false;};
if (emptyvalidation(Subject,"Please enter a message")==false) {Suject.focus(); return false;};
if (emailvalidation(Email,"Please enter a VALID email address")==false) {Email.focus(); return false;};
}
}
</script>
</head>
<body>
<div class="boxcontent">
<h1>Thanks for your comment</h1>
<?php
// declare values
$contact_email = $_POST['Email'];
$contact_name = $_POST['FullName'];
$contact_message = $_POST['Subject'];
$mydate = date ( 'l, F d Y g:i A',time()+240 );
// where to send e-mail to
$to = 'adrian_trythall@hotmail.com';
// e-mail subject
$subject = "Message submitted using Contact Us form";
// e-mail message
$message = "You have received a contact message:\r\n"
."----------------------------------------------------------------\r\n"
."Contact Name: $contact_name\r\n"
."Submitted: $mydate\r\n"
."From IP: {$_SERVER['REMOTE_ADDR']}\r\n\r\n"
."Message: $contact_message\r\n"
."Form Address: {$_SERVER['SERVER_NAME']}{$_SERVER['REQUEST_URI']}";
$headers = "From: $contact_name <$contact_email>\n"
."Reply-To: $contact_email\n"
."X-Mailer: PHP/".phpversion();
// check for validation, then send the e-mail
if(empty($contact_name) || empty($contact_email) || empty($contact_message)) {
echo '<p>Send us a message, enter your information below and click \'Submit\'!</p>
<form method="post" action="" onSubmit="return formvalidation(this)">
<table id="Form-Details">
<tbody>
<tr><td>Name:</td><td><input type="text" name="FullName" size="20" /></td><tr/>
<tr><td>Email:</td><td colspan="3"><input type="text" name="Email" size="20" /></td></tr>
<tr><td colspan="4">Message:</td></tr>
<tr><td colspan="4"><textarea rows="6" name="Subject" cols="47" class="input"></textarea></td></tr>
<tr><td colspan="4" class="right1"><input type="submit" value="Submit" /><input type="reset" value="Reset" /></td></tr>
</tbody>
</table>
</form>';
} else {
mail( $to, $subject, $message, $headers );
echo "<h3>Message Sent!</h3><p>Dear $contact_name,<br /><br />We will get back to you as soon as possible using $contact_email.";
}
?>
</div>
</body>
</html>