Option List Problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Option List Problem

Post 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
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Option List Problem

Post 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
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Option List Problem

Post by nitediver »

is there any other way than using ajax?
lshaw
Forum Commoner
Posts: 69
Joined: Mon Apr 20, 2009 3:40 pm
Location: United Kingdom

Re: Option List Problem

Post 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
}
 
 
 
 
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: Option List Problem

Post by nitediver »

thanks, ill try that...
Post Reply