Page 1 of 1

menu list from database problem

Posted: Fri May 11, 2007 10:50 am
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]

Posted: Fri May 11, 2007 10:58 am
by blackbeard
You need to use AJAX for something like that.

No

Posted: Fri May 11, 2007 11:03 am
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...

Posted: Fri May 11, 2007 11:04 am
by dhrosti
try using:

Code: Select all

window.location='Hotels.php?cat=' + val ;
instead.

Posted: Fri May 11, 2007 11:09 am
by blackbeard
Are you planning on reloading the page everytime the user makes a selection in the first box?

Posted: Fri May 11, 2007 11:17 am
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...

Posted: Fri May 11, 2007 11:17 am
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........

Posted: Fri May 11, 2007 11:39 am
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.

Posted: Fri May 11, 2007 11:55 am
by ghadacr
That didnt work mate, all i was getting was blank web browser screens.

Posted: Fri May 11, 2007 12:35 pm
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.)