Re: empty() in email message

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Dec 13, 2010 at 12:47, Gary <gpaul@xxxxxxxxxxxxxxxx> wrote:
> I have an email message
>
> $msg =  'Name: $fname ' . ' $lname\n'
> . "Phone: $phone\n"
> . "Email: $email\n"
>
> and it works fine, however in this message there are about 30 variables that
> are being called...as such
>
> . "Order: beefschnitzel $beefschnitzel\n"
> . "Order: beefstrips $beefstrips\n"
> . "Order: cheesesausage $cheesesausage\n"
> . "Order: crumbedsausage $crumbedsausage\n"
> . "Order: chucksteak $chucksteak\n"
> . "Order: cornedbeef $cornedbeef\n"
> . "Order: dicedsteak $dicedsteak\n"
> . "Order: filletmignon $filletmignon\n"
>
> I want to only send the message if the submitter enters an amount in the
> form for the corresponding variable, instead of having a bunch of empty
> messages.  So I have been trying to use the empty() function as such:
>
> . if empty($beefolives){''} elseif (isset($beefolives)) { 'Order: beefolives
> $beefolives\n'}
>
> But I am getting the error
>
> Parse error: syntax error, unexpected T_IF
>
> Can someone point me in the right direction?

    That's because you're concatenating.  When appending to a
variable, you can't use constructs and conditions, unless using
ternaries:

. (!empty($beefolives) && is_numeric($beefolives) ? 'Order: beefolives
'.$beefolives : null)

    However, for readability and ease of management, you might want to
try something like this.  It makes at least two assumptions: you're
using a form post, and you understand that it's untested (as I'm just
typing it here into the body of this email).

<?php
// If we've posted our order
if (isset($_POST) && is_array($_POST['order'])) {

	// Instantiate the $order variable
	$order = null;

	// Iterate the order quantities
	foreach ($_POST['order'] as $k => $v) {

		// If the field is set and the amount is a number
		if (isset($v) && is_numeric($v)) {

			$order .= 'Order: '.$k.': '.$v.PHP_EOL;
		}
	}

	/**
	 * Note: in your example, you used single (literal)
	 * quotes and included the variables.  That would
	 * literally print $fname and $lname.
	 */
	$msg  = 'Name: '.$_POST['fname'].' '.$_POST['lname'].PHP_EOL;
	$msg .= 'Phone: '.$_POST['phone'].PHP_EOL;
	$msg .= 'Email: '.$_POST['email'].PHP_EOL;
	$msg .= PHP_EOL;
	$msg .= $order;

	// And then handle the rest of your processing.  Here, we just echo.
	echo '<pre>'.PHP_EOL.$msg.'</pre>'.PHP_EOL;
}
?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
	First Name: <input type="text" name="fname" id="fname"><br/>
	Last Name: <input type="text" name="lname" id="lname"><br/>
	Phone: <input type="text" name="phone" id="phone"><br/>
	Email: <input type="text" name="email" id="email"><br/>
	<br />

	<h3>Order</h3>

	Beef Schnitzel: <input type="text" name="order[beefschnitzel]"
id="order[beefschnitzel]" size="2" maxlength="3"><br/>
	Beef Strips: <input type="text" name="order[beefstrips]"
id="order[beefstrips]" size="2" maxlength="3"><br/>
	Cheese Sausage: <input type="text" name="order[cheesesausage]"
id="order[cheesesausage]" size="2" maxlength="3"><br/>
	Crumbed Sausage: <input type="text" name="order[crumbedsausage]"
id="order[crumbedsausage]" size="2" maxlength="3"><br/>
	Chuck Steak: <input type="text" name="order[chucksteak]"
id="order[chucksteak]" size="2" maxlength="3"><br/>
	Corned Beef: <input type="text" name="order[cornedbeef]"
id="order[cornedbeef]" size="2" maxlength="3"><br/>
	Diced Steak: <input type="text" name="order[dicedsteak]"
id="order[dicedsteak]" size="2" maxlength="3"><br/>
	Filet Mignon: <input type="text" name="order[filletmignon]"
id="order[filletmignon]" size="2" maxlength="3"><br/>
	<!-- NOTE: "Filet" as displayed on the form has one 'L', but the
variable is unchanged. -->
	<br />

	<input type="submit" value="Order Now">
</form>

-- 
</Daniel P. Brown>
Dedicated Servers, Cloud and Cloud Hybrid Solutions, VPS, Hosting
(866-) 725-4321
http://www.parasane.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux