0

この例を試してみましたが、どこが間違っているのかわかりません。データベースと相対フィールドを作成しました。UK を選択すると、空白の結果が表示されます。これが私のスクリプトです

main.php

<?php
$connect=mysql_connect("localhost","root","") or die("Cannot connect");
$dbselect=mysql_select_db("test");
$sql_country = "SELECT * FROM COUNTRY";
$result_country = mysql_query($sql_country) or die("Query problem");

echo "<form method='POST'>";
echo "<select name='country' onChange='get_cities(this.value)'>"; //get_cities is defined below
echo "<option value=''></option>";
while($row_country = mysql_fetch_array($result_country))
{
echo "<option value='".$row_country['id']."'>".$row_country['country']."</option>";
}
echo "</select>";

echo "<select name='city' id='city'></select>"; //We have given id to this dropdown
echo "</form>";
?>
<script type="text/javascript">
function get_cities(country_id)
{
$.ajax({
type: "POST",
url: "cities.php", /* The country id will be sent to this file */
beforeSend: function () {
$("#city").html("<option>Loading ...</option>");
},
data: "country_id="+country_id,
success: function(msg){
$("#city").html(msg);
}
});
}
</script>

都市.php

<?php
// Code for cities.php
$connect=mysql_connect("localhost","root","") or die("Cannot connect");
$dbselect=mysql_select_db("test");
$country_id = $_REQUEST['country_id'];

$sql_city = "SELECT * FROM CITY WHERE country_id = '".$country_id."'";
$result_city = mysql_query($sql_city);
echo "<select name='city'>";

while($row_city = mysql_fetch_array($result_city))
{
echo "<option value='".$row_city['id']."'>".$row_city['city']."</option>";
}

echo "</select>";

?>

サンプルコード: http://www.x-developer.com/php-scripts/loading-drop-downs-with-ajax-php-and-fetching-values-from-database-without-refreshing-the-page

4

5 に答える 5

1

あなたの例にはエラーチェックがないようです。データベースでは、多くのことがうまくいかない可能性があることに注意してください!

これらの修正を行ってみてください。これを変える:

$connect=mysql_connect("localhost","root","");

に:

$connect=mysql_connect("localhost","root","") or die("Cannot connect");

この:

$result_country = mysql_query($sql_country);

に:

$result_country = mysql_query($sql_country) or die("Query problem");

これにより、指定されたテーブルが接続されているか見つからないという 2 つの一般的な問題が浮き彫りになります。


また、cities.phpデータベースにまったく接続していないようです。最初のファイルの と を 2 番目のファイルに追加するmysql_connectと、mysql_select_db正しく接続されます。


最後に、このチュートリアルには深刻なセキュリティ上の脆弱性があり、インターネット ユーザーがデータベース サーバーで任意の SQL を実行できる可能性があります。これを変える:

$country_id = $_REQUEST['country_id'];

これに:

$country_id = (int) $_REQUEST['country_id'];

これにより、国 ID を無批判に文字列として受け入れるのではなく、強制的に整数にすることができます。

于 2013-03-17T06:54:42.373 に答える
0

ajax 駆動の Web アプリケーションに関しては非常に役立つ jquery を使用することにオープンである場合は、jquery コードでこれを試してください。

//ensure the select name 'country' has the id 'country'
$("#country").change(function(){
//get country id of the selected country option
var countryid = $("#country option:selected").val();
//here we post the id of the country to the php page. note that within the curly braces, the first parameter is what corresponds to the variable in php e.g $countryid=$_POST[country_id]. The second parameter is the javascript variable
$.post("cities.php",{country_id:countryid},function(data){
//check that the json object returned is not empty
if(!$.isEmptyObject(data))
       { 

     var html = '';
    var len = data.length;
    for (var i = 0; i< len; i++) {
        var cityid = [data[i].id];
        var cityname = [data[i].city];      
        html += '<option value = '+cityid+'>'+city+'</option>';

    }
//append the results of the query. The prepend part is to make a blank option first and make it selected so as to force a user to actually select a city.
       $("#city").html(html).prepend("<option value='' selected='selected'></option>").show();

        }   

        else{
             alert('No cities found for this country');
            }

},'json')
})

都市のphpページはそのままですが、次の調整を行います

<?php
// Code for cities.php
$connect=mysql_connect("localhost","root","") or die("Cannot connect");
$dbselect=mysql_select_db("test");
//change it from request to post.
$country_id = $_POST['country_id'];
//the below code is not safe!!! You are open to sql injection. To make it worse you are connected as root. A malicious user can drop your database. 
$sql_city = "SELECT * FROM CITY WHERE country_id = '".$country_id."'";
$result_city = mysql_query($sql_city);

while($row_city = mysql_fetch_array($result_city))
{
$data[] = $row_city;
}
//return the json onject to the client.
echo json_encode($data);

?>

上記のコードは、javascript を記述する jquery の方法を選択する場合に役立つ場合があります。最初に jquery をダウンロードする必要があります。

于 2013-03-17T08:25:25.210 に答える
0

サーバーからの応答にはドロップダウン メニューが含まれていますが、現在のコードは HTML の結果を div または span ではなく、別のドロップダウン メニューに追加するため、次のようになります。

<select name='city' id='city'><select name='city'>....</select> </select>

これは正しくありません。これを回避するには、次のように変更します。

echo "<select name='city' id='city'></select>";

echo "<div id='city'> </div>";

または、echo "<select>"citys.php から行を削除します

<?php
// DB CONNECTION
$country_id = $_REQUEST['country_id'];

$sql_city = "SELECT * FROM CITY WHERE country_id = '".$country_id."'";
$result_city = mysql_query($sql_city);


while($row_city = mysql_fetch_array($result_city))
{
echo "<option value='".$row_city['id']."'>".$row_city['city']."</option>";
}



?>
于 2013-03-17T06:57:47.533 に答える
0

あなたcities.phpにはデータベース接続がありません。そこにもDB接続を書き込んでください。その後、ドロップダウンが表示されます。

于 2013-03-17T06:59:30.200 に答える
0

にはデータベース接続がないcities.phpため、SELECTステートメントは機能しません。

于 2013-03-17T07:16:51.130 に答える