E-mail form error message

Associate
Joined
13 Jan 2007
Posts
2,424
Location
Belfast,Northern Ireland
I have this working almost how I want, just need to add in some validation for the text areas. However there is a problem that exists, when you first go to the page you will see a feedback message despite having done nothing...I dont understand why this is happening or how to remove it.

www.warfinder.co.uk

Code:
<div class="small-content-middle">
               <form id="form_message" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
 <input type="text" name="name" value="Enter Name"> <br /><br />
 <textarea rows="6" cols="24"  name="message">Enter Your Message
 </textarea>
<br/><br/><?php
$error_msg = "hello";
$to = "[email protected]";
$name = $_POST['name'];
$subject = "Queen's Radio Message From " . $name;
$txt = $_POST['message'];

if (mail($to,$subject,$txt))
{
	$error_msg = '<font color="#FF0000">*Message Sent</font>';
}
else
{
	$error_msg = '<font color="#FF0000">*Message Failed</font>';
}
echo $error_msg ?><br/>
<input type="submit" value="Submit">
</form>


                   </div>
 
What you want to do mate is to wrap
Code:
if (isset($_POST['submit']))
{

}

around your mail if and else condition. basically means it won't do anything untill that submit button has been pressed.
 
mail() is getting executed all the time rather being only executed when the submit button gets pressed.
Wrap your current mail code with another IF statement and create a condition to either check for REQUEST_METHOD (will become 'POST' on form submit) or add a name tag to your submit and check the post value of that.

Edit - As UncleRuckus said....
 
PHP:
if ($_SERVER['REQUEST_METHOD'] == 'post')
{
 // Your code here
}

As stated above.

This is the best way because the other method relies on the key 'submit' being present in the request. If you don't click the submit button but instead hit 'return' in a form field, certain browsers will post the form but not pass along the 'submit' key from the button. This problem can be solved with a hidden input field but the solution above is a bit simpler.
 
Have I understood this correctly as it is now not working. Im not getting the email and no feedback message ever occurs

Code:
<form id="form_message" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
 <input type="text" name="name" value="Enter Name"> <br /><br />
 <textarea rows="6" cols="24"  name="message">Enter Your Message
 </textarea>
<br/><br/><?php
$error_msg = "";
$to = "[email protected]";
$name = $_POST['name'];
$subject = "Queen's Radio Message From " . $name;
$txt = $_POST['message'];

if ($_SERVER['REQUEST_METHOD'] == 'post')
{
	if (mail($to,$subject,$txt))
	{
	$error_msg = '<font color="#FF0000">*Message Sent</font>';
	echo $error_msg;
	}
	else
	{
	$error_msg = '<font color="#FF0000">*Message Failed</font>';
	
	}
} 
echo $error_msg;
 ?><br/>
<input type="submit" value="Submit">
</form>
 
Sorry, FabienO's example has a slight mistake, 'post' needs to be in upper case ('POST'). See the corrected example below:

PHP:
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
 // Your code here
}
 
Sorry really should have caught that myself and did wonder but assumed it had to match the method type.

Still having issues with my attempt at validation. It works correctly in terms of function but I dont want it to send if the user leaves the fields empty. Will use JS to clear the boxes on click soon enough.

Code:
<form id="form_message" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
 <input type="text" name="name" value="Enter Name"> <br /><br />
 <textarea rows="6" cols="24"  name="message">Enter Your Message
 </textarea>
<br/><br/><?php
$error_msg = "";
$to = "[email protected]";
$name = $_POST['name'];
$subject = "Queen's Radio Message From " . $name;
$txt = $_POST['message'];
$valid = false;

if ($name != "" && $name != "Enter Name" && $txt != "" && $txt != "Enter Your Message")
{
	$valid = true;
}
else
{
	$valid = false;
}

if ($valid = true && $_SERVER['REQUEST_METHOD'] == 'POST'){
		if (mail($to,$subject,$txt))
		{
		$error_msg = '<font color="#FF0000">*Message Sent</font>';
		}
		else
		{
		$error_msg = '<font color="#FF0000">*Message Failed</font>';	
		}
	} 
else if ($valid = false && $_SERVER['REQUEST_METHOD'] == 'POST')
{
	$error_msg = '<font color="#FF0000">*Missing Information</font>';
}
echo $valid;
echo $error_msg;
 ?><br/><br/>
<input type="submit" value="Submit">
</form>
 
This is the best way because the other method relies on the key 'submit' being present in the request. If you don't click the submit button but instead hit 'return' in a form field, certain browsers will post the form but not pass along the 'submit' key from the button. This problem can be solved with a hidden input field but the solution above is a bit simpler.

I'm curious here Rob, which browsers?

As I understood it, if someone did hit enter it would pass it empty but would still be set.
 
Refer to below post.

Keep in mind lengthening your code for easier debugging and things like empty() and isset()
 
Last edited:
Code:
<?php 
$to = "[email protected]";
$name = $_POST['name'];
$subject = "Queen's Radio Message From " . $name;
$txt = $_POST['message'];
$valid = false;
$error_msg = "";

