2014-09-03 63 views
0

我需要在选择任何国家/地区后在frmState下拉框中预先填充状态详细信息。 但我是新来的ajax,我没有足够的知识。你能帮我做这个例程吗?它在下面给出。 它可能值得很多。提前致谢。在PHP中使用ajax预填充相应国家/地区的详细信息

我在MySQL的两个表..

tbl_country

columns: country_id(auto inc id) | country_name | country_code 

tbl_states

columns: state_id(auto inc id) | state_name | country_name 

HTML代码

<select class="txtselectcomp" name="frmCountry" id="frmCountry" onChange="showHint(this.value);" onblur="checkEmptyData('frmCountry');" required="required" > 
<option value="">Select country</option> 
    <?php 
for ($i = 0; $i < count($strCountries); $i++) { ?> 
     <option value="<?php 
    echo $strCountries[$i]['country_name']; ?>" <?php 
    if ($_POST['frmCountryName'] == $strCountries[$i]['country_name']) { ?> selected="selected" <?php 
    } ?>><?php 
    echo ucfirst($strCountries[$i]['country_name']); ?></option> 
    <?php 
} ?> 
</select> 

<select class="txtselectcomp" name="frmState" id="frmState" onblur="checkEmptyData('frmState');" required="required" > 
<option value="">Select State</option> 
</select> 

阿贾克斯脚本

<script language="javascript"> 
function showHint(str) 
{ 
    var xmlhttp; 
    if (str.length==0) { 
     document.getElementById("frmState").innerHTML=""; 
     return; 
    } 
    if (window.XMLHttpRequest) { 
     xmlhttp=new XMLHttpRequest(); 
    } 
    else { 
     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 
    } 
    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState == 4 && xmlhttp.status==200) { 
      document.getElementById("frmState").innerHTML=xmlhttp.responseText; 
     } 
    } 
    xmlhttp.open("GET","findState.php?value="+str,true); 
    xmlhttp.send(null); 
} 
</script> 

功能

$strCountries = doSelectRecordsById('tbl_country', 'country_id, country_name', 'country_id', ' order by country_name ASC', ''); 

function doSelectRecordsById($strTableName, $strSelect='*', $strTableId, $strOrderBy = NULL, $strFieldValue = NULL) 
    { 
     if ($strFieldValue != NULL) { 
      $strWhereClause = " WHERE $strTableId = '".(int)$strFieldValue."'"; 
     } 

     $strSqlRecords = "SELECT $strSelect FROM $strTableName $strOrderBy $strWhereClause"; 
     $strSqlData = SelectQry($strSqlRecords); 

     if ($strWhereClause == '') { 
      return $strSqlData; 
     } else { 
      return $strSqlData[0]; 
     } 
    } 

function doSelectState($strIdent) 
    { 
     $strSelectsSql = "Select * from tbl_states Where country_name='".$strIdent."' "; 
     $strSelectsResult = SelectQry($strSelectsSql); 
     return $strSelectsResult[0]; 
    } 

findState.php

<?php 
    include ("config.php"); 
    $country_name = $_GET["value"]; 
    $sql = doSelectState($country_name); 
    ?> 
     <select name="State" id="State"> 
     <?php 
    for ($i = 0; $i < count($sql); $i++) { 
     $id = $sql[$i]['state_id']; 
     $state = $sql[$i]['state_name']; 
     echo '<option value="' . $id . '">' . $state . '</option>'; 
    } 
    ?> 
    </select> 

回答

1
<?php 
include ("config.php"); 
$country_name = $_GET["value"]; 
$sql = doSelectState($country_name); 
?> 
    <select name="State" id="State"> 
    <?php 
for ($i = 0; $i < count($sql); $i++) { 
    $id = $sql[$i]['state_id']; 
    $state = $sql[$i]['state_name']; 
    echo '<option value="' . $id . '">' . $state . '</option>'; 
} 

?>

在这里你不必创建一个select元素。你可以不喜欢它

<?php 
include ("config.php"); 
$country_name = $_GET["value"]; 
$sql = doSelectState($country_name); 
for ($i = 0; $i < count($sql); $i++) { 
    $id = $sql[$i]['state_id']; 
    $state = $sql[$i]['state_name']; 
    echo '<option value="' . $id . '">' . $state . '</option>'; 
} 

?>

使返回数据时,浏览器,你会得到的选项字段,而不是选择这对于选择内部HTML。你现在想要做的是插入SELECT元素来选择元素

相关问题