Hi,
To get this to work what you need to do is add [] at the end of the name in your select tag, this will create an array of the data outputed.
Then you need to use the foreach loop and extract() to take the individual elements in the array and put them into usable variables.
Once you've done that you need to run a for or do while loop to run through each of the variables to determine which is which and what was selected.
I have put together a page of code which does all this then puts the information into a database.
The code for which is:
PHP Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Multiple List Selection in PHP</title> </head> <body> <?php $action=$_REQUEST['action']; if ($action=='process') { // Include your database settings here or link to a file with them in it. include('functions.php'); dbconnect(); // get form variables $output=$_REQUEST['userrank']; if($output) { foreach($output as $output_value) { extract($output, EXTR_PREFIX_INVALID, "user"); } } $num_keys=count($output); $i=0; do { $var="user_{$i}"; if("".${$var}.""=='Admin') { $admin="".${$var}.""; } if("".${$var}.""=='Member') { $member="".${$var}.""; } if("".${$var}.""=='Client') { $client="".${$var}.""; } $i=$i + 1; } while ($i<=$num_keys); // check to see if variables contain data if ($admin=='') { // admin wasn't selected $admin=0; } else { // set admin to 1 $admin=1; } if ($member=='') { //member wasn't selected $member=0; } else { // set member to 1 $member=1; } if ($client=='') { // client wasn't selected $client=0; } else { // set client to 1 $client=1; } // input variables to the database $addinfo=mysql_query("INSERT INTO auth_test VALUES (NULL, '$admin', '$member', '$client')")or die(mysql_error()); if ($addinfo==0) { echo "There was a problem!"; } else { echo "The information was added successfully, the user now has the following settings: <br />"; echo "Admin: ".$admin."<br />"; echo "Member: ".$member."<br />"; echo "Client: ".$client."<br />"; echo "<br />Were the 1 means they have that authoriazation the 0 means they don't."; }
} else { ?> <form action="multiple-list.php?action=process" method="post" enctype="multipart/form-data" name="multiple" id="multiple"> <p> <select name="userrank[]" size="5" MULTIPLE id="userrank"> <option value="Admin">Admin</option> <option value="Member">Member</option> <option value="Client">Client</option> </select> </p> <p> <input type="submit" name="Submit" value="Submit" /> </p> </form> <?php } ?> </body> </html>
If you want to see what the running example looks like go to http://www.labs.tazzyserver.co.uk/mu...tiple-list.php
Hope this helps.
If you need any further help just let me know.
Regards,
Martin
|