if (!empty($name) && $name != "Enter Name" && !empty($txt) && $txt != "Enter Your Message")
{
  $valid = true;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  if ($valid == true) 
    {
      $send_mail = mail();
      if ($send_mail)
      {
        $error_msg = '*Message Sent';
      } else {
         $error_msg = '*Message failed';
      }
    } else {
      $error_msg = 'Please fill in all the fields';
    }
} 
?>

<form id="form_message" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
  <input type="text" name="name" value="Enter Name"> <br /><br />
  <textarea rows="6" cols="24"  name="message">Enter Your Message</textarea>
  <input type="submit" value="Submit">
  <?php if(!empty($error_msg)): ?><p class="form_error"><?php echo $error_msg; ?></p><?php endif; ?>
</form>

If you wish to extend security. Check for a valid email, there's many ways to do this. here's a simple one. http://www.w3schools.com/php/filter_validate_email.asp
Have it throw an "Incorrect email" error if this returns false.

Also I noticed you haven't completed that slider yet. If you would like a good tool for doing that check out jQuery Tools. tripnologist pointed me to them, and they're great.

Edit: Removed some of the HTML from the PHP. Always try to keep this as separate as possible. Style the p.form_error {} in your CSS
 
Last edited:
Thank you fabien, that's finally working as it should, I hate wasting times on things I should be solving in a few minutes. I guess I am less familiar with php than I thought which aint good!

Yeah I know of jquery tools, luckily i've done several sliders before and tabbed windows so im hoping (fingers crossed) it aint too much of a pain in the arse. Problem will be linking it will images from a db but i cant imagine that being much of an issue
 
btw you're curious. And i'm not 100% myself because i'm self taught PHP, but the issue with yours was:

if ($valid = etc

should have been

if ($valid == etc
 
heh. It's not that bad, I didn't know it was the reason till I re-wrote the code and saw the difference. Sometimes it's just about having fresh eyes find the missing , or something like that. If you stare at your own code for long enough, it all kinda just blurs.

Best thing to do is give yourself a small break from it, come back a fresh.
 
Sometimes it's just about having fresh eyes find the missing , or something like that. If you stare at your own code for long enough, it all kinda just blurs.

Best thing to do is give yourself a small break from it, come back a fresh.
These are pearls of wisdom.

If I has a pound for every time I've been stuck and only needed a break so I can look at it with fresh eyes, I would be as rich as a web developer :D
 
Code:
<?php 
$to = "[email protected]";
$name = $_POST['name'];
$subject = "Queen's Radio Message From " . $name;
$txt = $_POST['message'];
$valid = false;
$error_msg = "";

if (!empty($name) && $name != "Enter Name" && !empty($txt) && $txt != "Enter Your Message")
{
  $valid = true;
}

if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  if ($valid == true) 
    {
      $send_mail = mail();
      if ($send_mail)
      {
        $error_msg = '*Message Sent';
      } else {
         $error_msg = '*Message failed';
      }
    } else {
      $error_msg = 'Please fill in all the fields';
    }
} 
?>

<form id="form_message" method="post" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
  <input type="text" name="name" value="Enter Name"> <br /><br />
  <textarea rows="6" cols="24"  name="message">Enter Your Message</textarea>
  <input type="submit" value="Submit">
  <?php if(!empty($error_msg)): ?><p class="form_error"><?php echo $error_msg; ?></p><?php endif; ?>
</form>

If you wish to extend security. Check for a valid email, there's many ways to do this. here's a simple one. http://www.w3schools.com/php/filter_validate_email.asp
Have it throw an "Incorrect email" error if this returns false.

Also I noticed you haven't completed that slider yet. If you would like a good tool for doing that check out jQuery Tools. tripnologist pointed me to them, and they're great.

Edit: Removed some of the HTML from the PHP. Always try to keep this as separate as possible. Style the p.form_error {} in your CSS


Quick question, how come you've spattered it into two IF statements?
I can't see any reasoning over sticking everything in one IF statement/condition....
Code:
if (!empty($name) && $name != "Enter Name" && !empty($txt) && $txt != "Enter Your Message" && $_SERVER['REQUEST_METHOD'] == 'POST') {
//Mail Code Here
}
 
No real reason. I was using his code as much as possible, completely rewriting it in my own wouldn't really be worth learning from.

Typically I avoid nesting too deep as it looks ugly. but I am self-taught, I don't know what I am doing half the time. If I ever get employed, I am hoping they teach me right from wrong.
 
Last edited:
Code:
if (!empty($name) && $name != "Enter Name" && !empty($txt) && $txt != "Enter Your Message")
{
  if ($_SERVER['REQUEST_METHOD'] == 'POST')
  {
      $send_mail = mail();
      if ($send_mail)
      {
        $error_msg = '*Message Sent';
      } else {
         $error_msg = '*Message failed';
      }
  } 
} else {
  $error_msg = 'Please fill in all the fields';
}

Probably makes more sense.
 
Back
Top Bottom