Solved

variable help needed

Posted on 2009-04-11
7
173 Views
Last Modified: 2012-05-06
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

0
Comment
Question by:peter_coop
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24122798
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
 
LVL 4

Accepted Solution

by:
Tagor earned 250 total points
ID: 24122903
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
 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 24123008
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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

Author Comment

by:peter_coop
ID: 24123423
@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
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24123457
@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
 

Author Closing Comment

by:peter_coop
ID: 31569210
thx once again
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24123967
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

Featured Post

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question