Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Hard to explain but need way to stop php error message displaying

Featured Replies

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>

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.

  • 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

  • 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

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?

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.

  • 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) :D

 

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 :)

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

  • Author

it works, but has no line breaks

 

its all on one line..

 

"hi, this a test.--------- From: jay Email: jay@testemail.co.uk "

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.

 

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 blush.gif

  • 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";

  • Author

fixed the problem, but still not getting the email contents on seperate lines.

 

thanks for the info jock :)

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.

  • 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?

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

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.