Link to home
Start Free TrialLog in
Avatar of 214-042308
214-042308

asked on

Dropbox selections into PHP mail()

So thanks to you experts I'm just about finished with this little project. Here's my next issue: I have the following code in the form body of my webpage:

<select name="dropbox" size="4" multiple="multiple">
    <option value="1">  H3347 001&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
    <option value="2">  H3347 002&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
    <option value="3">  H3347 003&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
</select>

And here is the php script:

<?php
$name = $_POST['name'] ;
$to = "user@domain.com";
$subject = "Test";
$message = $name . "\n";
$message .= "Requested By:  " . $_POST['your-name'] . "\n";
$message .= "Email Address:  " . $_POST['your-email'] . "\n";
$message .= "Publication(s):  " . $_POST['dropbox'] . "\n";
$message .= "Quantity Ordered:  " . $_POST['quantity'] . "\n";
$message .= "Comments:  " . $_POST['comments'] . "\n";
mail( $to, $subject, $message );
?>

When I receive the email it has everything except selections from the dropbox. Is there an easy way to code multiple dropbox selections to display in the email?

Avatar of Niversoft
Niversoft
Flag of Canada image

The usual way is to name your dropbox as "dropbox[]":

<select name="dropbox[]" size="4" multiple="multiple">

Then, in PHP, $_POST["dropbox"] becomes an array that you can iterate on, or implode, something like:

$dropboxes = implode(",", $_POST["dropbox"]);
Avatar of 214-042308
214-042308

ASKER

Then, in PHP, $_POST["dropbox"] becomes an array that you can iterate on, or implode, something like:

$dropboxes = implode(",", $_POST["dropbox"]);

You lost me.

I think I get the part about name="dropbox[]" in the select tag / webpage, but in the php script I can't understand what you mean by "iterate on or implode" - any chance you can explain that with a more basic code sample for a beginner? In other words, with three selections, and multiple enabled, how do the selected values from the array get passed code-wise to the $message variable along with the rest?
The php script will receive the (multiple) selected values (1, 2, 3) in $_POST["dropbox"], which is now an array if your <select> has been named "dropbox[]".

So, if you select 1 and 3 in your webpage and submit, $_POST["dropbox"] will contain array(1, 3). To put that in your message, you want to convert it to text, the easiest way is the implode function (http://php.net/manual/en/function.implode.php)

implode(', ', $_POST["dropbox"])

which will give "1, 3" (text)

You can then use that in your message-builiding code:

$message .= "Publication(s):  " . implode(', ', $_POST["dropbox"]) . "\n";


In short, replace "dropbox" by "dropbox[]" in your webpage file, and replace

$message .= "Publication(s):  " . $_POST['dropbox'] . "\n";

by

$message .= "Publication(s):  " . implode(', ', $_POST["dropbox"]) . "\n";

in your PHP code.

That works, but is there a way to get the actual text of each selection in the array? All I get, as you said, is a return value of 1,3 when I'm looking for book1, book2 in the resulting email. Please hang in there - this is all new to me.
ASKER CERTIFIED SOLUTION
Avatar of Niversoft
Niversoft
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yep, that did it alright. The text passed to the variable in the array is now correct. Note to self: don't forget to check the option values - doh!