安らかに国"!だから、私はデータベースに接続し、最初のリスト「名前」でデータベースからデータを取得し、次にAjaxを使用して、最初のリストのオプションを選択した後、一致するデータを正常に取得し、「年齢」と呼ばれる2番目のリストに入れました、問題は、「年齢」と呼ばれる2番目のリストでまったく同じ方法を使用して、一致するデータを「国」と呼ばれる3番目のリストに取得すると、機能しないことです! 私はこの例を使用して Ajax を学習し、より大きな実際のプロジェクトに適用しています! ここにコードがあります:- まず、home.php ページ:-
<?php
include "config.php";
?>
<html>
<head>
<script type="text/javascript">
function agematch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('age').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'connection.inc.php?name='+document.ajax.name.value, true );
xmlhttp.send();
}
function countrymatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('country').innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open('GET', 'country.inc.php?age='+document.ajax.age.value, true );
xmlhttp.send();
}
</script>
</head>
<body>
<form id="ajax" name="ajax" >
Choose your name : <select name="name" id="name" select="selected" onchange="agematch();"> <option> </option>
<?php
$query = "SELECT DISTINCT name FROM info";
$result = mysql_query($query);
while($row = mysql_fetch_array($result)){
echo"<option value ='".$row[0]."'> '".@$row[0]."'</option>";
}
?>
</select>
Age : <select id="age" name="age" onchange="countrymatch();"> </select>
country : <select id="country" name="country"> <option> </option> </select>
</form>
</body>
</html>
今、最初の Ajax 呼び出しのページ:-
<?php
include "config.php";
echo " <option> </option> " ;
if(isset( $_GET['name']) ) {
@$name = $_GET['name'];
}
$query = "SELECT age FROM info WHERE name = '".@$name."' ";
$result = mysql_query($query);
while ($query_row = mysql_fetch_array($result)) {
echo " <option value ='".$query_row[0]."'> $query_row[0]</option> ";
}
?>
次に、3 番目のドロップ メニューの 2 番目の Ajax 呼び出しのページ:-
<?php
include "config.php";
if (isset( $_GET['age']) ) {
@$age=$_GET['age'];
}
$query = "SELECT country FROM info WHERE name='".@$name."' AND age='".@$age."' ";
$result= mysql_query($query);
while ($query_row = mysql_fetch_array($result)) {
echo " <option value = '".$query_row[0]."'> $query_row[0] </option> ";
}
?>
ご覧のとおり、コードは次のとおりです。もちろん、「config.php」というページを介してデータベースに接続しています。この問題を解決し、データベースから 3 番目のドロップにデータを取得する方法を教えてください。ダウンリスト「国」。前もって感謝します!
わかりました、ムサはここに編集があります:-
function countrymatch() {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
}
else {
xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange = function() {
if(xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById('country').innerHTML = xmlhttp.responseText;
}
}
var age = encodeUriComponent(document.ajax.age.value),
var name = encodeUriComponent(document.ajax.name.value),
xmlhttp.open('GET', 'country.inc.php?age='+age+'&name'+name, true );
xmlhttp.send();
}
そしてまた:-
<?php
include "config.php";
if (isset($_GET['age'], $_GET['name']) ) {
@$age=$_GET['age'];
@$name = $_GET['name'];
}
$query = "SELECT country from info where name='".@$name."' AND age='".@$age."' ";
$result= mysql_query($query);
while ($query_row = mysql_fetch_array($result)) {
echo " <option value = '".$query_row[0]."'> $query_row[0] </option> ";
}
?>
エラーメッセージは表示されません。あなたの指摘は正しいと思いますが、残念ながらこの解決策はうまくいきませんでした! 私を助けてくれてありがとう:)