Change value of second select box according first select box.

How to change value of second select box according first select box.manually is easy, basicaly the same thing as for the first just with a different SQL request.

function getState_add(countryId)
{
ajax= nuevoAjax();
var url="getstate_add.php";
url=url+"?id="+countryId;
//alert(url);
ajax.open ("GET",url, true);
ajax.onreadystatechange= function()
{
if (ajax.readyState == 4)
{
//alert(ajax.responseText);
document.getElementById("getState").innerHTML = ajax.responseText;
var val = ajax.responseText;
}
}
ajax.send (null);
}
function nuevoAjax()
{
var xmlhttp=false;
try
{
xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
catch(E)
{
xmlhttp=false;
}
}
if (!xmlhttp && typeof XMLHttpRequest!='undefined')
{
xmlhttp=new XMLHttpRequest();
}
return xmlhttp;
}
view raw address_add.js hosted with ❤ by GitHub
<?php
if(isset($_GET['id']))
{
$iCountryId = $_GET['id'];
$qState = mysql_query("select * from states where StateCtrCode= '$iCountryId' order by StateName desc");
?>
<select name="ddState" id="ddState" class="just_textfield">
<?php
while ($aState = mysql_fetch_array($qState))
{?>
<option value="<?php echo $aState['StateCode'];?>"><?php echo $aState['StateName'];?></option>
<?php
}
?>
</select>
<?php } ?>
------------------- First Select box -----------
<select name="ddCountry" id="ddCountry" class="just_textfield" onChange="getState_add(this.value)">
<option value="0" selected="selected">Please select---</option>
<?php
$qGetCompanies = mysql_query("SELECT * FROM countries");
while ($GetCompanies = mysql_fetch_array($qGetCompanies))
{
?>
<option value="<?php echo $GetCompanies['ctr_code']; ?>" <? if($GetCompanies['ctr_code'] == "US"){echo ' selected="selected"';}?><?php echo $GetCompanies['ctr_name']; ?></option>
<?php
}
?>
</select>
------------------- Second Select box -----------
<?php
$sCountryCode = 'US';
$sTempState = mysql_query("select * from states where StateCtrCode = '$sCountryCode'");
?>
<select id="ddState" name="ddState" class="just_textfield" >
<?php
while ($aTempState = mysql_fetch_array($sTempState))
{?>
<option value="<?php $aTempState['StateCode'];?>"><?=$aTempState['StateName'];?></option>
<?php }?>
</select>

Comments

Popular posts from this blog

Login with facebook using coldfusion

Create CSV file in Coldfusion Using CFFile

Get Previous One Day Data in Sql Server