Forum Moderators: coopster

Message Too Old, No Replies

PHP & JavaScipt Form

error message

         

WillisTi

2:32 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Hi,

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">&nbsp;</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]

whoisgregg

3:18 pm on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Instead of :
if (!$name) {

You should test using something like this:

if ( (!isset($name) ) ¦¦ ( $name== '' ) ) {

Edit: don't mind me, still getting my caffeine for the morning. :)

WillisTi

5:12 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Ok that brings up a parse error.

whoisgregg

5:24 pm on Jan 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, WebmasterWorld turns pipes into "¦" so you need to replace those with the Shift-\ character on your keyboard.

WillisTi

5:56 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Thanks i thought it looked a bit weird.

Still no luck though comes up with the same message "Sorry, but you need to enter your name". I dont think im ever going to get this to work.

jatar_k

6:08 pm on Jan 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



as opposed to == '' try using empty()

if (!isset($name) ¦¦ empty($name)) {

remember to change for real pipe chars and see what that does

also are you sure it comes through empty?

you could also do
trim($name);

before the check to make sure there is no extra whitespace

WillisTi

6:50 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Still comes up with same message.

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]

jatar_k

6:54 pm on Jan 26, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



um

$name = $_POST['name'];

that won't work because your field is actually called "Name"

this should

$name = $_POST['Name'];

WillisTi

7:11 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



thankyou very much jatar-k for spotting that i didnt realise. It now works.

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.

WillisTi

7:33 pm on Jan 26, 2006 (gmt 0)

10+ Year Member



Actually i'll take that question to the JavaScript section.

Thanks everyone for your help.