【问题标题】:Get city id from ajax generating city select box to my original php page从 ajax 生成城市选择框获取城市 ID 到我原来的 php 页面
【发布时间】:2014-11-06 19:34:36
【问题描述】:

在我的注册表单中,有许多表单元素可以提供用户在我的网站上注册的详细信息。在这些元素中,有两个选择框供用户选择他们的地区和城市。我使用 ajax 创建了这两个选择框。因此,用户选择一个地区,然后自动 ajax 为城市创建第二个选择框正在填充。我使用名为 findcity.php 的单独 PHP 页面来创建城市选择框。我通过 onChange 属性从我原来的 register.php 页面调用了这个 findcity.php 页面。然后我将带有 url 的地区 ID 传递给 findcity.php 页面。同样,

现在,当用户从 findcity.php 页面的城市选择框中选择一个城市时,我需要将城市 ID 带到我原来的 register.php 页面。我的问题是。我试图让城市 ID 到 register.php 页面,但我仍然无法得到它。我需要城市 ID 与其他表单元素的值一起发送到数据库。

谁能帮我解决我的问题?

这是我的代码供您参考。

此代码来自我的 register.php 页面

<div>
<label for="district">District <img src="../images/required_star.png" alt="required" /> : </label>
<?php

require_once ('../includes/config.inc.php');    
require_once( MYSQL2 );

$query="select * from district order by district_id";
$result = mysqli_query( $dbc, $query);

    echo '<select name="district" class="text" onChange="getCity(' . "'" . 'findcity.php?district=' . "'" . '+this.value)">';
    echo '<option value="">-- Select District --</option>';

    while( $row = mysqli_fetch_array($result, MYSQLI_NUM)) { 
        echo '<option value="' . $row[0] . '"';

        // Check for stickyness: 
        if ( isset( $_POST['district']) && ( $_POST['district'] == $row[0] ))    
            echo ' selected="selected"';

            echo " >$row[1]</option>";    
    }
    echo '</select>';
?> 

</div>    
<div>
<label for="city">City <img src="../images/required_star.png" alt="required" /> : </label>
<input type="hidden" name="reg_locationid" id="reg_locationid" value="56" />
<div id="citydiv" style="position: relative; top: -14px; left: 130px; margin-bottom: -26px;">
    <select name="city" class="text">
        <option>-- Select City --</option>
    </select>
</div>
</div>

这是,来自我的 findcity.php 页面

<?php

$districtId=$_GET['district'];

require_once ('../includes/configaration.inc.php'); 
require_once( MYSQLCONNECTION );

$query="select city_id, city_name from city2 where district_id=$districtId";
$result=mysqli_query( $dbc, $query);



echo '<select name="city" class="text">
    <option>-- Select City --</option>';

while($row=mysqli_fetch_array($result, MYSQLI_NUM)) { 

echo '<option value="' . $row[0] . '"';

// Check for stickyness: 
if ( isset( $_POST['city']) && ( $_POST['city'] == $row[0] )) { 
    echo ' selected="selected"';

    //echo '<input type="hidden" name="city"  value="' . $row[0] . '"'; 

}
    echo " >$row[1]</option>";  

}

echo '</select>';

?>

这些是 ajax 函数

function getXMLHTTP() { //function to return the xml http object
var xmlhttp=false;  
try{
    xmlhttp=new XMLHttpRequest();
}
catch(e)    {       
    try{            
        xmlhttp= new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e){
        try{
        xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch(e1){
            xmlhttp=false;
        }
    }
}

return xmlhttp;
}



function getCity(strURL) {      

    var req = getXMLHTTP();

    if (req) {

        req.onreadystatechange = function() {
            if (req.readyState == 4) {
                // only if "OK"
                if (req.status == 200) {                        
                document.getElementById('citydiv').innerHTML=req.responseText;                      
            } else {
                alert("There was a problem while using XMLHTTP:\n" + req.statusText);
            }
        }               
    }           
    req.open("GET", strURL, true);
    req.send(null);
}

}

非常感谢任何 cmets。

【问题讨论】:

  • 它给你带来了什么错误?
  • 没有任何错误。只是我需要将 cityId 获取到我的 register.php 页面..
  • 当您选择一个地区时,城市下拉列表是否填充?
  • 是...当用户选择一个地区时,我的城市下拉列表会根据该所选地区进行填充。它对我有用.. 我的问题是当我尝试获取用户选择的城市名称或其 id 时。

标签: php ajax


【解决方案1】:

您的 findcity.php 页面中似乎没有选择该城市!

在替换之前也可以尝试清理 citydiv 的 innerhtml。

【讨论】:

    猜你喜欢
    • 2016-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多