so trying have website update state , city lists server.
my code states seems work fine, problem have updating city list. adapted answer here earlier, , doesn't seem work, although fault.
here state php code.
<?php $con= mysqli_connect($host, $user, $pass, $database); if($debug){ echo $host,$user,$pass,$database; } // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $states = ''; $resultstate = mysqli_query($con,"select distinct state citiesstates"); while($row = mysqli_fetch_array($resultstate)) { if($debug){ echo $row['state']; } $states .="<option>" . $row['state'] . "</option>"; } $statesdrop=" <p><label>states</label></p> <select name='states' id='states' onchange='getcity(this.value))'> " . $states . " </select>"; echo $statesdrop; mysqli_close($con); ?> so on selection should call function.
<script type="text/javascript"> function getcity(stateid) { var strurl="findcity.php?state="+stateid; var req = getxmlhttp(); if (req) { req.onreadystatechange = function() { if (req.readystate == 4) // if "ok" { if (req.status == 200) { document.getelementbyid('citydiv').innerhtml=req.responsetext; } else { alert("there problem while using xmlhttp:\n" + req.statustext); } } } req.open("get", strurl, true); req.send(null); } } </script> which calls php file.
$stateid=intval($_get['state']); $con= mysqli_connect($host, $user, $pass, $database); // check connection if (mysqli_connect_errno()) { echo "failed connect mysql: " . mysqli_connect_error(); } $cities = ''; $resultcity = mysqli_query($con,"select city citiesstates state='$stateid'"); while($row = mysqli_fetch_array($resultcity)) { if($debug){ echo $row['city']; } $cities .="<option>" . $row['city'] . "</option>"; } $citiesdrop=" <p><label>cities</label></p> <select name='cities' id='cities' onchange=''> " . $cities . " </select>"; echo $citiesdrop; mysqli_close($con); ?> additionally getxmlhttp() function, since seems problem
function getxmlhttp() { var x = false; try { x = new xmlhttprequest(); }catch(e) { try { x = new activexobject("microsoft.xmlhttp"); }catch(ex) { try { req = new activexobject("msxml2.xmlhttp"); } catch(e1) { x = false; } } } return x; }
i think due in findcity.php
$citiesdrop=" <p><label>states</label></p> <select name='states' id='states' onchange='getcity(this.value))'> " . $states . " </select>"; it looks copied state php code, did not update correctly. php page sending req.status of 500, $states not defined. try updating -
$citiesdrop=" <p><label>cities</label></p> <select name='cities' id='cities'> " . $cities . " </select>";
Comments
Post a Comment