0
嘗試使用AJAX PHP和HTML更新多個Div。下面是一個附加的例子。我可以得到一個DIV更新,但是我想從一個PHP SELECT語句返回多個結果,並基於對列名的選擇就像每個返回的每個DIV 列:使用AJAX和PHP中的POST使用POST更新多個Div
$("#IDBox").html(msg.ID);
$("#FirstNameBox").html(msg.FirstName);
這可能嗎?該設計將是用戶輸入ID和執行PHP Query的帖子,該查詢返回每個DIV的值,即ID,名字,姓氏等。 謝謝!
<?php
$databaseName = "SQL_SERVER" ;
$serverName = "192.168.89.89";
$uid = "Rep02";
$pwd = "Password123";
$connectionInfo = array("UID"=>$uid,
"PWD"=>$pwd ,
"Database"=>$databaseName);
/* Connect using SQL Server Authentication. */
$conn = sqlsrv_connect($serverName , $connectionInfo);
$action = $_POST['action'];
$tsql = "SELECT [ID], [FirstName]
FROM TABLE1
WHERE [Player_ID] = '$action'";
/* Execute the query. */
$stmt = sqlsrv_query($conn, $tsql);
//Working Query
/* Iterate through the result set printing a row of data upon each iteration. BR=."\n" */
while($row = sqlsrv_fetch_ARRAY($stmt, SQLSRV_FETCH_ASSOC)){
{
echo $row['ID'];
echo $row['FirstName'];
}}
/* Free statement and connection resources. */
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
?>
:
<!doctype html>
<html>
<head>
<meta charset="utf-8" />
<title>Winner</title>
<style type="text/css">
#IDBox, #FirstNameBox {
width: 100px;
height: 18px;
border: 1px solid #999;
}
</style>
<script src="../MS/Jquery/jquery-1.11.0.min.js"></script>
<script>
function myCall4() {
var Player_Card = $('#Player_Card').val();
$.ajax({ url: 'DBv6.php',
data: {action:Player_Card},
dataType: 'html',
type: 'post',
success: function(msg) {
$("#IDBox").html(msg);
$("#FirstNameBox").html(msg);
}
})
};
</script>
</head>
<body>
ID:
<div id="IDBox"> </div>
First Name:
<div id="FirstNameBox"> </div>
<br>
<form>
<tr>
<td style="width: 100px;">Player Card:</td>
<td style="width: 150px;"><input type= "text" ID= "Player_Card" placeholder= "Please Swipe Card..." ></td>
</tr>
<td><input type = "button" value="Submit" onclick = "myCall4();">
</form>
</body>
</html>
OMG謝謝你這麼多!更改它$(「#FirstNameBox」)。html(msg.FirstName); –
你總是歡迎隊友.. @MichaelSandusky :) – ameenulla0007