Jump to content

Select Option List Help


twilitegxa

Recommended Posts

I have the following HTML page:

 

<?php
$get_map = "select * from monsters1 group by map";
$get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
<option selected=\"selected\">None Selected</option>";
while ($list_maps = mysql_fetch_array($get_map_res)) {
$map_id = $list_maps['id'];
$map = $list_maps['map'];
echo "<option value=\"$map_id\">$map</option>";
}
echo "</select>";
?>
<div id="txtHint2"></div>

 

This page, when the user chooses an option, the js is called that populates the div tag:

 

js:

var xmlhttp2;

function showMap(str)
{
xmlhttp2=GetXmlHttpObject();
if (xmlhttp2==null)
  {
  alert ("Browser does not support HTTP Request");
  return;
  }
var url2="getlocations.php";
url2=url2+"?q="+str;
url2=url2+"&sid="+Math.random();
xmlhttp2.onreadystatechange=stateChanged2;
xmlhttp2.open("GET",url2,true);
xmlhttp2.send(null);
}

function stateChanged2()
{
if (xmlhttp2.readyState==4)
{
document.getElementById("txtHint2").innerHTML=xmlhttp2.responseText;
}
}

function GetXmlHttpObject2()
{
if (window.XMLHttpRequest2)
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  return new XMLHttpRequest2();
  }
if (window.ActiveXObject)
  {
  // code for IE6, IE5
  return new ActiveXObject("Microsoft.XMLHTTP");
  }
return null;
}

 

This php page is what the js uses to populate the div tag on the HTML page:

 

php:

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("smrpg", $con);

$sql="SELECT * FROM monsters1 WHERE id = '".$q."'";

$result = mysql_query($sql);

echo "<table border='0'>";

while($row = mysql_fetch_array($result))
  {
  echo "Select a location: <select>";
  echo "<option>" . $row['location'] . "</option>";
  }
echo "</select>";

mysql_close($con);
?> 

 

But right now, it's sending the id, but what I want it to do is when the user chooses from the select list, the div tag is populated by another select option list that fills in all the unique values from the location field based on what they chose from the previous select option list (which was from the map field). Can anyone help me out with how to do that? Do I need to send the location field through the js instead of the id, and how can I do that?

Link to comment
Share on other sites

I figured it out. I changed the value of the options on the html page, then changed the where statement on the php page, and got it working perfectly:

 

<?php
$q=$_GET["q"];

$con = mysql_connect('localhost', 'root', '');
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("smrpg", $con);

$sql="SELECT * FROM monsters1 WHERE map = '".$q."' group by location";

$result = mysql_query($sql);

echo "<table border='0'>";
echo "Select a location: <select>";

while($row = mysql_fetch_array($result))
  {
   echo "<option>" . $row['location'] . "</option>";
  }
echo "</select>";

mysql_close($con);
?> 

 

<?php
$get_map = "select * from monsters1 group by map";
$get_map_res = mysql_query($get_map, $conn) or die(mysql_error());
echo "<select name=\"maps\" onchange=\"showMap(this.value)\">
<option selected=\"selected\">None Selected</option>";
while ($list_maps = mysql_fetch_array($get_map_res)) {
$map_id = $list_maps['id'];
$map = $list_maps['map'];
echo "<option value=\"$map\">$map</option>";
}
echo "</select>";
?>
<div id="txtHint2"></div>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.