Not sure which forum (Javascript, PHP or SQL) to post in so I figured I’d start here and see what happened ?
I’m trying to incorporate some Javascript for a “Option Transfer” Multiple Select form field. I need to pull the values from a Mysql database (which is working fine) then submit them through the Javascript form and reformat with PHP and submit to a MySQL database.
My problem is that I can’t seem to figure out how to get the Javascript to output an array that I can use to make my comma deliminated list for upload to Mysql.
Here is the relevant code from the form page and upload page. I’d appreciate any help!
The Script
[CODE]<SCRIPT LANGUAGE=”JavaScript”>
<!– This script and many more are available free online at –>
<!– The JavaScript Source!! http://javascript.internet.com –>
<!– Original: Fred P –>
<!– Begin
// Compare two options within a list by VALUES
function compareOptionValues(a, b)
{
// Radix 10: for numeric values
// Radix 36: for alphanumeric values
var sA = parseInt( a.value, 36 );
var sB = parseInt( b.value, 36 );
return sA – sB;
}
// Compare two options within a list by TEXT
function compareOptionText(a, b)
{
// Radix 10: for numeric values
// Radix 36: for alphanumeric values
var sA = parseInt( a.text, 36 );
var sB = parseInt( b.text, 36 );
return sA – sB;
}
// Dual list move function
function moveDualList( srcList, destList, moveAll )
{
// Do nothing if nothing is selected
if ( ( srcList.selectedIndex == -1 ) && ( moveAll == false ) )
{
return;
}
newDestList = new Array( destList.options.length );
var len = 0;
for( len = 0; len < destList.options.length; len++ )
{
if ( destList.options[ len ] != null )
{
newDestList[ len ] = new Option( destList.options[ len ].text, destList.options[ len ].value, destList.options[ len ].defaultSelected, destList.options[ len ].selected );
}
}
for( var i = 0; i < srcList.options.length; i++ )
{
if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
{
// Statements to perform if option is selected
// Incorporate into new list
newDestList[ len ] = new Option( srcList.options[i].text, srcList.options[i].value, srcList.options[i].defaultSelected, srcList.options[i].selected );
len++;
}
}
// Sort out the new destination list
newDestList.sort( compareOptionValues ); // BY VALUES
//newDestList.sort( compareOptionText ); // BY TEXT
// Populate the destination with the items from the new array
for ( var j = 0; j < newDestList.length; j++ )
{
if ( newDestList[ j ] != null )
{
destList.options[ j ] = newDestList[ j ];
}
}
// Erase source list selected elements
for( var i = srcList.options.length – 1; i >= 0; i– )
{
if ( srcList.options[i] != null && ( srcList.options[i].selected == true || moveAll ) )
{
// Erase Source
//srcList.options[i].value = “”;
//srcList.options[i].text = “”;
srcList.options[i] = null;
}
}
} // End of moveDualList()
// End –>
</script>
[CODE]<table border=”0″>
<tr>
<td>
<!– Multiple Select List with 20 rows size and 70 pixels wide –>
<!– Using for SPACING and alignment –>
<select multiple size=”20″ style=”width:70″ name=”listLeft”>
<?php
$username=”wipaire”;
$password=”w1pa1re”;
$database=”wipaire_com_-_wipaire”;
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( “Unable to select database”);
$query1 = “SELECT *,CONCAT( make, ‘ ‘, model ) AS aircraft_label FROM aircraft_models”;
$result1 = mysql_query($query1) or die(mysql_error());
/*$aircraft_label=mysql_result($result_cc,$i,”aircraft_label”);
echo $aircraft_label;
*/
$primary = array();
while ($row = mysql_fetch_assoc($result1)) {
$primary[$row[‘model_code’]] = $row[‘aircraft_label’];
}
$primary = str_replace(” “, ” “, $primary);
foreach ($primary as $key => $value)
{
echo ‘<OPTION value=’.$key.’> ‘.$value.”;
}
echo ‘</select>’;
mysql_close();
?>
</select>
</td>
<td><NOBR>
<input type=”button” style=”width:90″ onclick=”moveDualList( this.form.listLeft, this.form.listRight, false )”
name=”Add >>” value=”Add >>”> <BR>
<NOBR>
<input type=”button” style=”width:90″ onclick=”moveDualList( this.form.listRight, this.form.listLeft, false )”
name=”Add <<” value=”Remove <<“> <BR>
<NOBR>
<input type=”button” style=”width:90″ onclick=”moveDualList( this.form.listLeft, this.form.listRight, true )”
name=”Add All >>” value=”Add All >>”> <BR>
<NOBR>
<input type=”button” style=”width:90″ onclick=”moveDualList( this.form.listRight, this.form.listLeft, true )”
name=”Add All <<” value=”Remove All <<“> <BR>
</NOBR>
</td>
<td>
<select multiple size=”20″ style=”width:70″ name=”listRight”>
<option value=”01″> None </option>
</select>
</td>
</tr>
</table>
[CODE] <?php
if(isset($_POST[‘listRight’])){
$aircraft_list = ”;
foreach($_POST[‘listRight’] AS $listRight){
$aircraft_list .= $listRight.’,’;
}
}
?>
Please let me know if this belongs in one of the other forums.
Thanks!
Anne-Marie