Jump to content

Recommended Posts

I have a dropdown that is on two levels. The first drop down is populated with the county list from my database. It returns the county in a drop down of what data is in the database. Then when you select the county you want the second list is populated with the city. The problem I have is the second drop down is getting more than one of the same city.

 

So for example if you select Greater Manchester in the drop down, it should return in the city drop down Manchester and Altringham, but it show manchester twice and altringham once, not sure why it is repeating the manchester one, as I am using select distinct? does any one have any idea why i get these results like this.

 

Heere is the code for the drop down

 


<form action="quick-results.html" METHOD="POST"><p align="right">Quick Find: <select name="region" onchange="ajaxrequest('select2',this.value)"> <option value selected="">Choose your County</option><?php echo $options; ?></select> <select name="club_town" id="select2" class="userinput"><option value selected="">Choose your City</option></select><input type="submit" value="go"></form></p>

 

Then we have two scripts and an ajax file.

 

Here is the script that generates the first drop down:

 

<?php
include("connect.php");
$sql = mysql_query("select distinct club_county from clubs ORDER BY club_county ASC");
$options = "";
while($row = mysql_fetch_row($sql)) {
$options .= "<option value=\"$row[0]\">$row[0]</option>\n";
}
$region = "";
$club_town = "";
if(isset($_POST)) {
$region = $_POST["region"];
$club_town = $_POST["club_town"];
}
?>

 

Here is the script for the second drop down that gets created based on the first

 

<?php
include("connect.php");
$request = $_POST["request"];

$sql = mysql_query("select distinct club_town from clubs WHERE club_county = '".$request."' ");
if (mysql_num_rows($sql) ==0)
{
echo ("<option selected>Choose your City</option>\n");
} 
else {
$club_town = "";
while($row = mysql_fetch_row($sql)) {
$club_town .= "<option value=\"$row[0]\">$row[0]</option>\n";
echo $club_town;
}
}
?>

 

And finally here is the ajax file

 

function get_XmlHttp() {
var xmlHttp = null;

if (window.XMLHttpRequest) {
	xmlHttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}

return xmlHttp;
}

function ajaxrequest(tagID, post) {
var http = get_XmlHttp();
var info = 'request=' + post;

http.open("POST", 'inc/destinations.ajax.city.php', true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(info);

http.onreadystatechange = function() {
	if (http.readyState == 4) {
		var mata = http.responseText.split("</option>");

		for (i = 0; i <= (mata.length - 2); i++) {
			var container = document.getElementById(tagID);
			var newdiv = document.createElement("option");
			if (i == 0) {
				container.innerHTML = '';
			}
			newdiv.innerHTML = mata[i];
			container.appendChild(newdiv);
		}

	}
}
}

 

Help anyone?

Link to comment
https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/
Share on other sites

Thanks Mr Adam,

 

I have just ran this script but put the request in manually.

 


<?php
$sql = mysql_query("select distinct club_town from clubs WHERE club_county = 'Greater Manchester' ");
if (mysql_num_rows($sql) ==0)
{
echo ("<option selected>Choose your City</option>\n");
} 
else {
$club_town = "";
while($row = mysql_fetch_row($sql)) {
$club_town .= "<option value=\"$row[0]\">$row[0]</option>\n";
echo $club_town;
}
}
?>

 

And it is returning the following:

Manchester

Manchester

Altrincham

 

In my database for Greater Manchester I have 2 records so it should only return: Manchester Altrincham

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1217996
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.