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;
                    }
                <? } ?>
            }
<?	} ?>
}

Link to comment
Share on other sites

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!");

}

Link to comment
Share on other sites

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);
}
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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