Jump to content

Display Results Upon Drop down selection (PHP/MySQL)


wispas

Recommended Posts

Hi All,

 

I currently have a drop dpwn menu that works and retrieves options from my MySQL database. when i select a option from the drop down menu it currently has a selection of the hotel names. (hotel_name)

 

what i want it to do is when i have selected a hotel from the lists i want it to show the hotel name and its description underneath. (hotel_desc)

 

Is this possible to do without having the page reload onto a new page?

Heres my code so far:

 

<?php
// Connect database
include("includes/connectdb.php");

$sql="SELECT hotel_id, hotel_name FROM hotel ORDER BY hotel_name ASC";
$result=mysql_query($sql);

$options=""; 

while ($row=mysql_fetch_array($result)) {

    $id=$row["hotel_id"];
    $hotel_name=$row["hotel_name"];
    $options.="<OPTION VALUE=\"$id\">".$hotel_name;
} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP Dynamic Drop Down Menu</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="selected.php">

<SELECT NAME=id>
<OPTION VALUE=0>Choose
<?=$options.="<OPTION VALUE=\"$id\">".$hotel_name.'</option>';?>
</SELECT> 

  <label>
  <input type="submit" name="submit" id="submit" value="Submit" />
  </label>
</form>

</body>
</html>

Hi

 

Simplest way would be to use Javascript, and save an array of hotel descriptions when the page is created. Use an onchange event on the SELECT to trigger a function to find the appropriate description from the array and put it into a div to display it.

 

You could also do something similar using AJAX. However that would mean more hits on the server. Depends on how many hotels there are and how big the descriptions are.

 

All the best

 

Keith

I got an alert to work now.... when i select from the lists it will give me an alert box telling me of the selected item... does anyone know how to get that text to display in a div instead of an alert box.

 

<?php
// Connect database
include("includes/connectdb.php");

$sql="SELECT hotel_id, hotel_name, hotel_location FROM hotel ORDER BY hotel_name ASC";
$result=mysql_query($sql);

$options=""; 

while ($row=mysql_fetch_array($result)) {

    $id=$row["hotel_id"];
    $hotel_name=$row["hotel_name"];
$hotel_location=$row["hotel_location"];
    $options.="<OPTION VALUE=\"$id\">".$hotel_name." (".$hotel_location.")";
} 

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="javascript">
function disp_text()
   {
   var w = document.myform.mylist.selectedIndex;
   var selected_text = document.myform.mylist.options[w].text;
   alert(selected_text);
   }
</script>

<title>PHP Dynamic Drop Down Menu</title>
</head>

<body>

<form id="myform" name="myform" method="post" action="selected.php">

<SELECT NAME="mylist" onChange="disp_text()">
<OPTION VALUE=0>Choose
<?=$options.="<OPTION VALUE=\"$id\">".$hotel_name.'</option>';?>
</SELECT> 
</form>

</body>
</html>

Hi

 

Something like this would do it:-

 

<?php
// Connect database
include("includes/connectdb.php");

$sql="SELECT hotel_id, hotel_name, hotel_location FROM hotel ORDER BY hotel_name ASC";
$result=mysql_query($sql);

$options="";

while ($row=mysql_fetch_array($result)) {

    $id=$row["hotel_id"];
    $hotel_name=$row["hotel_name"];
   $hotel_location=$row["hotel_location"];
    $options.="<OPTION VALUE=\"$id\">".$hotel_name." (".$hotel_location.")";
}

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<script language="javascript">
function disp_text()
   {
   var w = document.myform.mylist.selectedIndex;
   var selected_text = document.myform.mylist.options[w].text;
   document.getElementById('').innerHTML = selected_text;
   alert(selected_text);
   }
</script>

<title>PHP Dynamic Drop Down Menu</title>
</head>

<body>

<form id="myform" name="myform" method="post" action="selected.php">

<SELECT NAME="mylist" onChange="disp_text()">
<OPTION VALUE=0>Choose
<?=$options.="<OPTION VALUE=\"$id\">".$hotel_name.'</option>';?>
</SELECT>
</form>
<div id='someDivName'>Nowt Selected Yet</div>
</body>
</html>

 

All the best

 

Keith

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.