473,563 Members | 2,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to create a dynamic drop down box

19 New Member
I have created a php form with dynamic drop down boxes in it which pull the options from a database. The boxes work fine and the data gets submitted fine but im having a problem getting the box to default to a record of my choice from the database. (record 1 = None).
Here is the code to what i have done.
Expand|Select|Wrap|Line Numbers
  1. <input maxLength="40" size="30" name="name"></td>
  2.     </tr>
  3.     <tr>
  4.       <td width="286" height="22"><b>Person Involved:</b></td>
  5.       <td align="left" width="365" height="24"><select name="person_involved">
  6.  
  7.         <?php
  8.  
Expand|Select|Wrap|Line Numbers
  1. hesk_dbConnect();
  2. $sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
  3. $result = hesk_dbQuery($sql);
  4. while ($row=hesk_dbFetchAssoc($result))
  5. {
  6.     echo "
  7.     <option value=\"$row[name]\">$row[name]</option>
  8.     ";
  9. }
  10.  
Expand|Select|Wrap|Line Numbers
  1. ?>
  2.       </select></td>
  3.     </tr>
  4.  
Hope you guys can help me out as im pulling my hair out now.
Oct 1 '07 #1
31 8290
nathj
938 Recognized Expert Contributor
I have created a php form with dynamic drop down boxes in it which pull the options from a database. The boxes work fine and the data gets submitted fine but im having a problem getting the box to default to a record of my choice from the database. (record 1 = None).
Here is the code to what i have done.
[php]
<input maxLength="40" size="30" name="name"></td>
</tr>
<tr>
<td width="286" height="22"><b> Person Involved:</b></td>
<td align="left" width="365" height="24"><se lect name="person_in volved">

<?php

hesk_dbConnect( );
$sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
$result = hesk_dbQuery($s ql);
while ($row=hesk_dbFe tchAssoc($resul t))
{
echo "
<option value=\"$row[name]\">$row[name]</option>
";
}

?>
</select></td>
</tr>
[/php]

Hope you guys can help me out as im pulling my hair out now.

Hi DirtySnipe,

Welcome to TSDN!

I had to do something similar recently and the way I decided to do it was to add a colum to the data source - isDefault as a tinyInt. then select this out in the SQL and during the processing of the data you can check for the value of this field and if it's set to 1 you can set selected="selec ted" in the processing code.

Here's an example from my database, this selects out countries from an ISO list and Sets United Kingdom as the default.

[php]
// get the data
$lcSelectStr =
"SELECT
a.ID, a.countryName as description, a.isDefault
FROM countrycode a
ORDER BY a.countryName ASC";

$laResults = $loDB->queryGetData($ lcSelectStr); // my data object

// build the drop down
echo "<select name=" . "'" . "list" . $lnGetType . "'" . "id=" . "'" . "list" . $lnGetType . "'" . ">";
foreach($laResu lts as $lcDataLine)
{
echo "<option value=" . $lcDataLine['ID'] . " ";
if($lcDataLine['isDefault'])
{
echo "selected='sele cted'";
}
echo ">" . $lcDataLine['description'] ."</option>";
}
echo "</select>";

[/php]
I hope that helps.

Cheers
nathj
Oct 1 '07 #2
DirtySnipe
19 New Member
Thanks for the welcome.

Im sorry but this is going straight over my head. <--(day 1 n00b to php mysql)

what i made was cobbled from bits of code i found on the net and a good old cut and paste session.

Can you please help me on how to get it working with what i got atm??

I know im a cheaky git... alot to learn and im tryin.
Oct 1 '07 #3
nathj
938 Recognized Expert Contributor
Hi,

I'll do my best to help you out, and if anything needs any further explanation then just shout.

I notice from your code that you have a table 'hesk_involved' so the first step, under my solution is to add a new field to this:
Name: isDefault
Type: TINYINT
Default:0

Once you have added the field simply decide which value is to be the default and set the isDefault to 1 for this record.

Alter your code to the following:
[php]
$sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
$result = hesk_dbQuery($s ql);
echo "<select name='testSelec t'>"; //name is up to you
while ($row=hesk_dbFe tchAssoc($resul t))
{
echo "<option value=\"$row[name]\">$row[name]";
if($row['isDefault']);
{
echo "selected='sele cted'";
}
echo ">" . $row['description']. "</option>"; // NOTE: $description is a column in the table that holds what will appear in the drop down list for the user.
echo "</option>";

}
[/php]

What is happening in this code, and apologies if this a bit obvious, is that the data is being selected first. Once the data has been selected a drop down is built. the wrapper tags for this are listed first and then the options within the select item are defined based on the data. One of the data items determines the default value for the drop down. For this value the HTML code of stating selected as selected is applied to ensure that it appears first in the list when the page loads.

I hope that explains the code and gives you nice solution. If you need any further help just shout here or PM me I don't mind.

Cheers
nathj
Oct 1 '07 #4
DirtySnipe
19 New Member
Thank you very much, this has helped loads...

Ive been stuck on this problem for a few weeks now and it was by luck that I stumbled along this cool site. I hope one day i can return the favour and give you a hand.

Once again tnxs alot.
Oct 1 '07 #5
DirtySnipe
19 New Member
I've added that code and also added into the mysql database the details you said but i get this appear in the drop down box.

