Current location: Hot Scripts Forums » Programming Languages » PHP » Dropdown forms problem


Dropdown forms problem

Reply
  #1 (permalink)  
Old 01-04-04, 10:09 AM
bastiaan bastiaan is offline
New Member
 
Join Date: Nov 2003
Location: Almere
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Question Dropdown forms problem

Hello,

I'm working on a virtual airline timetable. Now the timetable is working very smoothly but the problem is the following: What I need is a search flights page. On this page I want people to look up flights in the database. But I need two dropdown boxes, this is what I've got so far:

-------------------------------------

<?php
include "config.php";
?>

<?php

print "<form name=depairport method=\"POST\" action=\"engine.php\">
<SELECT name=\"naam\" size=1>
<OPTION>Select Airport";
$query="SELECT DISTINCT depairport FROM iberworldva_timetable ORDER BY depairport";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_object($result))
{
echo '<option value="' . $row->depairport . '">' . $row->depairport . '</option>';
}
print "</SELECT>
<input class=knop type=\"submit\" name=\"add\" value=\"Add\">
</form>";
?>


<?php

print "<form name=arrairport method=\"POST\" action=\"engine.php\">
<SELECT name=\"naam\" size=1>
<OPTION>Select Airport";
$query="SELECT DISTINCT arrairport FROM iberworldva_timetable ORDER BY arrairport";
$result = mysql_query($query) or die (mysql_error());
while($row = mysql_fetch_object($result))
{
echo '<option value="' . $row->arrairport . '">' . $row->arrairport . '</option>';
}
print "</SELECT>
<input class=knop type=\"submit\" name=\"add\" value=\"Add\">
</form>";
?>

-------------------------------------

This works well, but how is it possible to show results in the second box after you've picked something from the first box.. I don't know how to explain this but how does it automatically update the second box? I've searched all over the internet and saw some good help sites but I just cannot get it right and it keeps me awake!!

I hope someone is able to help me out with this.

With kind regards,
Bastiaan Klene.
Reply With Quote
  #2 (permalink)  
Old 01-04-04, 11:14 AM
PerHP PerHP is offline
New Member
 
