June 17, 201016 yr Hi guys, You may have seen me ask for observations on this site. Well, now i have a question that i can barely explain. Basically on my contact form when a user fills in correct data but fails at filling in a box, i get an error message for the correctly filled in box. For example, there is 3 boxes..name, email and message. Name requires at least 2 chars. email requires an @ sign and message requires 10 chars. If i fill in name as "jay" email as jayathotmail.com and the message as "hi this is over ten characters" i get this error notice: "Notice: Undefined variable: error1 in \\nas44ent\domains\j\jameswylie.co.uk\user\htdocs\Pigeons\contact.php on line 196 Notice: Undefined variable: error3 in \\nas44ent\domains\j\jameswylie.co.uk\user\htdocs\Pigeons\contact.php on line 196" basically its displaying because there is an error in "error 2" so i need a way to stop the message reporting. I know i can do it in the php.ini file, but i would have to contact my host, and in my experience its better to "fix" than to "pretend it isnt there". Sorry if i have not explained myself very well i am a complete noob when it comes to php. Feel free to try out the form yourself to get a better understanding of what i mean http://www.jameswylie.co.uk/pigeons/contact.php source code included below <div id="main"> <div id="caption"><h1>Contact Form</h1> <?php // Define your email address - where to send messages - here define("MAIL_TARGET","info@*********.co.uk"); // Here you can redefine error messages define("errorName","Invalid name! It must be at least 2 characters long"); define("errorEmail","Invalid email address!"); define("errorMsg","Invalid message! It must be at least 10 characters long"); function createForm($name="",$email="",$message="",$error1="",$error2="",$error3=""){ ?> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <div align="center"> <table> <tr><td><font face="Verdana" size="2">Name:</font></td><td class="error"><h5><?php echo $error1; ?></h5></td></tr> <tr><td colspan="2"><input class="text" type="text" name="name" value="<?php echo $name; ?>"></td></tr> <tr><td><font face="Verdana" size="2">Email:</font></td><td class="error"><h5><?php echo $error2; ?></h5></td></tr> <tr><td colspan="2"><input class="text" type="text" name="email" value="<?php echo $email; ?>"></td></tr> <tr><td><font face="Verdana" size="2">Message:</font></td><td class="error"><h5><?php echo $error3; ?></h5></td></tr> <tr><td colspan="2"><textarea cols="40" rows="6" name="message"><?php echo $message; ?></textarea></td></tr> <tr><td colspan="2"><br/><input class="text" type="submit" name="submitBtn" value="Send"></td></tr> </table> </div> </form> <?php } function isValidEmail($email){ $pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$"; if (eregi($pattern, $email)){ return true; } else { return false; } } function sendMail($name,$email,$message){ $subject = "Message from website"; $from = "From: $name <$email>\r\nReply-To: $email\r\n"; $header = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso-8859-1\r\n"; $content = htmlspecialchars($message); $content = wordwrap($content,70); mail(MAIL_TARGET,$subject,$content,$from.$header); } ?> </div> <div id="icon"> </div> <?php if (!isset($_POST['submitBtn'])) { createForm(); } else { $name = isset($_POST['name']) ? $_POST['name'] : ""; $email = isset($_POST['email']) ? $_POST['email'] : ""; $message = isset($_POST['message']) ? $_POST['message'] : ""; $error = false; if (strlen($name)<2) { $error = true; $error1 = errorName; } if (!isValidEmail($email)) { $error = true; $error2 = errorEmail; } if (strlen($message)<10) { $error = true; $error3 = errorMsg; } if ($error){ createForm($name,$email,$message,$error1,$error2,$error3); } else { sendMail($name,$email,$message); ?> <div id="result"> <table width="100%"> <tr><td> <h4 align="center">Thanks for your message! We will reply as quick as possible, but it could take up to 24 hours.</h4> </td></tr> </table> </div> <?php } } ?> <div>
June 17, 201016 yr Hi After a very quick look, you're missing the dollar signs for the variables here if (strlen($name)<2) { $error = true; $error1 = errorName; } if (!isValidEmail($email)) { $error = true; $error2 = errorEmail; } if (strlen($message)<10) { $error = true; $error3 = errorMsg; } prepend errorName, errorEmail and errorMsg with a $. There may be more but fix those first.
June 17, 201016 yr Author so basically this: if (strlen($name)<2) { $error = true; $error1 = errorName; } if (!isValidEmail($email)) { $error = true; $error2 = errorEmail; } if (strlen($message)<10) { $error = true; $error3 = errorMsg; } becomes this if (strlen($name)<2) { $error = true; $error1 = $errorName; } if (!isValidEmail($email)) { $error = true; $error2 = $errorEmail; } if (strlen($message)<10) { $error = true; $error3 = $errorMsg; } just to clarify thankyou very much
June 17, 201016 yr Author sadly not i now get : Notice: Undefined variable: errorName in \\nas44ent\domains\j\jameswylie.co.uk\user\htdocs\Pigeons\contact.php on line 185 <--- this when the name field was less than 2 chars. Also stops the user message that should come up saying "please enter more than 2 characters" or something along that line. Notice: Undefined variable: error2 in \\nas44ent\domains\j\jameswylie.co.uk\user\htdocs\Pigeons\contact.php on line 197 <---- email format was entered correctly Notice: Undefined variable: error3 in \\nas44ent\domains\j\jameswylie.co.uk\user\htdocs\Pigeons\contact.php on line 197 <---- message format was entered correctly any suggestions? the updated page is live on http://www.jameswylie.co.uk/pigeons/contact.php
June 17, 201016 yr Thats becasue there is no variable with those names ... Constants dont need a $. So i dont think that would fix anything... I am thinking on what could cause it and i will update this post... I am a PHP novice.. but i will try to help. These variables $errorName $errorEmail $errorMsg dont need the $, because they are constants defined at the top Can you tell me which line is line 197?
June 17, 201016 yr I'm guessing you're passing undefined variables to the createForm function if $error is true. Underneath your definition of $error = false; put... $error1 = $error2 = $error3 = ''; That will define the variables as empty strings.
June 17, 201016 yr Author I'm guessing you're passing undefined variables to the createForm function if $error is true. Underneath your definition of $error = false; put... $error1 = $error2 = $error3 = ''; That will define the variables as empty strings. removed the $ as requested and added your line of code and you sir are a god among men (at least to me) That fixed it and now i have fully working contact form! Thanks to everybody for helping me, this forum is really a great source of help! A massive thanks to you guys..can i rep on here? One more quick simple question.. The received email looks empty, it only has the message content and not the name or email - anyway i can include the name and email in the body of the email? I cant see anything that resembles a print function.. Not a biggie, but would just like to make it easier for my client to get data..many thanks
June 17, 201016 yr put them in the content variable in your sendMail function, just before you word wrap it. e.g $content = htmlspecialchars($message)."\n\r"; $content .= "---------\n\r"; $content .= "From: ".$name."\n\r"; $content .= "Email: ".$email."\n\r"; *untested
June 17, 201016 yr Author it works, but has no line breaks its all on one line.. "hi, this a test.--------- From: jay Email: jay@testemail.co.uk "
June 18, 201016 yr Try \r\n for a line break instead, or seeing as your sending it as an HTML message, you could format it using HTML or try removing the html header and sending it as plain text.
June 18, 201016 yr These variables $errorName $errorEmail $errorMsg dont need the $, because they are constants defined at the top haha quite correct, completely missed that! Will teach me to read the code properly before jumping in
June 18, 201016 yr Author np milky, thanks for trying though when i add the code i get "Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in \\nas44ent\domains\j\jameswylie.co.uk\user\htdocs\Pigeons\contact.php on line 164" here is the updated code: $subject = "Message from website"; $from = "From: $name <$email>\r\nReply-To: $email\r\n"; $header = "MIME-Version: 1.0\r\n"."Content-type: text/html; charset=iso8859-1\r\n"; $content = htmlspecialchars($message)"\n\r"; $content .= "---------\n\r"; $content .= "From: ".$name."\n\r"; $content .= "Email: ".$email."\n\r";
June 18, 201016 yr you need a dot to concatenate the htmlspecialchars function and the linebreak string. $content = htmlspecialchars($message)."\n\r";
June 19, 201016 yr Author fixed the problem, but still not getting the email contents on seperate lines. thanks for the info jock
June 19, 201016 yr There is some issue with php mail, line breaks and windows servers, probbaly worth googling to see if there is a solution. It might actually be inserting line breaks but because you're sending it as html they are not showing when parsed by the mail client. You should try <br/> tags.
June 23, 201016 yr fixed the problem, but still not getting the email contents on seperate lines. thanks for the info jock If ur sending the email as html u need to use <br /> tags not \n
June 28, 201016 yr Author hi guys, rather than start a new thread, i have one more question about this form. when it submits to itself, the "message sent successfully blah blah" is at the bottom of the page. Is there anyway i can either have the page redirect to a thankyou.html page or just display the success text at the top, rather than the bottom of the page?
June 28, 201016 yr if you're sending before headers have been sent you can use... header('Location: /thankyou.html'); or if you've already started outputting HTML, as it looks like you are from the code above, you could output some javascript to redirect them, although you're much better using PHP to modify the headers. echo "<script>window.location = '/thankyou.html';</script>";
Create an account or sign in to comment