Jump to content


Photo

[SOLVED] drop down using php


  • Please log in to reply
12 replies to this topic

#1 rsammy

rsammy
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 16 June 2006 - 08:10 PM

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">Registered
Client: &nbsp;&nbsp;</font></div></td>
<td >
<? if (isset($client_name)){print("<input name='ClientName' type='text' class='txtboxLarge' id='ClientName' value= '$client_name' >");print("&nbsp;<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 patient
with a client.</font></td>" );

}


//} ?>
</tr>

#2 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 16 June 2006 - 09:10 PM

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?
Legend has it that reading the manual never killed anyone.
My site

#3 rsammy

rsammy
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 16 June 2006 - 09:29 PM

[!--quoteo(post=384753:date=Jun 16 2006, 05:10 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 16 2006, 05:10 PM) View Post[/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 time

thanx

#4 rsammy

rsammy
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 17 June 2006 - 08:32 PM

[!--quoteo(post=384756:date=Jun 16 2006, 05:29 PM:name=lazyuser)--][div class=\'quotetop\']QUOTE(lazyuser @ Jun 16 2006, 05:29 PM) View Post[/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 time

thanx
[/quote]


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">Registered
Client: &nbsp;&nbsp;</font></div></td>
<td >
<? if (isset($client_name)){print("<input name='ClientName' type='text' class='txtboxLarge' id='ClientName' value= '$client_name' >");print("&nbsp;<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 patient
with 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: &nbsp;</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) View Post[/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">Registered
Client: &nbsp;&nbsp;</font></div></td>
<td >
<? if (isset($client_name)){print("<input name='ClientName' type='text' class='txtboxLarge' id='ClientName' value= '$client_name' >");print("&nbsp;<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 patient
with 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: &nbsp;</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]

#5 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 17 June 2006 - 09:59 PM

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



Don't worry, the printer fairies will sort it out.

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 June 2006 - 11:02 PM

Strictly speaking (cross-browser wise) that should be:
<option value="blah" selected="selected">blah</option>
Legend has it that reading the manual never killed anyone.
My site

#7 Fyorl

Fyorl
  • Members
  • PipPipPip
  • Advanced Member
  • 273 posts
  • LocationUK

Posted 17 June 2006 - 11:06 PM

[!--quoteo(post=385139:date=Jun 17 2006, 06:02 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 17 2006, 06:02 PM) View Post[/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
[table]



Don't worry, the printer fairies will sort it out.

#8 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 17 June 2006 - 11:18 PM

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.
Legend has it that reading the manual never killed anyone.
My site

#9 rsammy

rsammy
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 19 June 2006 - 06:12 PM

[!--quoteo(post=385145:date=Jun 17 2006, 07:18 PM:name=AndyB)--][div class=\'quotetop\']QUOTE(AndyB @ Jun 17 2006, 07:18 PM) View Post[/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: &nbsp;</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>


#10 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 June 2006 - 06:17 PM

Try changing this:
if ($resultclient){

To this:
if (mysql_num_rows($resultclient) != 0){

Orio.
Think you're smarty?

(Gone until 20 to November)

#11 rsammy

rsammy
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 19 June 2006 - 06:28 PM

[!--quoteo(post=385723:date=Jun 19 2006, 02:17 PM:name=Orio)--][div class=\'quotetop\']QUOTE(Orio @ Jun 19 2006, 02:17 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Try changing this:
if ($resultclient){

To this:
if (mysql_num_rows($resultclient) != 0){

Orio.
[/quote]

Thanks for your response, Orio. Tried this one too, no change in the result!

#12 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 19 June 2006 - 06:51 PM

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.
Think you're smarty?

(Gone until 20 to November)

#13 rsammy

rsammy
  • Members
  • PipPipPip
  • Advanced Member
  • 212 posts

Posted 30 June 2006 - 03:54 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users