• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 282
  • Last Modified:

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?

0
214-042308
Asked:
214-042308
  • 3
  • 3
1 Solution
 
NiversoftCommented:
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"]);
0
 
214-042308Author Commented:
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?
0
 
NiversoftCommented:
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.

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
214-042308Author Commented:
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.
0
 
NiversoftCommented:
When you post the form, only what's actually in the value= attribute is sent to the php script. So you may want to simply modify your form:

<select name="dropbox" size="4" multiple="multiple">
    <option value="H3347 001">  H3347 001&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
    <option value="H3347 002">  H3347 002&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
    <option value="H3347 003">  H3347 003&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</option>
</select>
0
 
214-042308Author Commented:
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!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now