Email items > 0 from a form

I (Extreme Novice) am trying to email a form that is basically a shopping list…

EGGS [ 0 ]
BACON [ 2 ]
SAUSAGES [ 0 ]

I only want the list that contains items so EGGS and SAUSAGES would not be emailed.
I have been playing with isset but not seeming to get it to work from what I gather 0 is still a value and is technically ‘set’ can someone point me in the right direction here please

A Snippit of the code is as follows…
<?php
if($_POST[“fullName”]) {
mail($_POST[“email”], “Customer Order”,
“NAME: “.$_POST[“fullName”].”\r\n”.
“EMAIL: “.$_POST[“email”].”\r\n”.
“TEL: “.$_POST[“contact”].”\r\n”.
“ADDRESS : #”.$_POST[“doornumber”]." - “.$_POST[“postcode”].”\r\n".
“SITE : “.$_POST[“site”].”\r\n”.
“\r\n”.
“====FOOD====\r\n”.
“[”.$_POST[EGGS]."] x EGGS\r\n".
“[”.$_POST[BACON]."] x BACON\r\n".
“[”.$_POST[SAUSAGES]."] x SAUSAGES\r\n".
… and so forth

Im thinking now should the items be something like…
IF $_POST[EGGS] > 0 { "[".$_POST[EGGS]."] x EGGS\r\n".} ELSE { } ... something like this??!!

Start by getting the code to work with a single form field. By writing out all the code, that you will need to change several times along the path of getting it to work, you are wasting time.

If the submitted data is organized properly, you can just use array_filter() on it to remove the empty/zero values. You should also loop over a list of expected input field names and test if the input isset(). By unconditionally looping over all the submitted form data, hackers/spammers can cause you code to include anything they want in the message…

All dynamic values being put into the email body should have htmlentities() applied to them to help prevent cross site scripting.

Any submitted email address should be validated to insure it contains only one valid email address. See php’s Filter Functions (filter_var for example) using FILTER_VALIDATE_EMAIL .

You should also build the message body in a php variable to make debugging easier.

Sponsor our Newsletter | Privacy Policy | Terms of Service