lepirtle
asked on
if then statement within html email message
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;ch arset=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." -".$exchan ge."-".$st ation." ".$_POST['extension']."</t d></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.
$to = 'myname@mydomain.com';
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;ch
$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>".$
</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'
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']
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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']
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.
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
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
The important part of the statement is the ternary operator used in this part of the expression:
Open in new window
Which states
IF the statement:
strlen($_POST['extension']
evaluates to TRUE then output:
("ext " . $_POST['extension'])
otherwise, output
"" <-- empty string