Jump to content

[SOLVED] Setting the selected value on a dropdown box.


garethhall

Recommended Posts

Hi I have this function the populates and drop down box. I am trying to set the selected value but I can't seem to get it to work

As far as I know this is a syntax " document.frmSearch.album.options[0].selected = true;"

Cant anyone see what I am doing wrong?

 

function updateAlbum(selectbox){
removeAllOptions(document.frmSearch.album);
    addOption(document.frmSearch.album, "", "All Clients Albums");
    <? 	$SQL_clientID = "SELECT clientId from albums GROUP BY clientId"; // select all client from albums and group
	$rs_clientID = mysql_query($SQL_clientID, $admin);
	while($row_clientID = mysql_fetch_assoc($rs_clientID)){ // loop though the clientId results from the albums table ?> 
		if(document.frmSearch.client.value == "<? echo $row_clientID['clientId'] ?>"){ 
            	<? $SQL_selectAlbum = "SELECT * FROM albums WHERE clientId = '$row_clientID[clientId]'";
			$rs_selectAlbum = mysql_query($SQL_selectAlbum, $admin);
			while($row_selectAlbum = mysql_fetch_assoc($rs_selectAlbum)){ 
				$i++ ; ?>
                	addOption(document.frmSearch.album, <? echo "'$row_selectAlbum[albumID]'" ?>, <? echo "'$row_selectAlbum[albumName]'" ?>);
                    if(<? echo "'$row_selectAlbum[albumID]'" ?> == <? echo "'$_GET[theAlbum]'" ?>){
                    document.frmSearch.album.options[<? echo "$i" ?>].selected = true;
                    }
                <? } ?>
            }
<?	} ?>
}

First of all, can you make sure that your if statement is being executed?

 

if(<? echo "'$row_selectAlbum[albumID]'" ?> == <? echo "'$_GET[theAlbum]'" ?>){

 

You should try debug it by putting an alert message inside this if statement:

if(<? echo "'$row_selectAlbum[albumID]'" ?> == <? echo "'$_GET[theAlbum]'" ?>){

    alert("Im here!");

}

Thanks for help so far Great you suggestion did work well only half way ?? not sure why that is. I thought I would put the rest of the code up so you can have a look

$SQL_client = "SELECT * FROM clients";
$rs_client = mysql_query($SQL_client, $admin);
?>
//==================================Add an option to selectbox=======================================
function addOption(selectbox,value,text){
var optn = document.createElement("OPTION");
optn.value = value;
    optn.text = text;

selectbox.options.add(optn);
}
//=================================Add clients to selectbox=====================================
function addClient(selectbox){
<? while($row_client = mysql_fetch_assoc($rs_client)){ 
$j++; ?>
addOption(document.frmSearch.client, <? echo "'$row_client[clientID]'" ?>, <? echo "'$row_client[clientName]'" ?>);
if(<? echo "'$row_client[clientID]'" ?> == <? echo "'$_GET[theClient]'" ?>){
document.frmSearch.client.selectedIndex = [<? echo "$j" ?>];
}
addOption(document.frmSearch.assignClient, <? echo "'$row_client[clientID]'" ?>, <? echo "'$row_client[clientName]'" ?>);
<? } ?>
}
<? //=============================================Update Album selectbox ================================================================  ?>
function updateAlbum(selectbox){
removeAllOptions(document.frmSearch.album);
    addOption(document.frmSearch.album, "", "All Clients Albums");
    <? 	$SQL_clientID = "SELECT clientId from albums GROUP BY clientId"; // select all client from albums and group
	$rs_clientID = mysql_query($SQL_clientID, $admin);
	while($row_clientID = mysql_fetch_assoc($rs_clientID)){ // loop though the clientId results from the albums table ?> 
		if(document.frmSearch.client.value == "<? echo $row_clientID['clientId'] ?>"){ 
            	<? $SQL_selectAlbum = "SELECT * FROM albums WHERE clientId = '$row_clientID[clientId]'";
			$rs_selectAlbum = mysql_query($SQL_selectAlbum, $admin);
			while($row_selectAlbum = mysql_fetch_assoc($rs_selectAlbum)){ 
				$i++ ; ?>
                	addOption(document.frmSearch.album, <? echo "'$row_selectAlbum[albumID]'" ?>, <? echo "'$row_selectAlbum[albumName]'" ?>);
                    if(<? echo "'$row_selectAlbum[albumID]'" ?> == <? echo "'$_GET[theAlbum]'" ?>){
                    	document.frmSearch.album.selectedIndex = [<? echo "$i" ?>];
                    }
                <? } ?>
            }
<?	} ?>
}
<? //=============================================Update Assign Album selectbox ================================================================  ?>
function updateAssignAlbum(selectbox){
    removeAllOptions(document.frmSearch.assignAlbum);
    addOption(document.frmSearch.assignAlbum, "", "Select Client Album");
    <? 	$SQL_clientID = "SELECT clientId from albums GROUP BY clientId"; // select all client from albums and group
	$rs_clientID = mysql_query($SQL_clientID, $admin);
	while($row_clientID = mysql_fetch_assoc($rs_clientID)){ // loop though the clientId results from the albums table ?> 
		if(document.frmSearch.assignClient.value == "<? echo $row_clientID['clientId'] ?>"){ 
            	<? $SQL_selectAlbum = "SELECT * FROM albums WHERE clientId = '$row_clientID[clientId]'";
			$rs_selectAlbum = mysql_query($SQL_selectAlbum, $admin);
			while($row_selectAlbum = mysql_fetch_assoc($rs_selectAlbum)){ ?>
                    addOption(document.frmSearch.assignAlbum, <? echo "'$row_selectAlbum[albumID]'" ?>, <? echo "'$row_selectAlbum[albumName]'" ?>);
                <? } ?>
            }
<?	} ?>
}
//=====================================Remove all values from the check box=================================
function removeAllOptions(selectbox){
var i;
for(i=selectbox.options.length-1;i>=0;i--){
	selectbox.remove(i);
}
}

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.