/    Sign up×
Community /Pin to ProfileBookmark

php for list boxes

I was wondering how I’d go about configuring this php so that it would send all the options that are selected in the list boxes. There are three boxes, named: genre, rating and period.

[code=php]<?
$message = “Name: {$_POST[‘name’]}n”;
$message .= “Genre: {$_POST[‘genre’]}n”;
$message .= “Rating: {$_POST[‘rating’]}n”;
$message .= “Period = {$_POST[‘period’]}n”;
$message .= “PShip: {$_POST[‘pship’]}n”;
$message .= “Pship2: {$_POST[‘pship2’]}n”;
$message .= “SShip: {$_POST[‘sship’]}n”;
$message .= “SShip2 = {$_POST[‘sship2’]}n”;
$message .= “SShip3: {$_POST[‘sship3’]}n”;
$message .= “SShip4: {$_POST[‘sship4’]}n”;
$message .= “Experience: {$_POST[‘experience’]}n”;
$to = “[email protected]”;
$subject = “HP-Ships.com Listing”;
$mailheaders = “From: ” . $_POST[‘name’] . “<” . $_POST[’email’] . “>n”;

mail($to,$subject,$message,$mailheaders);
header( “Location: http://www.hp-ships.com/thanks.html” );
?>[/code]

[URL=”http://www.hp-ships.com/main_files/submitbr.htm”]This is the page that contains the list boxes.[/URL]

Any suggestions would be much appreciated ?

to post a comment
PHP

18 Comments(s)

Copy linkTweet thisAlerts:
@NightShift58Feb 11.2007 — For "genre", you would need to change:[code=php]<select name="genre" size="6" multiple>[/code]to:[code=php]<select name="genre[]" size="6" multiple>[/code] (add brackets to the name attribute).

This will return $_POST['genre'] which will be an array with the same number of elements as selected on the form.

Proceed identically with "rating" and "period", renamed "rating[]" and "period[]", respecitively.
Copy linkTweet thisAlerts:
@LyssauthorFeb 11.2007 — I tried using that, however it just sent back the word array. I need the specific words they select ?
Copy linkTweet thisAlerts:
@apg88Feb 11.2007 — [code=php]
$message .= "Genre:";

foreach($_POST['genre'] as $genre) {

$message .= "$genre,";

}

// Replace the last added comma with a new line
$message = substr_replace($message,"n",-1,1);

[/code]


This should work.
Copy linkTweet thisAlerts:
@NightShift58Feb 11.2007 — This way:[code=php]$message .= "Genre:" . implode(",", $_POST['genre']);[/code]
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — [code=php]
$message .= "Genre:";

foreach($_POST['genre'] as $genre) {

$message .= "$genre,";

}

// Replace the last added comma with a new line
$message = substr_replace($message,"n",-1,1);

[/code]


This should work.[/QUOTE]


Which comma do I replace with which line? ?
Copy linkTweet thisAlerts:
@apg88Feb 12.2007 — Oh, it's just a comment saying what the function is doing.

Since the foreach statement adds an extra comma at the end of the string, I removed it and added a line break.
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — how do I use it for three different things? genre, rating and period? do I have to repeat the code?
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — Do it this way for your three different POST fields:[code=php]$message .= "Genre:" . implode(",", $_POST['genre']) . "n";
$message .= "Rating:" . implode(",", $_POST['rating']) . "n";
$message .= "Period:" . implode(",", $_POST['period']) . "n";
[/code]
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — Do it this way for your three different POST fields:[code=php]$message .= "Genre:" . implode(",", $_POST['genre']) . "n";
$message .= "Rating:" . implode(",", $_POST['rating']) . "n";
$message .= "Period:" . implode(",", $_POST['period']) . "n";
[/code]
[/QUOTE]


Sorry, it didn't work! It works for one line but not all three.
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — That can only mean that:

[B]name="rating"[/B] and [B]name="period"[/B]

have not yet been renamed - in your form - to:

[B]name="rating[]"[/B] and [B]name="period[]"[/B],

respectively...

I don't know if you're still using the test form you posted at the beginning of the thread, but, on there, none of the input field names have been changed... Until you do, none of the solutions proposed to your in this thread will really work...
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — Still doesn't seem to be working.

I'm recieveing this error:

Warning: implode() [function.implode]: Bad arguments. in /home/lyss19/public_html/main_files/sendmailbr.php on line 3

Warning: implode() [function.implode]: Bad arguments. in /home/lyss19/public_html/main_files/sendmailbr.php on line 4

Warning: implode() [function.implode]: Bad arguments. in /home/lyss19/public_html/main_files/sendmailbr.php on line 5

Warning: Cannot modify header information - headers already sent by (output started at /home/lyss19/public_html/main_files/sendmailbr.php:3) in /home/lyss19/public_html/main_files/sendmailbr.php on line 19


Line 3,4,5 are the new code I added.
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — Yes, the function implode() expects an array, which you can only get if your post fields are named with the square brackets: genre[], period[] and rating[].
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — Yep. They're named with square brackets, so I don't know why it's refusing to work!
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — Are you certain the fields have been set, i.e. at least one selected in each?
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — Yeah, everything is how it should be.
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — This is the same as above with the built-in check:[code=php]$message .= "Genre :" . (isset($_POST['genre']) ? implode(", ", $_POST['genre']) : "none") . "n";
$message .= "Rating:" . (isset($_POST['rating']) ? implode(", ", $_POST['rating']) : "none") . "n";
$message .= "Genre :" . (isset($_POST['period']) ? implode(",", $_POST['period']) : "none") . "n";
[/code]
Here's a live test of your form...

http://www.nightshift58.com/webdev/test_multi_select_values.php
Copy linkTweet thisAlerts:
@LyssauthorFeb 12.2007 — This is the same as above with the built-in check:[code=php]$message .= "Genre :" . (isset($_POST['genre']) ? implode(", ", $_POST['genre']) : "none") . "n";
$message .= "Rating:" . (isset($_POST['rating']) ? implode(", ", $_POST['rating']) : "none") . "n";
$message .= "Genre :" . (isset($_POST['period']) ? implode(",", $_POST['period']) : "none") . "n";
[/code]
Here's a live test of your form...

http://www.nightshift58.com/webdev/test_multi_select_values.php[/QUOTE]



It's finally working now. Thankyou again ?
Copy linkTweet thisAlerts:
@NightShift58Feb 12.2007 — You're welcome!
×

Success!

Help @Lyss 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.6,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...