Join Date: Jul 2003
Location: Bergen, Norway
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
This is a code which I found (can't remember where though) and I've used it with success, although alterations are needed to the code obviously.
If it's not exactly what you're looking for, you need to look at this one:
http://javascriptkit.com/script/cut183.shtml and make it work together with PHP/MySQL.
PHP Code:

<HTML>

<title>Dynamic List Boxes in PHP</title>

<BODY>
<?PHP

//  NAME:
//  DynLB.php
//
//  VERSION:
//  Version 1.0 - 2 Oct 2001
//  initial release
//
//  AUTHOR:
//  Asmadi Ahmad (chloro@effitech.com)
//  [url]www.effitech.com[/url]
//
//  DESCRIPTION:
//  This script demonstrates the use of data from two related tables
//  to create two dynamic listboxes via Javascript
//  and shows example on how to retrieve the selected
//  items from listboxes.



//change the following database properties to suit your database

$db_Database "";
$db_UserName "";
$db_Password "";
$db_Hostname "";


//connect to the database

mysql_connect($db_Hostname$db_UserName$db_Password) || UhOh("Can't Connect to Database: ".mysql_error());
mysql_select_db($db_Database);


$test="SELECT * FROM tblCountry";
if (
mysql_query($test)) 

//if the tables already exist do nothing

{        
}

else 

//if the tables isn't there yet, create them and fill up the info

{        
$query[]=" CREATE TABLE tblCountry (id int(10) DEFAULT '0' NOT NULL auto_increment, CountryName varchar(25), PRIMARY KEY(id), KEY id (id))";
$query[]=" CREATE TABLE tblCity (id int(10) DEFAULT '0' NOT NULL auto_increment, CityName varchar(25), Countryid int(10), PRIMARY KEY(id), KEY id (id))";
$query[] = "INSERT INTO tblCountry VALUES( 1, 'Malaysia')";
$query[] = "INSERT INTO tblCountry VALUES( 2, 'USA')";
$query[] = "INSERT INTO tblCountry VALUES( 3, 'UK')";
$query[] = "INSERT INTO tblCity VALUES( 1, 'Kuala Lumpur',1)";
$query[] = "INSERT INTO tblCity VALUES( 2, 'Penang',1)";
$query[] = "INSERT INTO tblCity VALUES( 3, 'Kulim',1)";
$query[] = "INSERT INTO tblCity VALUES( 4, 'New York',2)";
$query[] = "INSERT INTO tblCity VALUES( 5, 'Chicago',2)";
$query[] = "INSERT INTO tblCity VALUES( 6, 'London',3)";
$query[] = "INSERT INTO tblCity VALUES( 7, 'Liverpool',3)";


while (
$each_query each($query))
{
    
$result mysql_query($each_query[1]);
    if (!
$result)
    {print(
"<b>WARNING! We've encountered an error. Please check manually.  Error: ".mysql_error())."<p>";
    die();
    }
}

}    

//declare the form

echo "<FORM name=f1  action='$PHP_SELF' method=post>";

//read the database
 
$result mysql_query("SELECT tblCountry.CountryName,tblCity.Countryid,tblCity.CityName,tblCity.id FROM tblCity,tblCountry WHERE tblCity.Countryid=tblCountry.id");

//write the table

echo "<CENTER><BR><B>Dynamic List Boxes Demo (in PHP)</B><BR>";
echo 
"<TABLE font style='font-family: verdana; font-size: 12; font-weight:700' border=1>";

// write the country's listbox...
  
echo "<TR><TD valign=\"center\">Country</TD><TD><SELECT NAME=\"country\" SIZE=\"10\"  ONCHANGE=\"countryselected(this);\" >\n";

// write the entry code for the javascript...
// \n is used to force a new line so the resultant code is more readable

$sJavaScript "function countryselected(elem){\n for (var i = document.f1.city.options.length; i >= 0; i--){ \n document.f1.city.options[i] = null;\n";

// loop through the database..

$sLastCountry="";
while ( 
$row mysql_fetch_array($result) ) 
  {   
    
// is this a new country?

    
If ($sLastCountry!=$row["CountryName"]){ 
    
      
// if yes, add the entry to the country's listbox

      
$sLastCountry $row["CountryName"];
      echo 
"\n<OPTION VALUE='".$row["Countryid"]."'>".$sLastCountry."</OPTION>";
      
    
// and add a new section to the javascript...

      
$sJavaScript $sJavaScript."}\n"."if (elem.options[elem.selectedIndex].value==".$row["Countryid"]."){\n";
     }
    
// and add a new city line to the javascript

    
$sJavaScript $sJavaScript."document.f1.city.options[document.f1.city.options.length] = new Option('".$row["CityName"]."','".$row["id"]."');\n";

  }

  
// finish the country's listbox

  
echo "</SELECT></TD>";

  
// create the city listbox for no selection

  
echo "\n<TD valign=\"center\">City</TD><TD><SELECT NAME=\"city\" SIZE=10>";
  echo 
"<OPTION>[no city selected]</OPTION>";
  echo 
"</SELECT></TD></TR>";
  echo 
"<TR><TD><font style='font-size=10'></TD><TD></TD><TD></TD><TD><INPUT TYPE=SUBMIT NAME='submitcity' VALUE='SUBMIT'></TD></TR>";

echo 
"</TABLE>";
 
// finish the javascript and write out

  
$sJavaScript $sJavaScript."\n}\n}\n";
  echo 
"\n<SCRIPT LANGUAGE=\"JavaScript\">"
  echo 
"\n".$sJavaScript."\n</SCRIPT>\n";

//close the form

echo "</FORM></center>";


//code to test the submit button
//normally people would save the index in another table
//this example only display the indexes

if ("SUBMIT" == $submitcity
{

echo 
"<center>Your Selected Country index= ".$country."<BR>";  
echo 
"Your Selected City index= ".$city."<BR></center>";   
 
}   

 
?> 
    </body>
    </html>
Reply With Quote
  #3 (permalink)  
Old 01-04-04, 11:27 AM
bastiaan bastiaan is offline
New Member
 
Join Date: Nov 2003
Location: Almere
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Dropdown issue

Hello,

Thank you for your reply. Let me see what I can do with this, I just checked that javascript drop down thing, and it looks very useful I'll look that php you posted in a minute.

With kind regards,
Bastiaan Klene.
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
DHTML layer "behind" dropdown menus !?! paulwynd JavaScript 3 01-14-04 07:57 AM
Small problem for a pro, but for novice like me... argh! ntek Perl 3 11-19-03 01:45 PM
Multiple dropdown plus link problem bastiaan JavaScript 0 11-17-03 04:22 PM
file download problem ukyankee Perl 6 10-04-03 10:39 PM
jpGraph axis labeling problem Squeezer PHP 2 08-20-03 07:53 AM


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