zimmo Posted May 20, 2011 Share Posted May 20, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/ Share on other sites More sharing options...
zimmo Posted May 20, 2011 Author Share Posted May 20, 2011 Just to add to this, its only doing it for the one city/town. I have added more records to the database, but its only the first one that is returning 2 entries. Not sure why this is happening? Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1217989 Share on other sites More sharing options...
Adam Posted May 20, 2011 Share Posted May 20, 2011 Try running the PHP scripts directly, so that you can narrow the problem down to the PHP or JS. Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1217991 Share on other sites More sharing options...
zimmo Posted May 20, 2011 Author Share Posted May 20, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1217996 Share on other sites More sharing options...
Adam Posted May 20, 2011 Share Posted May 20, 2011 That's.. a little odd. Okay let's narrow it down further; query the SQL directly on the database server: select distinct club_town from clubs WHERE club_county = 'Greater Manchester' Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1218000 Share on other sites More sharing options...
PFMaBiSmAd Posted May 20, 2011 Share Posted May 20, 2011 Your echo statement is inside your while(){} loop. It should be after the end of your loop. Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1218001 Share on other sites More sharing options...
Adam Posted May 20, 2011 Share Posted May 20, 2011 Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1218002 Share on other sites More sharing options...
zimmo Posted May 20, 2011 Author Share Posted May 20, 2011 Its been a very long week ha ha... thanks for spotting the obvious... Quote Link to comment https://forums.phpfreaks.com/topic/236954-ajax-and-php-issue/#findComment-1218006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.