garethhall Posted June 26, 2009 Share Posted June 26, 2009 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; } <? } ?> } <? } ?> } Quote Link to comment Share on other sites More sharing options...
xenophobia Posted June 26, 2009 Share Posted June 26, 2009 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!"); } Quote Link to comment Share on other sites More sharing options...
garethhall Posted June 26, 2009 Author Share Posted June 26, 2009 I have checked the if statement works Quote Link to comment Share on other sites More sharing options...
dzelenika Posted June 26, 2009 Share Posted June 26, 2009 AFAIK you should use document.frmSearch.album.selectedIndex = 2 Quote Link to comment Share on other sites More sharing options...
garethhall Posted June 26, 2009 Author Share Posted June 26, 2009 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); } } Quote Link to comment Share on other sites More sharing options...
dzelenika Posted June 26, 2009 Share Posted June 26, 2009 I think you problem is embedding php code in javascript. That's very bad practice. It is very difficult to debug application. For example better solution is storing values in hidden fields Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.