rsammy Posted June 16, 2006 Share Posted June 16, 2006 Hi,I am working on an application that involves using a drop down to select an item from the list on a page. Once selected, this same filed can be updated in another screen but it still should load with the previously selected value highlighed in the drop down box. User can then select another option and update it.to make it more clear. ..there is a field called Registered Client. The field was populated as Test Client earlier. Later on when we select this record and want to update the values here, the Registered Client field displays Test Client on window load. The user should have the option to change it to say, Test Client 2. The way the system now works is the user can just type the value on the field and say Update to update the record. But, what if he enters some junk, say, dddddd for this field? the system still accepts it. To prevent this from happening, I thot I could use a drop down list box from which I can select pre-specified values. How do I do this?to better explain this point, i am pasting the existing code here...<tr><td><div align="right"><font class="inputLbl">RegisteredClient: </font></div></td><td ><? if (isset($client_name)){print("<input name='ClientName' type='text' class='txtboxLarge' id='ClientName' value= '$client_name' >");print(" <font class='redTxt'>*</font></td>");}else{$queryclient="SELECT client_name, client_id FROM clients";$resultclient= mysql_query($queryclient);if ($resultclient){print ("<select name='ClientName'>");if ($row = mysql_fetch_array($resultclient)){do{print("<option value=\"");print $row["client_name"];print("\">");//print ("Prov. ");print $row["client_name"];print("</option>");}while($row = mysql_fetch_array($resultclient));}}print ("</select>");print("<font class='redTxt'>*</font></td><td ><font class='errTxt'>Please register this patientwith a client.</font></td>" );}//} ?></tr> Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/ Share on other sites More sharing options...
AndyB Posted June 16, 2006 Share Posted June 16, 2006 OK, I'm confused.Are you asking how to generate a dropdown list from data in a database table, or are you asking how to generate a dropdown list from data in a database table AND have the option that matches something chosen previously appear as the 'selected' option in the new dropdown? Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46448 Share on other sites More sharing options...
rsammy Posted June 16, 2006 Author Share Posted June 16, 2006 [!--quoteo(post=384753:date=Jun 16 2006, 05:10 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 16 2006, 05:10 PM) [snapback]384753[/snapback][/div][div class=\'quotemain\'][!--quotec--]OK, I'm confused.Are you asking how to generate a dropdown list from data in a database table, or are you asking how to generate a dropdown list from data in a database table AND have the option that matches something chosen previously appear as the 'selected' option in the new dropdown?[/quote]thanx for the response! yes, i need to know both. i need to know how to generate a drop down box with values in a db table. i need to then select a value from this list and store it. i should then be able to update this value (at a later stage) with another value from the same list and store it!hope that makes it clear this timethanx Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46451 Share on other sites More sharing options...
rsammy Posted June 17, 2006 Author Share Posted June 17, 2006 [!--quoteo(post=384756:date=Jun 16 2006, 05:29 PM:name=lazyuser)--][div class=\'quotetop\']QUOTE(lazyuser @ Jun 16 2006, 05:29 PM) [snapback]384756[/snapback][/div][div class=\'quotemain\'][!--quotec--]thanx for the response! yes, i need to know both. i need to know how to generate a drop down box with values in a db table. i need to then select a value from this list and store it. i should then be able to update this value (at a later stage) with another value from the same list and store it!hope that makes it clear this timethanx[/quote][b]ILL TRY TO BE EVEN CLEARER THIS TIME.[/b]..i have a screen called Patient Info. Here, I enter all the details of the patient and there is a field called Registered Client in this screen. Here, I select a value from a drop down list(test client and test client2 are the values here - for now). I choose test client2 and submit it. This is then stored. Now, I want to search for Existing Patients. I do that and find this patient and check out his details. Since I hade already selected test client2 as the value for Registered Client, that is what I should be seeing there. And I do. When I click on the Update button to be able make some changes there, the drop down shows the default value as test client (the first in the list) and not test client2. Thats where the problem is. I need to see test client2 as the default value in the drop down.This feature was working earlier when I did not use drop downs on the Update screen.We used to display this field(in the update screen) as a text box. The value displayed in the text box reflected the value selected earlier.But, you could end up typing anything in this field llike dddd or abcd, etc., for the Registered Client. We wanted to prevent this by providing a drop down with some pre determined list for the users to choose from rather than type in their own values there! I am able to display the drop down with the values from the database. But, I need this drop down to display the correct value from the list (that is test client2) instead of the original default value(test client).I hope this explanation is much clear now. to better explain my point, I am providing the previous code (which had the changed value displayed in a textbox) here:<tr><td><div align="right"><font class="inputLbl">RegisteredClient: </font></div></td><td ><? if (isset($client_name)){print("<input name='ClientName' type='text' class='txtboxLarge' id='ClientName' value= '$client_name' >");print(" <font class='redTxt'>*</font></td>");}else{$queryclient="SELECT client_name, client_id FROM clients";$resultclient= mysql_query($queryclient);if ($resultclient){print ("<select name='ClientName'>");if ($row = mysql_fetch_array($resultclient)){do{print("<option value=\"");print $row["client_name"];print("\">");print $row["client_name"];print("</option>");}while($row = mysql_fetch_array($resultclient));}}print ("</select>");print("<font class='redTxt'>*</font></td><td ><font class='errTxt'>Please register this patientwith a client.</font></td>" );}?></tr>*****************************************The new code that I put in (to use the drop down list) is:<tr> <td><div align="right"><font class="inputLbl">Registered Client: </font> </div></td> <td colspan="2"><? $queryclient="SELECT client_name, client_id FROM clients"; $resultclient= mysql_query($queryclient); if ($resultclient) { //print ("<select name='client_id'>"); print ("<select name='ClientName'>"); if ($row = mysql_fetch_array($resultclient)) { do { print("<option value=\""); //print $row["client_id"]; print $row["client_name"]; print("\">"); //print ("Prov. "); //print $row["client_name"]; //print $row["client_id"]; print("</option>"); } while($row = mysql_fetch_array($resultclient)) } } print ("</select>"); ?></td></tr>********************************What changes do I need to make to make the drop down list point to the chosen value of test client2 instead of the default value of test client ???I would really appreciate any help regarding this query. Thanks in advance[!--quoteo(post=385083:date=Jun 17 2006, 04:31 PM:name=lazyuser)--][div class=\'quotetop\']QUOTE(lazyuser @ Jun 17 2006, 04:31 PM) [snapback]385083[/snapback][/div][div class=\'quotemain\'][!--quotec--]ILL TRY TO BE EVEN CLEARER THIS TIME...i have a screen called Patient Info. Here, I enter all the details of the patient and there is a field called Registered Client in this screen. Here, I select a value from a drop down list(test client and test client2 are the values here - for now). I choose test client2 and submit it. This is then stored. Now, I want to search for Existing Patients. I do that and find this patient and check out his details. Since I hade already selected test client2 as the value for Registered Client, that is what I should be seeing there. And I do. When I click on the Update button to be able make some changes there, the drop down shows the default value as test client (the first in the list) and not test client2. Thats where the problem is. I need to see test client2 as the default value in the drop down.This feature was working earlier when I did not use drop downs on the Update screen.We used to display this field(in the update screen) as a text box. The value displayed in the text box reflected the value selected earlier.But, you could end up typing anything in this field llike dddd or abcd, etc., for the Registered Client. We wanted to prevent this by providing a drop down with some pre determined list for the users to choose from rather than type in their own values there! I am able to display the drop down with the values from the database. But, I need this drop down to display the correct value from the list (that is test client2) instead of the original default value(test client).I hope this explanation is much clear now. to better explain my point, I am providing the previous code (which had the changed value displayed in a textbox) here:<tr><td><div align="right"><font class="inputLbl">RegisteredClient: </font></div></td><td ><? if (isset($client_name)){print("<input name='ClientName' type='text' class='txtboxLarge' id='ClientName' value= '$client_name' >");print(" <font class='redTxt'>*</font></td>");}else{$queryclient="SELECT client_name, client_id FROM clients";$resultclient= mysql_query($queryclient);if ($resultclient){print ("<select name='ClientName'>");if ($row = mysql_fetch_array($resultclient)){do{print("<option value=\"");print $row["client_name"];print("\">");print $row["client_name"];print("</option>");}while($row = mysql_fetch_array($resultclient));}}print ("</select>");print("<font class='redTxt'>*</font></td><td ><font class='errTxt'>Please register this patientwith a client.</font></td>" );}?></tr>*****************************************The new code that I put in (to use the drop down list) is:<tr> <td><div align="right"><font class="inputLbl">Registered Client: </font> </div></td> <td colspan="2"><? $queryclient="SELECT client_name, client_id FROM clients"; $resultclient= mysql_query($queryclient); if ($resultclient) { //print ("<select name='client_id'>"); print ("<select name='ClientName'>"); if ($row = mysql_fetch_array($resultclient)) { do { print("<option value=\""); //print $row["client_id"]; print $row["client_name"]; print("\">"); //print ("Prov. "); //print $row["client_name"]; //print $row["client_id"]; print("</option>"); } while($row = mysql_fetch_array($resultclient)) } } print ("</select>"); ?></td></tr>********************************What changes do I need to make to make the drop down list point to the chosen value of test client2 instead of the default value of test client ???I would really appreciate any help regarding this query. Thanks in advance[/quote] Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46771 Share on other sites More sharing options...
Fyorl Posted June 17, 2006 Share Posted June 17, 2006 inside the loop, check if the clientName equals the stored value and if so echo 'selected' inside the option tag so it ends up like <option value="blah" selected>blah</option>.Another quick note, you may prefer print or something but it's easier to use echo. Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46796 Share on other sites More sharing options...
AndyB Posted June 17, 2006 Share Posted June 17, 2006 Strictly speaking (cross-browser wise) that should be:<option value="blah" selected="selected">blah</option> Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46826 Share on other sites More sharing options...
Fyorl Posted June 17, 2006 Share Posted June 17, 2006 [!--quoteo(post=385139:date=Jun 17 2006, 06:02 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 17 2006, 06:02 PM) [snapback]385139[/snapback][/div][div class=\'quotemain\'][!--quotec--]Strictly speaking (cross-browser wise) that should be:<option value="blah" selected="selected">blah</option>[/quote]Ah well I wouldn't know, I only ever code for Firefox and Safari (sometimes I can be bothered with IE but that's fairly rare as I usually find myself getting frustrated at its idiocy) which, I know, is a bad idea but when you're coding monster web-apps like me you just want it to work first and then be cross-compatible later (even if it creates more work for you in the end).I just had a thought, wouldn't selected='true' suffice? I'm aware disabled='true' does (but I wouldn't know if that's cross-compatible). I was also wondering if disabled='true' was the same as readonly Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46828 Share on other sites More sharing options...
AndyB Posted June 17, 2006 Share Posted June 17, 2006 Dunno about selected="true", but I do know selected="selected" works on all the browsers I've tried it with. Regardless, you've posted the solution to the OP's questions so let's sit back and wait to see if it's solved for him/her. Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-46832 Share on other sites More sharing options...
rsammy Posted June 19, 2006 Author Share Posted June 19, 2006 [!--quoteo(post=385145:date=Jun 17 2006, 07:18 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 17 2006, 07:18 PM) [snapback]385145[/snapback][/div][div class=\'quotemain\'][!--quotec--]Dunno about selected="true", but I do know selected="selected" works on all the browsers I've tried it with. Regardless, you've posted the solution to the OP's questions so let's sit back and wait to see if it's solved for him/her.[/quote]thanx for your help, Andy and Fyorl! I was able to make some progress. I used "selected" in the loop and it now displays testclient2 as the highlighted value for this client. however, i tried repeating the same sequence of steps but changed the Registred client field to test client for th esame user instead of test client2 and clicked update. I once again pulled up info for this patient and to try another update operation. this time, however, the registered cilent field shows test client2 as the default even though the actual value should have been test client.is there something wrong that im doing? heres the piece of code that i changed....<tr><td><div align="right"><font class="inputLbl">Registered Client: </font> </div></td><td colspan="2"><? $queryclient="SELECT client_name, client_id FROM clients"; $resultclient= mysql_query($queryclient); if ($resultclient) { //print ("<select name='client_id'>"); print ("<select name='ClientName'>"); //echo "$row"; if ($row = mysql_fetch_array($resultclient)) { do { print("<option value=\""); //print $row["client_id"]; print $row["client_name"]; print("\" selected>"); //print ("Prov. "); print $row["client_name"]; //print $row["client_id"]; //echo "$client_name"; print("</option>"); } while($row = mysql_fetch_array($resultclient)); } } print ("</select>"); //} ?></td></tr> Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-47386 Share on other sites More sharing options...
Orio Posted June 19, 2006 Share Posted June 19, 2006 Try changing this:[code]if ($resultclient){[/code]To this:[code]if (mysql_num_rows($resultclient) != 0){[/code]Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-47391 Share on other sites More sharing options...
rsammy Posted June 19, 2006 Author Share Posted June 19, 2006 [!--quoteo(post=385723:date=Jun 19 2006, 02:17 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 19 2006, 02:17 PM) [snapback]385723[/snapback][/div][div class=\'quotemain\'][!--quotec--]Try changing this:[code]if ($resultclient){[/code]To this:[code]if (mysql_num_rows($resultclient) != 0){[/code]Orio.[/quote]Thanks for your response, Orio. Tried this one too, no change in the result! Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-47396 Share on other sites More sharing options...
Orio Posted June 19, 2006 Share Posted June 19, 2006 I cant see you connecting and selecting your database. Your accual script connects to a DB (and you removed it here) or did you forget connecting?Orio. Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-47406 Share on other sites More sharing options...
rsammy Posted June 30, 2006 Author Share Posted June 30, 2006 Sorry for not replyin earlier. I was on vacation and just got back. I figured it out during my vacation and got it to work. Thanks a lot guys, for all your help. Quote Link to comment https://forums.phpfreaks.com/topic/12186-solved-drop-down-using-php/#findComment-51294 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.