drop down menu - How to use AJAX to populate state list depending on Country list? -


I have the following code that will change the state's dropdown list when you change the list of countries.
How can I change this state's list only when country ID number 234 and 224 are selected?
If another country is chosen, then it should be changed to this text input box

  & lt; Input type = "text" name = "ostestate" value = "" class = "text box" & gt;  

Form

  & lt; Form method = "post" name = "form1" & gt; & Lt; Select style = "background-color: #FFFF 0" name = "country" onchange = "getState (this.value)" & gt; & Lt; Options & gt; Select country & lt; / Options & gt; & Lt; Option value = "223" & gt; USA & lt; / Options & gt; & Lt; Option value = "224" & gt; Canada & lt; / Options & gt; & Lt; Option value = "225" & gt; England & lt; / Options & gt; & Lt; Option value = "226" & gt; Ireland & lt; / Options & gt; & Lt; / Select & gt; & Lt; Select style = "background-color: #FFFF 0" name = "state"> & Lt; Options & gt; Select country first & lt; / Options & gt; & Lt; / Select & gt;  

JavaScript

  & lt; Script & gt; Function Millstate (Country ID) {var strURL = "findState.php? Country =" + Country ID; Var REQ = getXMLHTTP (); If (req) {req.onreadystatechange = function () {if (req.readyState == 4) {// only if "ok" if (req.status == 200) {document.getElementById ('saidiv'). 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;  

Just check the countryId value before requesting AJAX and only request if country The ID is in the acceptable range in the case where the country does not match the country, I have to hide the selection (perhaps its value is also clear) and show an already existing input which was previously hidden if an acceptable country is chosen if it is chosen to be reverse needed.

Example of a jQuery example below:

  & lt; Form method = "post" name = "form1" & gt; & Lt; Select style = "background-color: #FFFF 0" name = "country" onchange = "getState (this.value)" & gt; & Lt; Options & gt; Select country & lt; / Options & gt; & Lt; Option value = "223" & gt; USA & lt; / Options & gt; & Lt; Option value = "224" & gt; Canada & lt; / Options & gt; & Lt; Option value = "225" & gt; England & lt; / Options & gt; & Lt; Option value = "226" & gt; Ireland & lt; / Options & gt; & Lt; / Select & gt; & Lt; Select style = "background-color: #FFFF 0" name = "state"> & Lt; Options & gt; Select country first & lt; / Options & gt; & Lt; / Select & gt; & Lt; Input type = "text" name = "oststate" value = "" class = "text box" style = "display: none;" & Gt; & Lt; / Form & gt; $ (Function () {$ ('' country ''). Change (function () {var val = $ (this) .val (); if (val == 223 || val == 224) {$ (' # (''). Val (); $ .ajax ({url: 'findState.php', datatype: 'html', data: {country: vel}, success: work (data) { $ ('#state') .html (data);}})} other {$ ('# state'). Val (''). Hide (); $ ('# othstate'). Show ();} });});  

Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -