1

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

1
  • you might need to have name attribute of select tag something like name='chatRoom[]' and then in php you can use implode as implode(',', $_POST['chatRoom']) Commented Feb 25, 2014 at 12:39

3 Answers 3

2

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']);

Sign up to request clarification or add additional context in comments.

3 Comments

I dont think this is the way to go. If you make the select into a multiple="multiple", it already is an 'array' that's posted (comma separted string, I believe) so you don't need the brackets
Its only an array if the name contains []. Otherwise, $_POST['chatRoom'] will only contain the last value.
@barryhunter you are right. but my point is that you need to add multiple="multiple" to the select. You can always make the posted value into an array through server side code...
0
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 ;)

Comments

0

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']) : ''; ?>";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.