Jump to content

Multiple Lists Selection / PHP


liquidd

Recommended Posts

Hey all,

 

I am still learning PHP, which is the best language for database driven sites. I am using Dreamweaver CS3 with the devleopers toolbox which is "ok" to help take the guess work out of things.

 

I am using a simple registration form that i made with the toolbox, but when I come to a multiple list to choose from (populated from the database) it doesn't work correctly. Of course, out of the multiple items selected only the last one gets inserted.

 

From what I have read, the selection must be turned into an array then looped for this to work. Is this correct? Next how would I code this to work right.

 

If you let me know which code to post, I will post it.

 

Thanks for all your help!!

Link to comment
Share on other sites

Yes, you must name the select tag as an array with square brackets. Take this example:

 

<?php
if(isset($_POST['submit'])){
    foreach($_POST['select'] as $value){
        echo 'You selected '.$value.'<br />';
    }
}
?>
<form action="<?php echo $_SERVER['phpself']; ?>" method="post">
<select name="select[]" multiple="multiple">
<option value="var1">Var 1</option>
<option value="var2">Var 2</option>
<option value="var3">Var 3</option>
<option value="var4">Var 4</option>
<option value="var5">Var 5</option>
</select>
<br />
<input type="submit" name="submit" />

If you were to select all 5 options, the output would be:

You selected var1
You selected var2
You selected var3
You selected var4
You selected var5

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.