使用AJAX和PHP中的POST使用POST更新多个Div
问题描述:
尝试使用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>
答
当然烨这是可能的,通过使用echo json_encode($row);
返回你的Ajax中JSON,而不是简单的字符串和检索对象的阿贾克斯成功返回,然后你将很容易能够单独检索它。
$.ajax({
dataType: "json",
data: {action:Player_Card},
type: 'post',
url: "DBv6.php",
success: function(msg) {
alert(msg.FirstName); //use your statement instead
alert(msg.ID); //use your statement instead
}
});
OMG谢谢你这么多!更改它$(“#FirstNameBox”)。html(msg.FirstName); –
你总是欢迎队友.. @MichaelSandusky :) – ameenulla0007