Solved

if then statement within html email message

Posted on 2012-04-07
4
308 Views
Last Modified: 2012-06-21
I have a form which collects a user’s area code, exchange, station, and optional extension number. These variables are then used in an html mail script which emails those 3 or four fields to me. My mail script works fine as follows:

$to  = 'myname@mydomain.com';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: my namee <myname@mydomain.com>' . "\r\n";
$subject = 'The subject line';
$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<table>
<tr>
<td>Telephone:</td><td>".$areacode."-".$exchange."-".$station." ".$_POST['extension']."</td></tr>
</table>
</body>
</html>
";
mail($to, $subject, $message, $headers);

But, because the $_POST['extension'] variable may not contain any characters, I thought I’d dress-up my script by writing an “if-then” statement that says something like
 if (strlen($_POST['extension']) > 0 then the output should be
areacode-exchange-station ext nnn (where nnn is the optional extension number that may or may not be provided) and if the strlen($_POST['extension'])  is < 1 then the output should be simply
areacode-exchange-station

I have tried every combination of writing an if-then statement and escaping the quotes that I am ashamed to admit how much time I’ve wasted.

Might someone provide direction?
Thanks.
0
Comment
Question by:lepirtle
  • 2
4 Comments
 
LVL 31

Expert Comment

by:Frosty555
Comment Utility
It would be more elegant if you broke up that $message variable into a few statements rather than having the entire thing be one big huge statement, but you can do it with the ternary operator:

$to  = 'myname@mydomain.com';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= 'From: my namee <myname@mydomain.com>' . "\r\n";
$subject = 'The subject line';

$telephone = $areacode . "-" . $exchange . "-" . $station . (strlen($_POST['extension']) > 0 ? (" ext " . $_POST['extension']) : $_POST['extension']);

$message = "
<html>
<head>
<title>HTML email</title>
</head>
<body>
<table>
<tr>
<td>Telephone:</td><td>$telephone</td></tr>
</table>
</body>
</html>
";

Open in new window


The important part of the statement is the ternary operator used in this part of the expression:

(strlen($_POST['extension']) > 0 ? (" ext " . $_POST['extension']) : "") 

Open in new window


Which states

IF the statement:
     strlen($_POST['extension']) > 0
evaluates to TRUE then output:
     ("ext " . $_POST['extension'])
otherwise, output
     ""    <-- empty string
0
 
LVL 31

Accepted Solution

by:
Frosty555 earned 500 total points
Comment Utility
I think this is a bit more error-proof and a bit more readable, though:

...

$telephone = $areacode . "-" . $exchange . "-" . $station;
if( array_key_exists('extension', $_POST) ) {
   if( strlen($_POST['extension'] > 0 ) {
        $telephone = $telephone . " ext " . $_POST['extension'];
   }
}

...

Open in new window


When you're talking about "optional" form parameters, it is important to test whether the post variable actually exists or not. If the 'extension' parameter was never passed in the form then your code will throw a warning since $_POST['extension'] would be indexing a non-existent index in the $_POST array. The code above tests for that and behaves correctly.
0
 

Author Closing Comment

by:lepirtle
Comment Utility
Frosty555,
Thanks very much! I tried both solutions and they each work the way I want. Note for anyone else regarding this: the 2nd solution, line #5 is missing a ")", it should read:
   if( strlen($_POST['extension']) > 0 ) {

I also want to thank you for the explanation you provided in solution #1 because I didn't know about ternary operators but reading your very thoughtful plain-english explanation helped me understand.
0
 
LVL 11

Expert Comment

by:un1x86
Comment Utility
also a very elegant way is to use empty()

This will check if $_POST['extesion'] is actually set and if it has a value in it.

For example

if ( !empty($_POST['extension']) )
{
  $telephone = $telephone . " ext " . $_POST['extension'];   
}

Open in new window

0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Use PHP to save or display image returned by JSON call. 20 45
Button Click 11 23
Modify PHP Code on the Fly? 8 38
php image upload 3 24
Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
This article discusses how to create an extensible mechanism for linked drop downs.
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.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

763 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

8 Experts available now in Live!

Get 1:1 Help Now