Forum Moderators: coopster
Ive got a PHP and JavaScript comment form and when i test it out i get this message: "Sorry, but you need to enter your name". This would be fine if i actually missed this field out but i didnt. This message is also coming from the PHP code instead of the JavaScript.
Heres the php:
<? //This line tells the server "PHP Code is Starting!"
//We need to get the values submitted to the form and save them.
// $blah = $_POST['email'];
// now $blah is set to whatever email is.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//These IFs check if everything is filled in.
if (!$email) { //if $email is blank and not filled in
echo "Sorry, but you need to enter your email"; //tell the user to fill in the appropriate fields
exit; //stop the PHP from continuing
}
// Validate Email Address String
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+$',$email) ) {
echo "Email field has invalid characters!";
exit;
}
if (!$name) {
echo "Sorry, but you need to enter your name";
exit;
}
if (!$message) {
echo "Sorry, but you need to say something to me";
exit;
}
//Now that we have checked that everything is filled in, LETS SEND THE EMAIL!
//We are going to use a simple function called mail().
// mail("EMAIL TO","NAME","MESSAGE","From: name <email>");
mail("FirstName.LastName@example.com", $name, "$email\n$message", "From: Contemporary Multimedia - Message <my@example.com>");
header ("Location: contactthanks.htm");
//echo "Your message has been sent, thankyou."; //now lets tell the user "your email was sent!"
// either have a thank you massage here, you can use any html you want to show them.
// or you can redirect them to a thank you page.
?>
Heres the form:
<script language="JavaScript" type="text/javascript">
<!--
function validateForm() {
with (document.comment) {
var alertMsg = "The following REQUIRED fields\nhave been left empty:\n";
if (name.value == "") alertMsg += "\nName";
if (email.value == "") alertMsg += "\nE Mail";
if (addcomment.value == "") alertMsg += "\nAdd Comment";
if (alertMsg!= "The following REQUIRED fields\nhave been left empty:\n") {
alert(alertMsg);
return false;
} else {
return true;
} } }
//-->
</script>
<form name="comment" onsubmit="return validateForm();" action="mailer.php" method="post">
<input type='hidden' name='formmail_submit' value='Y' />
<input type='hidden' name='formmail_recipient' value="FirstName.LastName@example.com" />
<input type='hidden' name='formmail_subject' value="Feedback Contemporary Multimedia" />
<input type='hidden' name='formmail_return_subject' value="Hi, Thanks for your Feedback" />
<input type='hidden' name='formmail_return_msg' value="This is a autoresponder
I have recieved your e-mail and will be in
touch with you soon." />
<input type='hidden' name='formmail_mail_and_file' value="" />
<input type='hidden' name='formmail_charset' value="" />
<input type='hidden' name='esh_formmail_cc' value="" />
<input type='hidden' name='esh_formmail_bcc' value="" />
<input type='hidden' name='esh_formmail_mail_and_file' value="" />
<input type='hidden' name='esh_formmail_charset' value="" />
<fieldset class="fieldset">
<!--<legend>Contact form</legend>-->
<label>Name</label><br class="comment" />
<input onclick="highlight(event)" onfocus="style.borderColor='#afeeee'" onblur="style.borderColor='#cccccc';" style="border:1px solid #cccccc;" type="text" name="Name" value="" class="textfield" />
<br />
<label>Email</label><br class="comment" />
<input name="email" onfocus="style.borderColor='#afeeee'" onblur="style.borderColor='#cccccc';" style="border:1px solid #cccccc;" type="text" value="" class="textfield" />
<br/>
<!-- class='form_text' -->
<label>Subject</label><br class="comment" />
<input name="subject" onfocus="style.borderColor='#afeeee'" onblur="style.borderColor='#cccccc';" style="border:1px solid #cccccc;" type="text" value="" class="textfield" />
<br/>
<br />
<label>Comment</label><br class="comment" />
<textarea name="message" class="textfield" onfocus="style.borderColor='#afeeee'" onblur="style.borderColor='#cccccc';" style="border:1px solid #cccccc;" rows="4" cols="25" ></textarea>
<br />
<br class="nobr" />
<input class="send" onmouseover="this.src='submit_ov.gif';" onmouseout="this.src='submit_ov.gif';" type="image" src="submit_ov.gif" alt=" GO! " />
<div class="break"> </div>
</fieldset>
</form>
Any help would be great.
Thanks
[edited by: coopster at 4:41 pm (utc) on Jan. 26, 2006]
[edit reason] generalized email [/edit]
And here is the updated php file:
<? //This line tells the server "PHP Code is Starting!"
//We need to get the values submitted to the form and save them.
// $blah = $_POST['email'];
// now $blah is set to whatever email is.
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
//These IFs check if everything is filled in.
if (!$email) { //if $email is blank and not filled in
echo "Sorry, but you need to enter your email"; //tell the user to fill in the appropriate fields
exit; //stop the PHP from continuing
}
// Validate Email Address String
if (!ereg('^[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+@[-!#$%&\'*+\\/0-9=?A-Z^_`a-z{¦}~]+\.[-!#$%&\'*+\\./0-9=?A-Z^_`a-z{¦}~]+$',$email) ) {
echo "Email field has invalid characters!";
exit;
}
if (!isset($name) ¦¦ empty($name)) {
echo "Sorry, but you need to enter your name";
exit;
}
if (!$message) {
echo "Sorry, but you need to say something to me";
exit;
}
//Now that we have checked that everything is filled in, LETS SEND THE EMAIL!
//We are going to use a simple function called mail().
// mail("EMAIL TO","NAME","MESSAGE","From: name <email>");
mail("me@example.com", $name, "$email\n$message", "From: Contemporary Multimedia - Message <my@email.com>");
header ("Location: contactthanks.htm");
//echo "Your message has been sent, thankyou."; //now lets tell the user "your email was sent!"
// either have a thank you massage here, you can use any html you want to show them.
// or you can redirect them to a thank you page.
?>
Maybe i should just remove some of the validation code for now as im using the JavaScript for that?
[edited by: jatar_k at 6:53 pm (utc) on Jan. 26, 2006]
[edit reason] no urls thanks [/edit]
But...
How come none of the javascript is working? For example if you now test it out again when missing a field it spots the mistake but doesnt bring up the javascript window its just the php that tells you.
I posted above the form and the javascript if that might help.