Contractorselec ted='selected'>
Oct 1 '07 #6
DirtySnipe
19 New Member
Expand|Select|Wrap|Line Numbers
  1.  <tr>
  2.       <td width="286" height="22"><b>Person Involved:</b></td>
  3.       <td align="left" width="365" height="24">
  4.  
  5.         <?php
  6.  
  7.  
  8. $sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
  9. $result = hesk_dbQuery($sql);
  10. echo "<select name='person_involved'>"; //name is up to you
  11. while ($row=hesk_dbFetchAssoc($result))
  12. {
  13.     echo "<option value=\"$row[name]\">$row[name]";
  14.     if($row['isDefault']);
  15.     {
  16.      echo "selected='selected'";
  17.     }
  18.     echo ">" . $row['name']. "</option>"; // NOTE: $description is a column in the table that holds what will appear in the drop down list for the user.
  19.     echo "</option>";
  20.  
  21. }
  22.  
  23.  
  24. ?>
  25.       </select></td>
  26.     </tr>
Oct 1 '07 #7
nathj
938 Recognized Expert Contributor
Hi DirtySnipe,


The solution is simple, I had this myself, I should have mentioned it. simply ensure there is a space before and after the word selected:

[php]
// snippet
echo " selected='selec ted' " ;
[/php]
These spaces force the browser to recognise that is handling new information about the control you wish to display.

Sorry for the cofusion.

Cheers
nathj
Oct 1 '07 #8
DirtySnipe
19 New Member
Its still doing it.

Contractor selected='selec ted' >Contractor

The funny thing is also i havn't selected Contractor as default.

Doesn't this need to = 1 or something??

[PHP]if($row['isDefault']);[/PHP]

Also why are you telling it to display the word selected='selec ted'???

[PHP] echo " selected='selec ted' " ;[/PHP]
Oct 1 '07 #9
nathj
938 Recognized Expert Contributor
Doesn't this need to = 1 or something??

[PHP]if($row['isDefault']);[/PHP]
Hi,
If the data type is tinyint this is behaves in a way that is the same as logical type. However, it won't hurt set to '=1' if you prefer.
Also why are you telling it to display the word selected='selec ted'???

[PHP] echo " selected='selec ted' " ;[/PHP]
What this does, and here is where the problem lies, is is says that the selected property of the option within the select input type is set to be selected - that's the syntax. In the code snippet I supplied before there is a bit of a copy and past error:
[php]
$sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
$result = hesk_dbQuery($s ql);
echo "<select name='testSelec t'>"; //name is up to you
while ($row=hesk_dbFe tchAssoc($resul t))
{
echo "<option value=\"$row[name]\">$row[name]";
if($row['isDefault']);
{
echo "selected='sele cted'";
}
echo ">" . $row['description']. "</option>"; // NOTE: $description is a column in the table that holds what will appear in the drop down list for the user.
echo "</option>";

}
[/php]
If you remove >$row[name] then I think all should be well as the code will then set the property within the element tag. That's my fault for some sloppy copy and paste work.

This should now work for you.
Cheers
nathj
Oct 1 '07 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1750
by: xxbmichae1 | last post by:
I am writing a function that will create different type of input ranges, example one function that will create date range inputs with drop down calendars, another will be for currency ranges, another for uppercase input, etc.... anyway, the drop down calendar I am using has a javascript you call after the html of your edit box and object to...
1
1683
by: ayende | last post by:
Okay, I'm pretty sure that I'm doing everything right here, but something is very wrong in the result. I've a page that has a drop down list (with auto-post-back = true) and a place holder. During page load, I'm loading a control dynamically (using LoadControl()) and adding it to the place holder. In the control OnInit(), I load some...
1
1572
by: daveyand | last post by:
Hey guys, I am currently stuck, i am trying to create a function that will create a fresh new drop down option box dynamically. What happens is the user can double click an element on the page, when they do this function should be called and create the drop down. I can do the innerHTML stuff, i know how to add new options but am stuck...
6
8291
by: mcgrew.michael | last post by:
I hope this is the right group. I am very new to ASP so this is probably a stupid question. I have some vbscript that query's AD and populates a recordset. I know the recorset contains the information I want by doing a Response.write. I am having problems dynamically creating a drop down list from the data in the recordset. The drop down is...
2
1785
by: Jim Gregg | last post by:
Hello all, I am faced with some logic that I am unsure how to handle. Imagine that I am running a WMI query and I am outputting the data into a dynamically created ASP table control. Here is my code that does this. I have left out the portion that connects to my server and the query, but this should be enough to show what I am doing....
2
4322
by: vinceboy | last post by:
Hi anybody. I am newbie here and would like to know that how can I validate both drop down menu and radio button from a dynamic display form.Something went wrong with my script.The radio button is dynamic,however drop down menu is static. <script language="JavaScript" type="text/JavaScript"> function formSubmit(obj) { var returnStatus =...
1
3453
by: bytesFTW99 | last post by:
I have been struggling with this for some time can anyone help out? just trying to have 3 dropdown boxes that fill depending on what is selected, then in some cases click a button and have the second set of 3 dropdown boxes be filled with the same values. thank you <head> <SCRIPT LANGUAGE="JavaScript" type="text/javascript"> var...
3
2800
by: happyman992 | last post by:
Hi, I have 2 questions. First, I am building a form with multiple drop down boxes. The options of the second drop down box will depend on what the user chooses on the first, options of the third drop down box depend on what they choose on the second, etc. The values of each option are inside a database which I can query for and put into a...
4
2762
by: audreyality | last post by:
I am new to javascript and appreciate any guidance on this issue, so thank you in advance for your advice! Situation: I am working with a form that will send information to a database. I need the following: A drop-down box of options that onChange=... Opens another drop-down box of unique options that onChange=... Displays text dependent...
0
7665
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7583
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7642
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5484
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5213
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3643
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2082
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1200
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.