/    Sign up×
Community /Pin to ProfileBookmark

conditional output in email form

I have 7 checkboxes in a form, but for simplicity, let’s say I have have 3 (amongst other items) and would like to email the individual checkbox results only if it is checked. For example:

[CODE]Please check which bottled water you like to drink:
[] Ozarka
[] Aquafina
[] Dasani[/CODE]

If the user checks Ozarka and Aquafina, then those are the only two that are sent in the email.

The form just mails a text file using a string to grab the info. And here’s what I’m currently using:

[CODE]$email_text = ‘Questionnaire Information: ‘ . “n” .
‘First Name: ‘ . $firstname . “n” .
‘Last Name: ‘ . $lastname . “n” .
‘Email: ‘ . $email . “n” .
‘Bottled water you drink: ‘ . $bottled_water1 . ‘ ‘ . $bottled_water2 . ‘ ‘ . $bottled_water3 . “n”
(more code after this that I didn’t include)[/CODE]

This works, but I’d like to be able to put commas or dashes between each variable so it’s more legible in the email. Those are easy enough to add, but if the user only selects Ozarka and Dasani, the output would end up looking like this:

[CODE]Ozarka, , Dasani[/CODE]

Notice the extra comma.
If they only pick Ozarka and Aquafina it looks like this:

[CODE]Ozarka, Aquafina,[/CODE]

Again, an extra comma.

Is it possible to add a conditional statement to output only the bottled water that is selected. I tried several things and couldn’t get it to work. Or can you create a new variable that will check for the conditions, let it construct the string, then use only that one variable to send the info?

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@callumdJun 14.2007 — One solution is to change your HTML form code to something like

[code=html]<input type="checkbox" name="drinks[]" value="Ozarka" />Ozarka
<input type="checkbox" name="drinks[]" value="Aquafina" />Aquafina
<input type="checkbox" name="drinks[]" value="Dasani" />Dasani
[/code]

Then alter your PHP mail script:

[code=php]$email_text = 'Questionnaire Information: ' . "n" .
'First Name: ' . $firstname . "n" .
'Last Name: ' . $lastname . "n" .
'Email: ' . $email . "n" .
'Bottled water you drink: ';
foreach ($_POST['drinks'] as $drink) {
$email_text .= "$drink, ";
}
$email_text = substr($email_text, 0, -2);[/code]
×

Success!

Help @bradymc spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.9,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...