0

データベースのテーブルからドロップダウンリストにデータを入力します。ユーザーが選択したドロップリストで選択したアイテムを取得しようとしています。これにより、データベースにクエリを実行して、そのアイテムに関連する詳細情報を取得できます。

<html>
<head></head>
<body>
<br>    
<table id ="topnav" width="1100" border="0" align="center">
    <tr>
    <td colspan="2" style="background-color:#FFA500;">
    <h1><table border="0">
    <tr>
    <th colspan="6"><img src ="../_images/Eu_flags.jpg" border="0" align="top" width="1100" height="180" />
    </th>
    </tr>

<table></h1>
    </td>
    </tr>

    <tr valign="top">
    <td style="background-color:#FFD700;width:150px;text-align:top;">


    <b>Menu</b><br />

    Select the state<br>
    See rank of Universities<br />
    Other information
    </td>
    <td style="background-color:#eeeeee;height:100px;width:940px;text-align:top;">
<?php
        $con = mysql_connect("localhost","root","pasword");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }

        mysql_select_db("test", $con);
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }


        $result = mysql_query("SELECT lnamestate FROM state" );

        while($row = mysql_fetch_array($result))
        {
        $lnamestate_id = $lnamestate['id'];
        $row_lnamestate=$row['lnamestate'];
        $row_block .= '<OPTION value="'.lnamestate_id.'">'.$row_lnamestate.'</OPTION>';
        }
        mysql_close($con);

?>  
        <label for="row">Select state</label>
        <select name="lnamestateID"><?php echo $row_block; ?></select>



    </td>
    </tr>

    <tr>
    <td colspan="2" style="background-color:#FFA500;text-align:center;">
    </td>
    </tr>
</table>



</table>

</body>

</html>
4

2 に答える 2

1

最初に、Select コントロールにイベント ハンドラーを追加して、何かが選択されたときにこのイベントをキャッチできるようにする必要があります。次の手順で実現できます。

<select name="ab" onchange="if (this.selectedIndex) doSomething();">

ここで、インデックスを保持して何が選択されているかを確認するか、必要に応じてインデックスを介して、選択された項目を取得し、JavaScript のグローバル変数に入れます。

アイテムが選択されたときにさらに情報をロードする場合は、xmlHttpRequest で ajax を使用します。多くの Web アプリケーションでは、JQuery を使用すると、多くのタスクが容易になります。こちらの JQuery をご覧ください。使い方は簡単で、多くのことを容易にします。

于 2012-04-04T03:47:57.683 に答える
0

AJAX が必要になります。

$('select.whatever').change(function(){
   $.ajax({
      url: '/path/to/your/script.php',
      data: 'var='+$(this).val(),
      type: 'POST',
      success: function(data) {
         $('.insert_element').html(data);
      }
   });
});

これは、変更されている要素の値を受け取り、それをスクリプトに送信し (スクリプトは DB にクエリを実行できます)、特定の要素のデータを返します。

于 2012-04-04T15:36:24.450 に答える