Jump to content

Ajax state>city dropdown troubles


rondog

Recommended Posts

I made an ajax dropdown that is making calls to my DB selecting first state then the next dropdown shows all the cities in that state. It works fine by itself. I now need to put it into my admin page. The problem is my ajax call to my remote script is just using echo. Well I cant use just echo on my admin page because it places it on the top of my page so I am making every equal to $content .= "blah blah" then I echo $content at the very end.

 

You can see the working example here: http://drewestate.com/new/2008/admin/state_dropdown.php

here is an example what its doing on my admin page: http://drewestate.com/new/2008/admin/storeeditor.php?s=Alabama&c=Birmingham&storeID=4

 

 

I just need help getting it into my admin page. On my admin page it calling the ajax fine because I can see the header response in firebug. It just wont output to my select drop down menus.

<?php
if(isset($_GET['storeID'])) {
$query = mysql_query("SELECT * FROM de_dealers WHERE id = '".$_GET['storeID']."'") or die(mysql_error());
$content = "<fieldset><legend>You are editing inside of <b>".str_replace("_"," ",$_GET['c']).", ".str_replace("_"," ",$_GET['s'])."</b>  |  <a href=\"javascript:history.go(-1)\"><img src=\"../images/folderup.gif\" border=\"0\" /></a>  |  <a href=\"storeeditor.php\">Main</a></legend>";
  	$row = mysql_fetch_array($query);
$content .= "<table><form name=\"sel\" method=\"POST\">\n";
$content .= "<tr><td>$status</td></tr>\n";

$content .= "<tr><td>State</td><td><select name=\"states\"><option id=\"states\" value=\"0\">=== none ===</option></select></td></tr>\n";
$content .= "<tr><td>City</td><td><select name=\"cities\"><option id=\"cities\" value=\"0\">============</option></select></td></tr>\n";

$content .= "<tr><td>Zip</td><td><input type=\"text\" name=\"ziptxt\" id=\"textfield4\" value=\"$row[zip]\" disabled=\"\" size=\"35\"/></td></tr>\n";
$content .= "<tr><td colspan=\"2\"><div align=\"right\"><input name=\"savechanges\" type=\"submit\" value=\"Save Changes\"/></div></td></tr>\n";
$content .= "</form></table></fieldset>";
?>

 

 

state.php (gets called by the ajax function):

<?php
//set IE read from page only not read from cache
include '../connect.php';
header ("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header ("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header ("Cache-Control: no-cache, must-revalidate");
header ("Pragma: no-cache");

header("content-type: application/x-javascript; charset=tis-620");

$data = $_GET['data'];
$val = $_GET['val'];

if ($data=='states') {  // first dropdown
echo "<select name='states' onChange=\"dochange('cities', this.value)\">\n";
echo "<option value='0'>==== choose state ====</option>\n";
$result = mysql_query("SELECT DISTINCT state FROM de_dealers ORDER BY state ASC");
while($row = mysql_fetch_array($result)){
   echo "<option value=\"$row[state]\">$row[state]</option> \n" ;
}
} else if ($data=='cities') { // second dropdown
echo "<select name='cities' >\n";
echo "<option value='0'>====choose cities ====</option>\n";                           
$result = mysql_query("SELECT DISTINCT city FROM de_dealers WHERE state = '$val' ORDER BY city ASC");
while($row = mysql_fetch_array($result)){       
   echo "<option value=\"$row[city]\">$row[city]</option> \n" ;
}
} 
echo "</select>\n";  
?>

 

My ajax call (You probably dont need this)

<script language=Javascript>
function Inint_AJAX() {
   try { return new ActiveXObject("Msxml2.XMLHTTP");  } catch(e) {} //IE
   try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {} //IE
   try { return new XMLHttpRequest();          } catch(e) {} //Native Javascript
   alert("XMLHttpRequest not supported");
   return null;
};

function dochange(src, val) {
     var req = Inint_AJAX();
     req.onreadystatechange = function () { 
          if (req.readyState==4) {
               if (req.status==200) {
                    document.getElementById(src).innerHTML=req.responseText; //retuen value
               } 
          }
     };
     req.open("GET", "state.php?data="+src+"&val="+val); //make connection
     req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=iso-8859-1"); // set Header
     req.send(null); //send value
}

window.onLoad=dochange('states', -1);         // value in first dropdown
</script>

Link to comment
https://forums.phpfreaks.com/topic/107133-ajax-statecity-dropdown-troubles/
Share on other sites

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.