Current location: Hot Scripts Forums » Programming Languages » PHP » Help with a PHP drop down menu!


Help with a PHP drop down menu!

Reply
  #1 (permalink)  
Old 06-01-05, 10:42 AM
maweber98 maweber98 is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Help with a PHP drop down menu!

I need to create a drop down menu in PHP that is populated by an Mysql table. The table consists of 3 field "ID", "Breeds", and "Sizes".

Table Structure:
CREATE TABLE `Halti_Sizes` (
`Breeds` varchar(255) default NULL,
`Sizes` varchar(255) default NULL,
`ID` int(255) NOT NULL auto_increment,
PRIMARY KEY (`ID`)
) TYPE=MyISAM;

I want the drop down menu to be populated by the "Breeds" field. Once a breed is selected I want the corresponding "Sizes" and "ID" from that row to be displayed. What is the easiest way to accomplish this. Any help would be greatly appreciated. Thanks so much!
Reply With Quote
  #2 (permalink)  
Old 06-01-05, 02:01 PM
ben.periton ben.periton is offline
Wannabe Coder
 
Join Date: Oct 2004
Posts: 183
Thanks: 0
Thanked 0 Times in 0 Posts
Javascript with arrays would probably be the easiest way to go, or you could try using xmlHTTPRequest function. (http://www.webpasties.com/xmlHttpRequest , http://xulplanet.com/references/objr...tpRequest.html , http://www.fiftyfoureleven.com/resou...quest/examples)

Got to go out, but Ill post a javascript example later on when I get in
__________________
Ben Periton
http://ben.periton.co.uk
Reply With Quote
  #3 (permalink)  
Old 06-01-05, 02:13 PM
tallpaul858 tallpaul858 is offline
Newbie Coder
 
Join Date: Apr 2005
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Hi There,

I wrote this script for displaying car ref numbers and names, look at http://www.lotus.uk.net/testdrive.php for an example.

This is within a form and the content of each option's value is the ID for that row, this ID is sent to the next page and can be used in another sql query to display the row you want.

SEE COMMENTS IN PHP CODE BELOW

Code Below:

<select name="car_id" class="searchboxfields">
<option value="">Select... (Leave For No Car)</option>
<option value="">-- Used Cars --</option>
PHP Code:

<?php

              
                                         
include("cms/connect.php"); //connect to database

              
              
$sql "SELECT * FROM tblcar";

//query
              
              
$result mysql_query($sql);
              
              
$num mysql_numrows($result); //number of rows returned

              
              
$i 0// $i is counter
              
              
while ($i $num) { // while loop goes through each row and extracts, in this case, ref, name (to display) and id (for sending to another page)
              //content of each row
              
$id mysql_result($result,$i,'id');
              
$ref mysql_result($result,$i,'ref');
              
$name mysql_result($result,$i,'name');
              

//outputs option values into dropdown menu
              
echo("
              
              
              <option value='"
.$id."'>REF: ".$ref." | NAME: ".$name."</option>
              
              
              "
);

              
$i++;
              }
              include(
"cms/close.php");
              
              
              unset(
$id,$ref,$name);

              
?>
</select>

Last edited by tallpaul858; 06-01-05 at 02:17 PM.
Reply With Quote
  #4 (permalink)  
Old 06-01-05, 02:29 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Code:

<form action=<?=$_SERVER['PHP_SELF']?> method=post>

<select name="breeds" onchange="this.form.submit();">
<?php $sql="SELECT * FROM Halti_Sizes";
$result=mysql_query($sql);
while(
$row=mysql_fetch_assoc($result);
if(
$row['ID']==$_POST['breeds']) $selected="selected"; else $selected="";
echo 
"<option {$selected} value=\"{$row['ID']}\">{$row['Breeds']}</option>";?>
</select>
</form>

<?php
if($_POST['breeds']){
    
$sql="SELECT *  FROM Halti_Sizes WHERE ID='{$_POST['breeds']}'";
    
$result mysql_query($sql);
    
$row=mysql_fetch_assoc($result);
    echo 
"Breeds:  ".$row['Breeds']."<br>Sizes:  ".$row['Sizes']."<br>ID:  ".$row['ID']."<br>";
}
?>
give that a try, i didnt test it but im pretty sure it works.
Reply With Quote
  #5 (permalink)  
Old 06-01-05, 03:56 PM
maweber98 maweber98 is offline
Newbie Coder
 
Join Date: Aug 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
drop down menu

I tried the code listed, but I think there is a syntax error. I haven't been able to pin point it. All I get is a blank white page. If you could take another look and let me know if I'm doing something wrong. Thanks so much!!
Reply With Quote
  #6 (permalink)  
Old 06-01-05, 04:02 PM
Jaffizzle Jaffizzle is offline
Newbie Coder
 
Join Date: May 2005
Posts: 61
Thanks: 0
Thanked 0 Times in 0 Posts
err hold up lemme redo the code real fast there are plenty of small syntax errors
PHP Code:

<?php //include db connection here// ?>

<form action=<?=$_SERVER['PHP_SELF']?> method=post>
<select name="breeds" onchange="this.form.submit();">
<?php $sql="SELECT * FROM Halti_Sizes";
$result=mysql_query($sql);
while(
$row=mysql_fetch_assoc($result)){
if(
$row['ID']==$_POST['breeds']) $selected="selected"; else $selected="";
echo 
"<option {$selected} value=\"{$row['ID']}\">{$row['Breeds']}</option>"; }?>
</select>
</form>

<?php
if($_POST['breeds']){
    
$sql="SELECT *  FROM Halti_Sizes WHERE ID='{$_POST['breeds']}'";
    
$result mysql_query($sql);
    
$row=mysql_fetch_assoc($result);
    echo 
"Breeds:  ".$row['Breeds']."<br>Sizes:  ".$row['Sizes']."<br>ID:  ".$row['ID']."<br>";
}
?>
like i said the code wasnt tested, but if you have some PHP familiarity it should be pretty simple to get it working and understand the logic.

Last edited by Jaffizzle; 06-01-05 at 04:06 PM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a drop down menu ? ? ? Spreegem PHP 4 03-09-05 09:34 AM
using javascript in php for image changing with drop down menu developer_x PHP 0 02-13-05 08:26 AM
Drop Down menu order peterc PHP 1 08-20-04 02:23 AM
drop down menu problem !!! nurqeen PHP 1 02-08-04 08:07 PM
putting content in a drop down menu tom PHP 7 07-04-03 02:25 PM


All times are GMT -5. The time now is 06:49 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.