簡體   English   中英

使用Ajax從mysql DB檢索和打印數據

[英]Retrieve and print data from mysql DB using ajax

我有以下文本字段-

<input type="text" name="id" id="id" tabindex="1">
<input type="text" name="name" id="name" tabindex="1"/>

而mysql數據庫的屏幕截圖是-

id          name
123         John
124         Rahi

當我在第一個文本字段中插入曲目ID時,相應的學生姓名應顯示在第二個字段中。 如何使用ajax完成?

嘗試這個:

>> Trigger onblur event (javascript function) for the first textbox.
>> Define ajax in that function to get name.
>> Show the name in textbox.

I think this is simplest way to get this.

- 謝謝

您可以在文本框的onblur()事件上調用jquery函數。

<input type="text" name="id" id="id" tabindex="1" onblur="getname()">

編寫ajax函數以獲取如下名稱:

<script>
function getname()
{
var id=$("#id").val();    // get the id from textbox
$.ajax({
        type:"post",
        dataType:"text",
        data:"id="+id,
        url:"page.php",   // url of php page where you are writing the query
        success:function(response)
        { 
            $("#name").val(response);   // setting the result from page as value of name field

        }
        });

}
</script>

並編寫用於在頁面中獲取結果的代碼,例如page.php

$id=$_POST['id'];
$query=mysql_query("select name from table where id=$id");
$result=mysql_fetch_row($query);
echo $result[0];    // echo the name to js function
exit;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM