Solved

variable help needed

Posted on 2009-04-11
7
169 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:peter_coop
Comment Utility
@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 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Comment Utility
thx once again
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

10 Experts available now in Live!

Get 1:1 Help Now