0

http://img62.imageshack.us/img62/5582/20704606.jpg

上記のドロップダウン メニューには 3 つの値があります。

i)   Staf DC   
ii)  Admin
iii) Staf DT

「所有者」列の値 (hafiz) はデータベースから取得されます。ダウンダウンの各値には、異なる「所有者」値があります。このようにしたいです。

スタッフ DC が選択されている場合、次のクエリが実行されます。

$query = "SELECT * FROM owner where type='Staf DC'";

管理者が選択されている場合、次のクエリが実行されます。

 $query = "SELECT * FROM owner where type='Admin'";

また、表の「所有者」列の値は、ページを更新しなくても自動的に変更されます。誰かがこれを行う方法の例を教えてもらえますか?

4

2 に答える 2

4

以下のコードは、ドロップダウンの onchange で jquery 関数を呼び出します。Jquery 関数は、選択されたドロップダウン値を getdata.php に渡し、それを処理して所有者名をエコーし​​ます。次に、所有者の名前がラベル ボックスに表示されます。

ドロップダウン コード

echo '<table><tr><td>ModelNo.</td><td>';
echo "<select id='typeval' onchange='changeOwner();'>";
echo "<option value='Staf DC'>Staf DC</option>";
echo '</select></td>';
echo "<td><label id='own'>hafiz</label></td></tr></table>";

jqueryコード

<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
function changeOwner()
{
    var selname = $("#typeval option:selected").val();  
    $.ajax({ url: "getdata.php",

        data: {"selname":selname},

        type: 'post',

        success: function(output) {
            $("#own").html(output);
        }

    });
}
</script>

PHP コード (getdata.php);

$selname = $_POST['selname'];
$query = "SELECT * FROM owner where type='$selname'";
$res = mysql_query($query);
$rows = mysql_fetch_array($res);
echo $rows['owner']; //assumed that the column name in db is owner
于 2012-05-03T07:40:43.730 に答える
0

ページ全体を更新したくない場合は、Javascript/Ajax を使用する必要があります。JQuery では、必要なことを.post()メソッドで非常に簡単に実行できます。

まず、JQuery ファイルを HTML ヘッダーに追加します。

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js">

次に、リクエストを含む .php ファイルを作成します (例: update_owner.php):

<?php
    // 1. Connect to your SQL database
    // ...

    // 2. Get the type
    $type = $_POST['type'];

    // 3. Perform your query
    $results = mysql_query("SELECT * FROM owner where type=".$type);

    // 4. Get the only result you want (the first row)
    $row = mysql_fetch_array( $results );

    // 5. Return the result in json format (assuming that the name
    echo json_encode(array("responseCode" => 200, "row" => $row));

次に、いくつかの JavaScript (JQuery を使用) を追加します。

$("select#type").change(function() {
    // Get the value of your input field
    var type = $(this).val();

    // Set the URL
    var url = 'update_owner.php';

    // Start send the post request
    $.post(url,
        function(data){

            // The response is in the data variable
            if (data.responseCode == 200) {
                // Write the name in the right cell (with #owner id)
                $("#owner").html( data.row.name );
            }
            else {
                // Bad request
            }
        },"json"
    ); // Make sure the the response is in json format
    return false;
});

それはそれを行う必要があります

PS : 下手な英語で申し訳ありません... 私はフランス人です

于 2012-05-03T07:05:40.480 に答える