There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
black screen blue screen boot bsod computer connection crash css dell display driver drivers email error explorer firefox firefox 3 hard drive internet internet explorer itunes laptop lcd malware monitor network networking nvidia outlook outlook 2003 outlook express password printer problem problems ram router security slow software sound sprtcmd.exe trojan usb video virus vista windows windows xp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Solved: Php+drop Down Menu


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

Closed Thread
 
Thread Tools
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
18-Jun-2008, 08:34 PM #1
Question Solved: Php+drop Down Menu
Hello all,

I have a databse that lists all the product NAME and CODES.
I also have a form in which a drop down menu exists. In the drop down menu, all the codes of the products are populated. What I want to do is once the user has selected something from the dropdown, I want it to show the Name of the product beside it. I dont even mind if the page has to be refreshed each the user selects something from the drop down menu. I just want it to go to the database on each change and get the name of that product.

Please help!!!!!
Mudley's Avatar
Computer Specs
Senior Member with 100 posts.
 
Join Date: Apr 2008
Experience: Advanced
18-Jun-2008, 09:24 PM #2
you don't need to go to the database each time as long as the product name is getting pulled in the same query as the product code

use some javascript to change the text next to the select box

use an ONCHANGE event in the <select> tag



do you want to learn how to do this?
or do you just want it done?
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 10:05 AM #3
Thanks for your reply,

I would really appreciate if you could only do this for ONE product. This way i get a sample and I get to learn by my self.

Use the following product details:

Product Code: C440
Product Name: Product A.

Product Code: C441
Product Name: Product B

Please use these products to show me an example where by changing the value in the select automatically shows me the product name beside it.

Thanks,
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 10:07 AM #4
And lets say that the product code and name are in a database.
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 01:45 PM #5
So when we are getting the product details in javascript......they must be called from a database which has someting like 60 products.

help!!!!
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
19-Jun-2008, 03:49 PM #6
This isn't too hard to do - what I would do is read all the product names and codes in one query when the page is loaded. For this example I will assume that they are in arrays called $names and $codes.

The most efficient way to do this is with a for loop (or a foreach loop, but with two separate arrays, I find it easier to keep track of the key with a for loop), populating the dropdown list and javascript as it goes along.

HTML Code:
<head> <script type='text/javascript'>
function textChange(text) {
document.getElementById('nameText').innerHTML = text;
}
</script> </head> <body> <select> <!-- PHP code goes here --> </select> <p id='nameText'></p> </body>
PHP Code:
for ($x 0$x <= sizeof($names); $x $x 1) {
echo 
"<option onSelect=\"textChange('$names[x$]')\">$codes[$x]</option>\n";

__________________
There's no place like 127.0.0.1


Last edited by TheRobatron : 19-Jun-2008 04:05 PM.
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 04:04 PM #7
Can't i set it up in such a way that it creates the $ name and $code array from the database. Because I will be changing database entries and I dont want to have to manually edit the array????
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
19-Jun-2008, 04:06 PM #8
Yeah, are you using MySQL?
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 04:08 PM #9
yes
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 04:08 PM #10
phpmyadmin as well to manage and create tables, rows, etc
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 04:18 PM #11
Can i do the following?

$result = mysql_query('SELECT * FROM table') or exit(mysql_error());
while ($row = mysql_fetch_assoc($result)) {$array[$row['P_Codes']] = $row['P_Name'];}

print_r($array);
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 04:19 PM #12
this should create an array with the product code as the key and the product name as the value.....right?
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
19-Jun-2008, 04:28 PM #13
I would do it slightly differently:
PHP Code:
$result mysql_query('SELECT * FROM table') or exit(mysql_error());
$x 0;
while (
$row mysql_fetch_assoc($result)) {
$codes[$x] = $row['P_Codes'];
$names[$x] = $row['P_Name'];
$x $x 1;} 
Mr.LLB's Avatar
Senior Member with 144 posts.
 
Join Date: May 2008
19-Jun-2008, 04:35 PM #14
Ok this is cool too.....and the javascript stuff you provided earlier will work with this PHP array......right?
TheRobatron's Avatar
Computer Specs
Senior Member with 480 posts.
 
Join Date: Oct 2007
Location: England
Experience: Intermediate
19-Jun-2008, 04:36 PM #15
It should do

If it doesn't, post the code that you're using.
Closed Thread

THIS THREAD HAS EXPIRED.
Are you having the same problem? We have volunteers ready to answer your question, but first you'll have to join for free. Need help getting started? Check out our Welcome Guide.


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
WELCOME TO TECH SUPPORT GUY! Are you looking for the solution to your computer problem? Join our site today to ask your question -- for free! Our site is run completely by volunteers who help people like you solve computer problems. See our Welcome Guide to get started.



Thread Tools


You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 11:19 AM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.