ok here is my problem. I have a select list in a form that a user can select multiple items from. This list is generated from a database.

<select name="type" class="formfields-select" id="JobType">
<option value='' selected="selected">Select</option>
<option value=''>------</option>
<?PHP

						$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
						$db=mysql_select_db ("foorbar", $connection) or die (mysql_error());
						$query = "SELECT type FROM typelist ORDER BY type ASC";
						$sql_result = mysql_query($query, $connection) or die (mysql_error());

						while ($row = mysql_fetch_array($sql_result)) {
							$type = $row["type"];
							echo "<option value = '$type'>$type</option>";
						}
					?>
			  </select>

When the form is submitted only one selection is saved to the database and echoed to the page.

I need all of them to be saved and echoed.

I'm kind of new at this, if you can't tell from my post and any suggestions would be appreciated.

    If I remember correctly, you need to change the name in your select dropdown to something like

    <select name="type[]" multiple>
         <option value="1">1</option>
         <option value="2">2</option>
    </select>

    The [] designates the menu as a select. The multiple attribute allows users to select more than one option by holding down the ctrl key. Just remember when you run your INSERT $type is an array, so you will need to loop through it or do something similar when INSERTint.

      ok I understand what your asking. but I'm at a loss as to where in the code I need to loop it. below is the insert tag can you point me in the right direction?

      $connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
      $db=mysql_select_db ("foobar", $connection) or die (mysql_error());
      $query = "INSERT INTO worklog (id, newtime, datetime, clientname, clientcode, startmo, startday, startyr, endmo, endday, endyr, duemo, dueday, dueyr, market, job, type, spec, status, designer, dsgnemail, adrep, ademail, rush) VALUES ('$id', $newtime, now(), '$clientname', '$clientcode', '$startmo', '$startday', '$startyr', '$endmo', '$endday', '$endyr', '$duemo', '$dueday', '$dueyr', '$market', '$job', '$type', '$spec', '$status', '$designer', '$dsgnemail', '$adrep', '$ademail', '$rush')";

        Here's some code to get you going. Make SURE you validate user's input to ensure that first off it's not malicous, and secondly that all fields are filled in and/or empty ones won't break your code. Now this code assumes that you named each of your form fields to match the name of it's corresponding column in the database ($fields variable). Adjust this code as needed

        <?php
        
        $insert = "";
        $fields = "";
        foreach($type as $key => $value) 
        {
            $fields .= $key . ",";
            $insert .= "'".$insert."',";
        }
        
        // Remove the last comma from $fields and $insert
        $fields = substr_replace($fields, ",", -1, 1);
        $insert = substr_replace($insert, ",", -1, 1);
        
        // Connect to DB
        $conn = mysql_connect($server, $username, $pass) or die(mysql_error());
        
        // Select DB
        $db = mysql_select_db($database, $conn) or die(mysql_error());
        
        // Develop Query String
        $query = "INSERT INTO worklog ($fields) VALUES($insert)";
        
        // Print Query String to ensure proper formatting 
        // (just during development. Remove this later
        print $query;
        
        // Execute Query
        $result = mysql_query($query) or die(mysql_error());
        
        ?>

          wait a second...disregard my last post. What is it you're trying to do here? Can you post your HTML code for your form (please put it in code brackets for readability). Where are all these variables that are in your SQL command coming from?

            ok this is the code the form data to the point of the select box I'm having trouble with. I try to post the entire page but it was larger than the board would allow. As you can see the form is generated by php code.

            
            </head>
            <body>
            	<div id="wrapper">
            		<div id="header"><div id="title">Welcome</div><div id="clock">.. <?PHP $h = (date("g") - 1); $datetime = date("M d, Y  $h:i a"); echo"$datetime"; ?></div><div class="break"></div></div>
            		<div id="nav">
            			<ul id="buttons">
            			  <?PHP	
            				echo'
            				<li><a href="'.$_SERVER["PHP_SELF"].'">add job request</a></li>
            				<li><a href="'.$_SERVER["PHP_SELF"].'?do=viewreports">view job reports</a></li>
            				';
            			  ?>
            			</ul>
            			<div class="break"></div>
            		</div>
            		<div id="content">
            
            	<?PHP
            	switch ($do) {
            	default:
            	?>
            		<div id="left" style="border-right:1px dotted #666;">
            			<?PHP
            				if ($message) {
            					echo 'The following problems occurred:<br />\n';
            					foreach ($message as $key => $value) {
            						echo '$value <br />\n';
            						}
            				}
            			?>
            			<?PHP echo'<form id="Form" action="'.$_SERVER["PHP_SELF"].'?do=add" method="post" onsubmit="return valiDate()">'; ?>
            			<div class="text">Client&nbsp;(<a class="editlist" href="javascript:editlist('listedit.php?edit=clientlist',700,400);">edit list</a>):</div>
            			<div class="field">
            				<select class="formfields-select" name="client" id="Client">
            					<option value="" selected="selected">Select</option>
            					<option value="">------</option>
            					<?PHP
            						$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
            						$db=mysql_select_db ("foobar", $connection) or die (mysql_error());
            						$query = "SELECT clientname, clientcode FROM clientlist ORDER BY clientname ASC";
            						$sql_result = mysql_query($query, $connection) or die (mysql_error());
            
            						while ($row = mysql_fetch_array($sql_result)) {
            							$clientname = $row["clientname"];
            							$clientcode = $row["clientcode"];
            							$client = $clientcode."-".$clientname;
            							echo "<option value = '$client'>$clientname</option>";
            						}
            					?>
            				</select>
            			</div><div class="break"></div>
            
            			<div class="break"></div>
            			<div class="text">Creative Due By:</div>
            				<div class="field">
            						<?PHP $facetime=date("m-d-Y",time());  ?>
            <input type="Text" id="demo1" name="demo1"  value="<?php echo $facetime; ?>" maxlength="25" size="10">
            <a href="javascript:NewCal('demo1','mmddyyyy')"><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date"></a><br />
            <span style="font-weight:normal; color:#666; font-size:10px;">(Please use the calendar tool to select date!)</span>
            
            					<!--<input class="formfields" name="duedate" size="20" type="text" />-->
            				</div>
            			<div class="break"></div>
            
            
            
            			<div class="break"></div>
            			<div class="text">Job:</div><div class="field"><input class="formfields" name="job"  id="Job" size="30" type="text" /></div><div class="break"></div>
            			<div class="text">Job Type&nbsp;(<a class="editlist" href="javascript:editlist('listedit.php?edit=typelist',700,400);">edit list</a>):</div>
            			<div class="field">
            				<select name="type[]" size="10" multiple="multiple" class="formfields-select" id="JobType">
            					<option value='' selected="selected">Select</option>
            					<option value=''>------</option>
            					<?PHP
            
            						$connection=mysql_connect ("localhost", "foo", "bar") or die ("I cannot connect to the database.");
            						$db=mysql_select_db ("foobar", $connection) or die (mysql_error());
            						$query = "SELECT type FROM typelist ORDER BY type ASC";
            						$sql_result = mysql_query($query, $connection) or die (mysql_error());
            
            						while ($row = mysql_fetch_array($sql_result)) {
            							$type = $row["type"];
            							echo "<option value = '$type'>$type</option>";
            						}
            					?>
            			  </select>
            			</div>
            
            
            
            
            
            
            
            
            
            

              roughly read ur post..
              remind u, all the selected values from multiple select will be returned into an array..
              use looping such as foreach and so on to get the value.
              cheers.

                Write a Reply...