twilitegxa Posted January 31, 2010 Share Posted January 31, 2010 I can't get my script to work. I have a js that runs a php script and displays the data in the div tag. The php script populates the select option list. But I can't get select list to populate with the right data. I want the previous list to determine which values display in the next select list. If I take out the where statement, all the values display from the table, but when I put in the where statement, none of the values display. What am I doing wrong? HTML: <?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> <br /> <div id="txtHint3"></div> <br /> <div id="txtHint"></div> The second JS section, where it says to display the monsters, is the part I'm having trouble with. As in, it associates with the part I'm havign trouble with. JavaScript: //code to display locations 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; } //code to display monsters var xmlhttp3; function showMonsters(str) { xmlhttp3=GetXmlHttpObject(); if (xmlhttp3==null) { alert ("Browser does not support HTTP Request"); return; } var url3="getmonsters.php"; url3=url3+"?q="+str; url3=url3+"&sid="+Math.random(); xmlhttp3.onreadystatechange=stateChanged3; xmlhttp3.open("GET",url3,true); xmlhttp3.send(null); } function stateChanged3() { if (xmlhttp3.readyState==4) { document.getElementById("txtHint3").innerHTML=xmlhttp3.responseText; } } function GetXmlHttpObject3() { if (window.XMLHttpRequest3) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest3(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); } return null; } I can't figure out why my location is not being pushed through when the select list is changed. 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 location = '".$q."'"; $result = mysql_query($sql); echo "<table border='0'>"; echo "Select a monster: <select name=\"monsters\"> <option selected=selected>None Selected</option>"; while($row = mysql_fetch_array($result)) { echo "<option>" . $row['location'] . "</option>"; } echo "</select>"; mysql_close($con); ?> Can anyone help? I need the monsters select list to be populated with the name field values according to which location is chosen from the locations select list. Can anyone help me figure out why? Quote Link to comment Share on other sites More sharing options...
jl5501 Posted January 31, 2010 Share Posted January 31, 2010 have you checked ecactly what is being passed to your initial js function as str? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted January 31, 2010 Author Share Posted January 31, 2010 No, I'm not exactly good with JS yet. I found this example online and used it, modifying the parts I needed. But the part I'm really confused about it that this JS worked on another example similar, so I figured it should work on this one, too. I have two select lists that should pop up and they do. The first one is on the page from the beginning, but the second one appears on the change of the first one, with the correct data according to what was selected from the list. The second select list should, on change, also populate another select list, but the data is wrong in this instance for some reason. When I check to see what value $q is getting, it appears to be that option that was selected from the second select list, which is right, because the where statement should get all the records from the table where the location field matches the value that they selected from the select list. But my other statement should be asking for the name field value, to display according to whatever location they chose from the last select list, but it won't display anything. For some reason, the name field can't be retrieved when I use the where statement. But, if I take the where statement out, the select list will populate with all the name field values from the table. So I need the where statement to make it only display all the name field values according to what location they picked. Can anyone see what I have done wrong? I think it must be in my php, since the JS seemed to be getting the right value. Like I said before, my other select list and JS works just fine. Here is the previous select list's JS and PHP: JS: //code to display locations 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; } 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 map = '".$q."' group by location"; $result = mysql_query($sql); echo "<table border='0'>"; echo "Select a location: <select name=\"locations\" onchange=\"showMonsters(this.value)\"> <option selected=selected>None Selected</option>"; while($row = mysql_fetch_array($result)) { $location = $row['location']; echo "<option value=\"$location\">" . $row['location'] . "</option>"; } echo "</select>"; mysql_close($con); ?> This code gets all the records from the same table where the map value has been chosen and then displays all the locations that have that map value in the map field. I just figured, if this one is working properly, the other one should be, too, but I can't get it to work. :-( Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted January 31, 2010 Author Share Posted January 31, 2010 Well, I added a group by name section to my where statement, and now I realize that some of the names are coming into the select option list, bt not all of them are the right ones. Some of them are from other locations, so I guess this has something to do with the JS that adds a random number to prevent the server from using a cached file. On one of the location options, it generates no options, and instead seems to be putting them with another location for some reason. My guess is that it's the JS, so I will try posting a question there about it, but if anyone knows what I's doing wrong here, please let me know. It's driving me crazy! Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted January 31, 2010 Author Share Posted January 31, 2010 I figured out the problem. Some of my name values have apostrophes in them. How do I use the msql_real_escape_string with the statements that display the names? Here's where is displays the name: $sql="SELECT * FROM monsters1 WHERE location = '".$q."' group by name"; $result = mysql_query($sql); echo "<table border='0'>"; echo "Select a monster: <select name=\"monsters\"> <option selected=selected>None Selected</option>"; while($row = mysql_fetch_array($result)) { echo "<option>" . $row['name'] . "</option>"; } echo "</select>"; Quote Link to comment Share on other sites More sharing options...
jl5501 Posted January 31, 2010 Share Posted January 31, 2010 Would it work better for you to use numeric values in your select, which you could do if you have a numeric id field in your database so your options would look like <option value="<?php echo $numericid;?>"><?php echo $your title;?></option> or with your 'all php approach echo '<option value="' . $row['id'] . '">' . $row['name'] . '</option>'; Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted February 25, 2010 Author Share Posted February 25, 2010 Thank you, I will try this out. Thanks for the suggestion. Quote Link to comment 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.