/    Sign up×
Community /Pin to ProfileBookmark

problem with option transfer multiple select using Javascript, PHP and MySQL

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]

[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]

[CODE] <?php

if(isset($_POST[‘listRight’])){
$aircraft_list = ”;
foreach($_POST[‘listRight’] AS $listRight){
$aircraft_list .= $listRight.’,’;
}
}
?>[/CODE]

Please let me know if this belongs in one of the other forums.

Thanks!

Anne-Marie

to post a comment
JavaScript

5 Comments(s)

Copy linkTweet thisAlerts:
@ScriptageAug 06.2008 — Do you mean something like this?:

<script>

var myArray = new Array("Hello","World!","Join","example");

var delimited = myArray.join(",");

document.write(delimited);

</script>[/QUOTE]
Copy linkTweet thisAlerts:
@annie_webbyauthorAug 06.2008 — How would I get the form fields into the "var myArray = new Array("Hello","World!","Join","example");" part though?

When I echo the result that's being passed from the form I only get 1 entry (the first one selected) so I'm thinking that the Javascript isn't sending an array at all.

Or am I supposed to use that code on the form side?
Copy linkTweet thisAlerts:
@ScriptageAug 06.2008 — [code=php] <select multiple size="20" style="width:70" name="listRight" id="listRight">

<option value="01"> 1 </option>
<option value="02"> 2 </option>
<option value="03"> 3 </option>
<option value="04"> 4 </option>

</select>


<script>

var myArray = new Array();

var select = document.getElementById("listRight").options;

for(var i=0; i<select.length; i++){

myArray.push(select[i].value);

}

document.write(myArray.join(","));


</script>[/code]


Regards

Carl
Copy linkTweet thisAlerts:
@annie_webbyauthorAug 06.2008 — AAAaaaaaahhhhhhh -cue lightbulb above head -

I get what you're saying now. I'm going to go try that out!

cheers,

Annie
Copy linkTweet thisAlerts:
@annie_webbyauthorAug 07.2008 — Alright I'm showing my total lack of Javascript knowledge here but....

How do I get that list to refresh after adding more elements? I inserted it after the form code and it creates the list with the one intial option but I need it to submit a full list of ALL the elements that are moved to that list.

Any hints?
×

Success!

Help @annie_webby spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...