Solved

variable help needed

Posted on 2009-04-11
7
171 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
  • 4
  • 2
7 Comments
 
LVL 109

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 109

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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

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 109

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 109

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

815 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now