473,698 Members | 2,452 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
31 8321
DirtySnipe
19 New Member
Here is a export of the mysql table so you can see for yourself I have only set one option to 1.



Expand|Select|Wrap|Line Numbers
  1. CREATE TABLE `hesk_involved` (
  2.   `id` smallint(5) unsigned NOT NULL auto_increment,
  3.   `name` varchar(20) collate latin1_general_ci NOT NULL default '',
  4.   `isDefault` tinyint(4) NOT NULL default '0',
  5.   PRIMARY KEY  (`id`)
  6. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=33 ;
  7.  
  8. -- 
  9. -- Dumping data for table `hesk_involved`
  10. -- 
  11.  
  12. INSERT INTO `hesk_involved` VALUES (1, 'None', 1);
  13. INSERT INTO `hesk_involved` VALUES (25, 'Staff', 0);
  14. INSERT INTO `hesk_involved` VALUES (26, 'Student', 0);
  15. INSERT INTO `hesk_involved` VALUES (27, 'Staff (agency)', 0);
  16. INSERT INTO `hesk_involved` VALUES (28, 'Staff (Bank)', 0);
  17. INSERT INTO `hesk_involved` VALUES (29, 'Visitor', 0);
  18. INSERT INTO `hesk_involved` VALUES (30, 'Parent', 0);
  19. INSERT INTO `hesk_involved` VALUES (31, 'Sibling', 0);
  20. INSERT INTO `hesk_involved` VALUES (32, 'Contractor', 0);
Oct 2 '07 #21
nathj
938 Recognized Expert Contributor
Hi,

Thanks for sending the data sump across, that was really helpful. I amust admit I am now baffled by this.

I'm trying to think of the best way to go forward on this and get this working for you.
[php]
<?php

$sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
$result = hesk_dbQuery($s ql);

while ($row=hesk_dbFe tchAssoc($resul t))
{
echo $row[name] . " " . $row[isDefault] . "<br />";
}
?>
[/php]
Try this code to see what the php page is pulling out of the database.

Cheers
nathj
Oct 2 '07 #22
DirtySnipe
19 New Member
here you go.

[HTML]<td width="286" height="22"><b> Person Involved:</b></td>
<td align="left" width="365" height="24">

Contractor 0<br />None 1<br />Parent 0<br />Sibling 0<br />Staff 0<br />Staff (agency) 0<br />Staff (Bank) 0<br />Student 0<br />Visitor 0<br />

</td>
</tr>[/HTML]
Oct 2 '07 #23
nathj
938 Recognized Expert Contributor
Hi,

To summarise then we have ruled out data and we have ruled out data hanlding. This means the problem must be in the processing side of things.

Here's a new code sample, it looks pretty much like the other ones but there are som subtle differences that may help.
[php]
<?php

$sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
$result = hesk_dbQuery($s ql);

echo "<select name='person_in volved'>";
while ($row=hesk_dbFe tchAssoc($resul t))
{
echo "<option value=" .$row[name] . " "; // final space is vital
if($row[isDefault]);
{
echo "selected='sele cted' " ;
}
echo ">" . $row[name] . "</option>";

}

echo "</select>";
?>
[/php]
Try that, the IF condition has been altered slightly. I think the trouble is that something was forcing the code to hit the lines inside the IF every time.

Cheers
nathj
Oct 2 '07 #24
DirtySnipe
19 New Member
[HTML]<td width="286" height="22"><b> Person Involved:</b></td>
<td align="left" width="365" height="24">

<select name='person_in volved'><option value=Contracto r selected='selec ted' >Contractor</option><option value=None selected='selec ted' >None</option><option value=Parent selected='selec ted' >Parent</option><option value=Sibling selected='selec ted' >Sibling</option><option value=Staff selected='selec ted' >Staff</option><option value=Staff (agency) selected='selec ted' >Staff (agency)</option><option value=Staff (Bank) selected='selec ted' >Staff (Bank)</option><option value=Student selected='selec ted' >Student</option><option value=Visitor selected='selec ted' >Visitor</option></select>

</td>[/HTML]
Oct 2 '07 #25
nathj
938 Recognized Expert Contributor
[HTML]<td width="286" height="22"><b> Person Involved:</b></td>
<td align="left" width="365" height="24">

<select name='person_in volved'><option value=Contracto r selected='selec ted' >Contractor</option><option value=None selected='selec ted' >None</option><option value=Parent selected='selec ted' >Parent</option><option value=Sibling selected='selec ted' >Sibling</option><option value=Staff selected='selec ted' >Staff</option><option value=Staff (agency) selected='selec ted' >Staff (agency)</option><option value=Staff (Bank) selected='selec ted' >Staff (Bank)</option><option value=Student selected='selec ted' >Student</option><option value=Visitor selected='selec ted' >Visitor</option></select>

</td>[/HTML]
Hi DirtySnipe,

I'm totally stumped. I cannot see any reason for this code not to work for you. What appears to be happening is that the IF is not affecting the code flow. I can't see why based on the data you have shown.

At present I'm out of answers but I'll keep thinking over it and if anything comes to me I'll post back.

One 'cheats' solution would be to order by isDefault desc (I think) then do nothing with selected and the default item should be the first in the list. The downside is that the list is then not alphabetical, perhaps a combination of order by clauses would do it for you?

Cheers
Nathan
Oct 2 '07 #26
DirtySnipe
19 New Member
Is there anyone else here that can solve this problem I have??
Oct 2 '07 #27
nathj
938 Recognized Expert Contributor
Hi DirtySnipe,

I was thinking about this problem today, as I said I would and i thought about changing the if. It currently stands as:
[php]
if($row[isDefault]);
{
echo &quot;selected= 'selected' &quot; ;
}
[/php]
Try changing it to:
[php]
if($row[isDefault]==1);
{
echo &quot;selected= 'selected' &quot; ;
}
else
{
echo &quot; &quot; ; // the space is important
}
[/php]

I've had to do something just like this today. It's working a treat now with this sort of structure.

Cheers
nathj
Oct 3 '07 #28
DirtySnipe
19 New Member
Now i don't even get a page display.


[PHP]<?php

$sql = "SELECT * FROM `hesk_involved` ORDER BY `name` ASC";
$result = hesk_dbQuery($s ql);

echo "<select name='person_in volved'>";
while ($row=hesk_dbFe tchAssoc($resul t))
{
echo "<option value=" .$row[name] . " "; // final space is vital
if($row[isDefault]==1);
{
echo "selected='sele cted' " ;
}
else
{
echo " " ; // the space is important
}
echo "</select>";



?>[/PHP]
Oct 5 '07 #29
Yardgnome
6 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.
Try this:
[PHP]<?
while ($row=hesk_dbFe tchAssoc($resul t))
{
$sel = $row['isDefault'] == true ? "selected='sele cted'" : "";
?>
<option <?=$sel?> value="<?=$row['name']?>"><?=$row['name']?></option>
<?
}
?>[/PHP]
Oct 5 '07 #30

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

Similar topics

2
1761
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 click on to drop the calendar....is there a way or is it correct to say, that if you use innerHtml...
1
1688
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 values into it.
1
1579
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 as to how to create the initial object in the first place.
6
8301
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 created but it is empty. Any help would be greatly appreciated. A sample of the code: <% 'On...
2
1792
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. Basically it is a web page that queries SMS for clients in a certain collection, then outputs the name...
2
4333
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; if (obj.rNumber.selectedIndex == 0 || !(movieList.time;?>].checked)) {
1
3463
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 firstChoice2 = 0; var secondChoice2 = 0;
3
2804
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 php array. I am wondering what is the easiest way to build this. Second, when this form is...
4
2771
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 on which option you chose. An example:
0
8609
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9166
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9030
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7737
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6525
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5861
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4371
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4621
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2007
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.