Jump to content

[SOLVED] drop down using php


rsammy

Recommended Posts

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>
Link to comment
Share on other sites

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?
Link to comment
Share on other sites

[!--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 time

thanx
Link to comment
Share on other sites

[!--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 time

thanx
[/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">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) [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">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]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--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
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[!--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: &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>
Link to comment
Share on other sites

[!--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!
Link to comment
Share on other sites

  • 2 weeks later...
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.