variable help needed

i am trying to display an error message if a client fails to select a service. although the code i have attached works, it only works by displaying the message way off the page in the top of the page. i would like it to appear in a place i designate more on the page. is this possible? many thanks
//From submitted form
 
$prez = $_POST['prez'];
 
 
 
//Need to error check before mailing
 
<?php
if (!isset($_POST['prez']) || (empty($_POST['prez'])==' '))
{
$error = '<span style="color:red">You must select a Service</span>';
}
else 
{
$to = "whoever@coldmail.com";
$subject = "Sales Enquiry";
$message = "You have an enquiry from $name for local services.\n\nName: $name\n\nAddress1: $address1\n\naddress2: $address2\n\nTown: $town\n\nPostcode: $postcode\n\nDescription: $description\n\nTelephone: $telephone\n\nContact By: $contact\n\nService: $prez";
$headers = "From: $email";
mail($to, $subject, $message, $headers); 
}
?>

Open in new window

peter_coopAsked:
Who is Participating?
 
TagorConnect With a Mentor Commented:
This should work. To view the error message anywhere in your HTML code add this:

<?php echo $error; ?>
//From submitted form
 
$prez = $_POST['prez'];
 
 
 
//Need to error check before mailing
 
<?php
if ($_POST['prez'] != "") {
$error = '<span style="color:red">You must select a Service</span>';
}
else {
$to = "whoever@coldmail.com";
$subject = "Sales Enquiry";
$message = "You have an enquiry from $name for local services.\n\nName: $name\n\nAddress1: $address1\n\naddress2: $address2\n\nTown: $town\n\nPostcode: $postcode\n\nDescription: $description\n\nTelephone: $telephone\n\nContact By: $contact\n\nService: $prez";
$headers = "From: $email";
mail($to, $subject, $message, $headers); 
}
?>

Open in new window

0
 
Ray PaseurCommented:
There is not enough code there to tell what is wrong. Can you please post the code for the ENTIRE pages, both the form and action? Thanks, ~Ray
0
 
Ray PaseurConnect With a Mentor Commented:
Maybe this will help show a "best practices" way to display the error messages.  Install it and run it to see how it works.   With a little styling, you can make this concept suitable for almost any web site.

Best regards, ~Ray
<?php // RAY_post_error_message_example.php
error_reporting(E_ALL ^ E_NOTICE);
 
 
 
// THIS IS THE ACTION SCRIPT - INITIALIZE THE ERROR MESSAGE
$errmsg = '';
 
// HAS ANYTHING BEEN POSTED?
if (!empty($_POST))
{
 
// TEST TO SEE IF THE FIELD CONTAINS THE RIGHT INFORMATION
   $myField = trim(strtoupper($_POST["myField"]));
   if ($myField == 'ABC')
   {
// THIS IS WHERE WE PROCESS THE GOOD INFORATION FROM THE FORM
      echo "<br/>Congratulations, you entered ABC\n";
      echo "<br/><a href=\"{$_SERVER["PHP_SELF"]}\">Try Again?</a>\n";
      die();
   } else
   {
// THIS IS WHERE WE CREATE - BUT DO NOT PRINT - THE ERROR MESSAGE
      $errmsg = "<br/>Sorry, you did not enter ABC<br/>\n";
   }
}
 
 
 
// THIS IS THE FORM SCRIPT
// IF NOTHING HAS BEEN POSTED, OR IF THERE WAS AN ERROR WE LAND HERE
?>
<form method="post">
<h2>Here is the form</h2>
 
<!-- IF THERE IS AN ERROR MESSAGE WE PRINT IT HERE -->
<?php echo $errmsg; ?>
 
Type the three letters 'ABC' here:
<input name="myField" />
<input type="submit" name="My_SUBMIT_Button" value="go" />
</form>

Open in new window

0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
peter_coopAuthor Commented:
@Ray_Paseur

Totally lost with your solution.

@Tagor

Your code does not work.

<?php echo $error ; ?> is blank on the page.

any other ideas?

many thanks for your input.
0
 
Ray PaseurCommented:
@peter_coop: You have some responsibility here at EE to at least try to understand the technologies that you are trying to use, and unless I am completely misreading your question this is an elementary issue.

The script I posted is VERY elementary - PHP 101.  Please forgive me for repeating myself, but "Install it and run it to see how it works."  Then post something in the form of a question - we can help if we work together on the issues.

Don't know why you did not post the complete scripts you are using, but if you do, that will be helpful, too.

Best regards, ~Ray
0
 
peter_coopAuthor Commented:
thx once again
0
 
Ray PaseurCommented:
Please read the grading guidelines here:
http://www.experts-exchange.com/help.jsp#hi403

I think you owe us an explanation why you marked the answers down to a "B" without any comment.  It would have been helpful if you had posted the code we asked for, and in the absence of that, it seems inappropriate to give an inferior grade.  It costs you nothing more to give the right grade according to the standards of EE.

You can ask a moderator to change the grade to "A" or you can explain why you believe, in the context of the EE grading system, that the answers did not deserve an "A".

Thanks for your consideration, ~Ray
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.