Web サイトの登録フォームを作成しています。私の登録フォームには、ユーザーの地区と都市を選択するための 2 つの選択ボックスがあります。ユーザーが自分の地区を選択すると、選択した上記の地区に関連する都市を含む都市選択ボックスが自動的に表示されます。これを行うために、AJAX と PHP を使用しました。findcity.php ページを使用して、register.php ページに都市を表示しました。私の問題は、register.php ページから都市 ID を取得しようとしたときに取得できないことです。register.php ページからデータベースに他のデータと共に送信する必要があります。
私の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);
}
}