user2656114
user2656114

Reputation: 970

Build a list in PHP from multiple selected form options

I have a form that a user completes to join a chat room, wasn't ever a problem as we only had it to be able to join a single chat room but now we've provided multiple options like

           <option value="#Chat1">Chat 1</option>
           <option value="#Chat2">Chat 2</option>
           <option value="#Chat3">Chat 3</option>
           <option value="#Chat4">Chat 4</option>

Later on I have

params.autojoin = "<? echo $_POST['channel']; ?>";

How can I change this so that it is every selected channel seperated by a comma i.e. #Chat1,#Chat3?

Thanks

Upvotes: 1

Views: 65

Answers (3)

Andres Orav
Andres Orav

Reputation: 61

It should work:

set name of the select as name="channel[]"

params.autojoin = "<?php echo isset($_POST['channel']) && is_array($_POST['channel']) ? implode(',', $_POST['channel']) : ''; ?>";

Upvotes: 0

Yogesh
Yogesh

Reputation: 924

HTML

<select name="chatRoom[]" multiple="multiple">
<option value="#Chat1">Chat 1</option>
<option value="#Chat2">Chat 2</option>
<option value="#Chat3">Chat 3</option>
<option value="#Chat4">Chat 4</option>
</select>

PHP $selectChatRoom = implode(",",$_POST['chatRoom']);

Upvotes: 2

Digital Chris
Digital Chris

Reputation: 6202

params.autojoin = "<? echo implode(',',$_POST['channel']); ?>";

Edit:

As others have commented, my solution assumes you have correctly set up your select like:

<select multiple name="channel[]">

You might already have this; no way to tell if you don't provide the code ;)

Upvotes: 0

Related Questions