dropdown with php and ajax -
For some time I'm struggling to solve this problem, but I'm not coming to any conclusion, So thought here to get some help. The problem is that I'm getting an empty dropdown, instead I should get a list of cities with population databases. The database connection is fine, but I am not getting anything in my dropdown.
This is what I am doing:
& lt; Php is required / include / connect.php '; - Database connection $ country = $ _ request ['country']; - Get from form (index.php) $ q = "Choose city from city where countryid =". $ Country; $ Result = $ mysqli- & gt; Query ($ q) or die (mysqli_error ($ mysqli)); If ($ result) {? & Gt; & Lt; Select name = "city" & gt; & Lt; Options & gt; Select city & lt; / Options & gt; $ Id = 0; & Lt ;? Php while ($ line = $ result-> fetch_object ()) {$ src = $ row- & gt; city; $ Id = $ id + 1; ? & Gt; & Lt; Option value = & lt ;? Php $ id? & Gt; & Gt; & Lt ;? Php $ src? & Gt; & Lt; / Options & gt; & Lt ;? Php}? & Gt; & Lt; / Select & gt; & Lt ;? Php}? & Gt; The AJAX script is:
& lt; Script & gt; Function getXMLHTTP () {// function xml http object var xmlhttp = false; Try {mlhttp = new XMLHttpRequest ();} Catch (E) {try {xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP"); } Hold (e) {try {xmlhttp = new ActiveXObject ("Msxml2.XMLHTTP"); } Hold (E1) {xmlhttp = false; }}} Return xmlhttp; } Getcity function (strURL) {var req = getXMLHTTP (); If (req) {req.onreadystatechange = function () {if (req.readyState == 4) {if (req.status == 200) {document.getElementById ('citydiv'). InnerHTML = req.responseText; } Else {alert ("There was a problem using XMLHTTP: \ n" + req.statusText); }}} Req.open ("GET", strURL, true); Req.send (zero); }} & Lt; / Script & gt; This is my form code:
& lt; Form method = "post" action = "" name = "form1" & gt; & Lt; Table width = "60%" range = "0" cells = "0" cellpadding = "0" & gt; & Lt; TR & gt; & Lt; Td width = "150" & gt; Country & lt; / Td> & Lt; Td width = "150" & gt; & Lt; Select name = "country" onChange = "getCity ('findcity.php? Country =' + this.value)" & gt; & Lt; Option value = "" & gt; Select country & lt; / Option & gt; & Lt; Option value = "1" & gt; New Zealand & lt; / Options & gt; & Lt; Option value = "2" & gt; Canada & lt; / Options & gt; & Lt; / Select & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; Tr style = "" & gt; & Lt; TD & gt; City & lt; / TD & gt; & Lt; Td> & Lt; Div id = "citydiv" & gt; & Lt; Select name = "city" & gt; & Lt; Options & gt; Select city & lt; / Options & gt; & Lt; / Select & gt; & Lt; / Div & gt; & Lt; / TD & gt; & Lt; / TR & gt; & Lt; / Table & gt; & Lt; / Form & gt;
I think the problem is that you & lt; Option & gt; Tags
Your & lt; Select & gt; Try to use this block of code between tags.
& lt; Option & gt; Choose City & lt; / Options & gt; & Lt ;? Php $ id = 0; While ($ line = $ result-> fetch_object ()) {$ src = $ row- & gt; city; $ Id = $ id + 1; ? & Gt; & Lt; Option value = "& lt ;? php echo htmlspecialchars ($ id, ENT_QUOTES)? & Gt;" & Gt; & Lt ;? Php HTC htmlspecialchars ($ src)? & Gt; & Lt; / Option & gt; & Lt ;? Php}? & Gt; Edit: To clarify, you did not have the echo statement before $ id and $ src < / Code> Variable I added htmlspecialchars () as a habit to avoid HTML.
Comments
Post a Comment