0

2 つの選択ドロップダウンがあるフォームがあります。1 つは国、もう 1 つは大学です。国ドロップダウンは、入力されたすべての国を示す MySQL データベースから取り込まれます。最初は、大学のドロップダウンにシステム内のすべての大学を入力したいと考えていますが、エンド ユーザーはシステムを動的にして進行に合わせて学習することを望んでいるため、現時点ではドロップダウンにデータは表示されません。

<select name="academicdropdown" onchange="countrysortlist()" style="width:178px">
<option value"">Sort By Country</option>
<option value"All" style="font-weight:bold; font-style:italic">All Countries</option>
<?php

while ($row=mysqli_fetch_array($countryresult2)) {

    $id = $row["country_id"];
    $country = $row["country"];
    echo "<option value='$id'>$country</option>\n";
}
?>
</select>

<select name="universitydropdown" onclick="unilist()" style="width:154px">
<option value"">University</option>
<?php

    if(isset($_SESSION['appformuniversity'])) {

    if($_SESSION['appformacademic'] == "universitylist") {

        $universityinput = $_SESSION['appformuniversitylist'];
                                                    while ($row=mysqli_fetch_array($universityresult)) {
                                                        $universityid = $row["university_id"];
                                                                $university = $row["university_name"];
                                                        if($universityid == $universityinput) {
                                                            echo "<option value='$universityid' selected='selected'>$university</option>\n";
                                                        }
        else {
                                                        echo "<option value='$universityid'>$university</option>\n";
                                                        }
    }
    }
else {
                                                while ($row=mysqli_fetch_array($universityresult)) {

        $universityid = $row["university_id"];
        $university = $row["university_name"];
        echo "<option value='$universityid'>$university</option>\n";
                                                    }
}
}
else {

   while ($row=mysqli_fetch_array($universityresult)) {

            $universityid = $row["university_id"];
    $university = $row["university_name"];
            echo "<option value='$universityid'>$university</option>\n";
   }
}
?>
</select></td></tr>

私がやりたいことは、ユーザーが国のドロップダウンで国のいずれかを選択すると、その国の大学で大学のドロップダウンが更新されることです。onchange イベントを使用する必要があることはわかっていますが、スクリプト側の操作方法がわかりません。どんな助けでも大歓迎です。

現時点での私の大学の質問は

$universitysql = "SELECT * FROM university ORDER BY university_name ASC" ;

助けてくれてありがとう。とても有難い。

4

2 に答える 2

1

JavaScript を使用すると、次のようなことができます。

<script type="text/javascript">
function countrysortlist()
{
   document.form_name.submit();
}
</script>

選択オプションを変更すると、サイトはフォームの送信を更新します

<form name='form_name' action='' method='get'>
<select name="academicdropdown" onchange="countrysortlist()" style="width:178px">
...
</select>
</form>

次に、クエリは送信された国 ID 内の大学のみを読み込みます。

<?php
$country_id = $_GET['academicdropdown'];
$universitysql = "SELECT * FROM university where countryid = '$country_id' ORDER BY university_name ASC" ;
?>
于 2012-04-19T16:18:25.847 に答える
0
You have to use ajax for this. If you know Jquery it would be easier. Just include the
jquery.js file . Then write the ajax in the onchange function of the country dropdown.
In the onchange method just pass the id of the country. Then in your ajax method send 
id to a php page. In that page generate the whole option list from the sql query using
that country id . Make a relation between two tables i.e every university row should 
contain country id. Then echo the option list. In your ajax method just change the 
university list dynamically using innerhtml. I am giving you example 

1. onchange on country select

 onchange="countrysortlist(this.value)"

2. ajax method in countrysortlist function

  $.ajax({
     type: 'POST',
     url: 'yourpage.php',  // change name
     data: "countryid="+id,  // country id
     success: function(data) {  // returns date
           $('.result').html(data); // result should be the class name of university dropdown

     }
 }); 
于 2012-04-19T15:49:34.110 に答える