Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从ajax生成城市选择框获取城市id到我的原始php页面_Php_Ajax - Fatal编程技术网

从ajax生成城市选择框获取城市id到我的原始php页面

从ajax生成城市选择框获取城市id到我的原始php页面,php,ajax,Php,Ajax,在我的注册表单中,有许多表单元素可以提供用户在我的网站中注册的详细信息。在这些元素中,有两个选择框供用户选择其所在地区和城市。我使用ajax创建了这两个选择框。因此,用户选择一个地区,然后自动为城市创建第二个选择框。我使用名为findcity.PHP的单独PHP页面创建城市选择框。我通过onChange属性从原始register.php页面调用了这个findcity.php页面。在那里,我将带有url的地区id传递到findcity.php页面。像怀斯一样 现在,当用户从findcity.php

在我的注册表单中,有许多表单元素可以提供用户在我的网站中注册的详细信息。在这些元素中,有两个选择框供用户选择其所在地区和城市。我使用ajax创建了这两个选择框。因此,用户选择一个地区,然后自动为城市创建第二个选择框。我使用名为findcity.PHP的单独PHP页面创建城市选择框。我通过onChange属性从原始register.php页面调用了这个findcity.php页面。在那里,我将带有url的地区id传递到findcity.php页面。像怀斯一样

现在,当用户从findcity.php页面的城市选择框中选择城市时,我需要将城市id带到我的原始register.php页面。我的问题是。我试图让city 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>
<?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>';

?>

非常感谢您的评论。

您的findcity.php页面中似乎没有选择该城市,请检查


在替换之前,请尝试清除citydiv的innerhtml。

它会给您带来什么错误?没有任何错误。我只需要将cityId转到我的register.php页面..当你选择一个地区时,城市下拉列表是否正在填充?是的。。。当用户选择一个地区时,我的城市下拉列表将根据所选地区进行填充。这对我有用。。我的问题是当我尝试获取用户选择的城市名称或其id时。
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);
}

}