【发布时间】:2016-03-01 17:26:59
【问题描述】:
尝试使用 AJAX PHP 和 HTML 更新多个 Div。下面是一个附加的例子。我可以更新一个 DIV,但是我想从 PHP SELECT 语句返回多个结果,并在每个 DIV 中返回每个列 基于列名选择,如:
$("#IDBox").html(msg.ID);
$("#FirstNameBox").html(msg.FirstName);
这可能吗?该设计将是用户输入 ID 和执行 PHP 查询的帖子,该查询返回每个 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>
【问题讨论】:
标签: php jquery html sql-server ajax