Page 1 of 1

Option List Problem

Posted: Thu Feb 18, 2010 8:14 am
by nitediver
my database...

Code: Select all

 
---------------------------
id| name  | reguler | extra
---------------------------
1 | City1 | 500     | 750
2 | City2 | 600     | 900
---------------------------
 
Image

Code: Select all

 
<form name="calc" action="calc.php" method="post">
<? 
include "conf.php"; 
 
echo "City <select name=name id=name>";
$city = "SELECT * from destination";
$reqcity = mysql_query($city) or die ("Error!");
 
while($result=mysql_fetch_array($reqcity))
    {
    echo "<option value=$result[name]>
          $result[name]</option>"; 
    }
echo "</select>";
 
echo "<p>Cost <select city=name id=name>";
 
$cost = "SELECT reguler,extra from destination";
$reqcost = mysql_query($city) or die ("Error!");
 
while($result=mysql_fetch_array($reqcost))
    {
    echo "<option value=$result[reguler]>$result[reguler]</option>";
    echo "<option value=$result[ons]>$result[extra]</option>";
    }
echo "</select>";
 
?>
<p>
<input type="submit" name="submit" value="submit">
</form>
</p>
 
I want to display the cost based on selected city, currently it display all cost in the table...
anyone can help me...

Ill appreciate any advice
thanks

Re: Option List Problem

Posted: Thu Feb 18, 2010 8:39 am
by lshaw
If you want to update the values of the second dropdown list when a value is changed in the first one, you need to use ajax.

You can then:

Code: Select all

 
$city= //pass the value of the city through ajax
mysql_query("SELECT `regular`,`extra` FROM `destination`WHERE `name`='$city'")
 
You can then use this query to show only the two prices needed for that city

Re: Option List Problem

Posted: Thu Feb 18, 2010 8:44 am
by nitediver
is there any other way than using ajax?

Re: Option List Problem

Posted: Thu Feb 18, 2010 9:27 am
by lshaw
Only show the second form if the first one has been submitted

Code: Select all

 
if(isset($_POST['submit_city']))
{
$city=$_POST['name'];
}
//must have form tags
echo "City <select name=name id=name>";
$city = "SELECT * from destination";
$reqcity = mysql_query($city) or die ("Error!");
 
while($result=mysql_fetch_array($reqcity))
    {
    echo "<option value=$result[name]>
          $result[name]</option>";
    }
echo "</select>";
echo "<input type='submit' name='submit_city' value='Show prices' />";
 
if(isset($city))
{
//do the query just for the city selected ($city)
//echo the second dropdown box
}
 
 
 
 

Re: Option List Problem

Posted: Thu Feb 18, 2010 10:19 am
by nitediver
thanks, ill try that...