gpt4 book ai didi

php - 在 PHP 中使用 ajax 预填充相应国家/地区的州详细信息

转载 作者:行者123 更新时间:2023-11-29 12:44:27 25 4
gpt4 key购买 nike

选择任何国家/地区后,我需要在 frmState 下拉框中预填充州详细信息。但我是ajax新手,对它的了解还不够。你能帮我完成这个日常工作吗?下面给出。它可能值很多钱。提前致谢。

我在 mySql 中有两个表..

tbl_国家

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

tbl_状态

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>

Ajax 脚本

<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>

最佳答案

<?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>';
}

?>

在这里您不必创建选择元素。你可以这样做

<?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 元素插入到选择元素中

关于php - 在 PHP 中使用 ajax 预填充相应国家/地区的州详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25642784/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com