menu list from database 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
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

menu list from database problem

Post by ghadacr »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi, 
I have two drop down box. Depending on the value selected in 1st drop down menu box, values in the 2nd drop down box should change.I am not able to get values in the 2nd box to change when selected from the 1st box. 
But i am getting the values from the database!!!!

Please help

Thanks in advance............

Code: Select all

<?PHP include 'opendb.php'; ?>

<html>

<head>
<title>i</title>
<SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;
self.location='Hotels.php?cat=' + val ;
}

</script>
</head>

<?PHP

@$cat=$HTTP_GET_VARS['cat'];

//$quer2 = mssql_query( "SELECT DISTINCT `Resort`, `ResortID` FROM `Resorts` order by `Resort` "); 
$quer2=mssql_query("SELECT DISTINCT Resort,ResortID FROM Resorts order by Resort");

if(isset($cat) && strlen($cat) > 0){

//$quer=mssql_query("SELECT DISTINCT `HotelName` FROM `Hotels` where `ResortID`=$cat order by ``");
$quer=mssql_query("SELECT DISTINCT HotelName FROM Hotels where ResortID=@$cat order by HotelName");


}else{$quer=mssql_query("SELECT DISTINCT HotelName FROM Hotels order by HotelName"); } 


echo "<form method=post name=f1 action='dd-check.php'>";

echo "<select name='cat' onchange=\"reload(this.form)\"><option value=''>Select one</option>";

while($noticia2 = mssql_fetch_array ($quer2)) { 

if($noticia2['ResortID']==@$cat){echo "<option selected value='$noticia2[ResortID]'>$noticia2[Resort]</option>"."<BR>";}

else{echo  "<option value='$noticia2[ResortID]'>$noticia2[Resort]</option>";}

}

echo "</select>";


echo "<select name='subcat'><option value=''>Select one</option>";

while($noticia = mssql_fetch_array ($quer)) { 

echo  "<option value='$noticia[ResortID]'>$noticia[HotelName]</option>";

}
echo "</select>";



echo "<input type=submit value=Submit>";
echo "</form>";
?>




</body>

</html>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

You need to use AJAX for something like that.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

No

Post by ghadacr »

No need to add an extra, platform or langauge. PHP can perfectly handle it, it just can't seem to work on my script.

NEED HELP...
User avatar
dhrosti
Forum Commoner
Posts: 90
Joined: Wed Jan 10, 2007 5:01 am
Location: Leeds, UK

Post by dhrosti »

try using:

Code: Select all

window.location='Hotels.php?cat=' + val ;
instead.
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

Are you planning on reloading the page everytime the user makes a selection in the first box?
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

blackbeard wrote:Are you planning on reloading the page everytime the user makes a selection in the first box?
The script already does that...
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

dhrosti wrote:try using:

Code: Select all

window.location='Hotels.php?cat=' + val ;
instead.
window.location='Hotels.php?cat=' + val ; -- Sorry that does not work........
blackbeard
Forum Contributor
Posts: 123
Joined: Thu Aug 03, 2006 6:20 pm

Post by blackbeard »

I suggest you use firefox with the firebug plugin to catch javascript errors, it looks like your problem is here:

Code: Select all

SCRIPT language=JavaScript>
function reload(form)
{
var val=form.cat.options[form.cat.options.selectedIndex].value;   <--- This doesn't look right.
self.location='Hotels.php?cat=' + val ;
}

</script>
Try this instead:

var val = document.getElementById('cat').value;

and add an id="cat" to the select line.


That said, you may want to look at using AJAX for a better client side experience. Updating one form element from another would be perfect for learning about it. There's a number of tool kits out there that make it easier to implement.
User avatar
ghadacr
Forum Contributor
Posts: 135
Joined: Fri May 11, 2007 10:44 am

Post by ghadacr »

That didnt work mate, all i was getting was blank web browser screens.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Please try to use the syntax tags for your code.

To have try thing accessable to non javascript users you are likely to have to have a form anyway and a button "GO" if javascript is disabled. If you have a form anyway all you need is for javascript submit the form containing the first select. That way you are not hardcoding the direction and you don't have to get any value.

(If you want to use Ajax, search for chained select on these forums.)
Post Reply