如何在不刷新页面的情况下在php中从sql server数据库中获取数据
我正在尝试从数据库中获取一些数据.我创建了一个位于 functions.php
文件中的函数,该函数返回一个值.在另一个页面上,我创建了一个变量并获取该值.我试图使用 onkey
来检查数据库,但后来我意识到即使他们不输入任何内容,我也需要知道票的数量.
I am trying to get some data from the database. I create a function that is located in functions.php
file that return a value. On another page, I create a variable and just get that value. I was trying to use the onkey
to check the database but then I realize that i need to know the amount of tickets even if they don't type anything.
这是函数:
function.php
function is_ticket_able($conn){
$query = "select number_of_tickets from [dbo].[TICKETS] " ;
$stmt = sqlsrv_query($conn, $query);
while ($row = sqlsrv_fetch_array($stmt)) {
$amount_of_tickets = $row['number_of_tickets'];
}
return $amount_of_tickets;
}
而且,我正在尝试检查数据库(不刷新页面)并获取此页面上的值:
And, I am trying to check the database (without refreshing the page) and get the value on this page:
application.php
$amount_of_tickets = is_ticket_able($conn);
然后,我只是检查 $amount_of_tickets
不是 0 或 1.因为如果是 1,那么某些东西必须改变.
Then, I just check that $amount_of_tickets
is not 0 or 1. Because if is one then some stuff have to change.
我正在这样做(在 application.php 中):
I am doing this (inside application.php):
if($amount_of_tickets !=0){
//show the form and let them apply for tickets.
//also
if($amount_of_tickets == 1){
//just let them apply for one ticket.
}
}
我看到 AJAX 是适合使用的,但我很困惑使用它.
I saw that AJAX would be the right one to use, but I am so confuse using it.
更新:
function.php
function is_ticket_able($conn){
$query = "select number_of_tickets from [dbo].[TICKETS_LKUP] " ;
$stmt = sqlsrv_query($conn, $query);
while ($row = sqlsrv_fetch_array($stmt)) {
$ticket = $row['number_of_tickets'];
}
return $ticket;
}
application.php
$amount_of_tickets = is_ticket_able($conn);
<script type="text/javascript">
var global_isTicketAble = 0;
checkTicket();
function checkTicket()
{
$.ajax(
{
url: "application.php",
method: 'GET',
dataType: 'text',
async: true,
success: function( text )
{
global_isTicketAble = text;
alert(global_isTicketAble);
if( global_isTicketAble == 0 ){
window.location.replace("http://www.google.com");
}
setTimeout( checkTicket, 5000 ); // check every 5 sec
}
});
}
</script>
所以,现在的问题是,当我 alert(global_isTicketAble);
它不会提醒数据库中的值,但它会提醒 application.php 中的所有内容强>...帮助 plzzz
So, now the problem is that when I alert(global_isTicketAble);
it doesn't alert the value from the database but it does alert everything that is inside application.php...Help plzzz
推荐答案
服务器端
假设您需要定期检查 $amount_of_tickets
并且可以将其计算到 application.php 中,在该文件中您将拥有
Assuming you need to check $amount_of_tickets
periodically and this can be computed into application.php, inside that file you'll have
<?php
// $conn is defined and set somewhere
$amount_of_tickets = is_ticket_able($conn);
echo $amount_of_tickets;
exit(0);
?>
这样,当使用简单的 GET 请求调用脚本时,响应中的值将作为简单文本返回.
This way when the script is invoked with a simple GET request the value is returned in the response as simple text.
客户端
ajax 如果您想在不重新加载页面的情况下更新页面上的信息,则是一种可行的方法.
ajax is the way to go if you want to update information on page without reloading it.
下面只是一个简单的例子(使用 jQuery),可以扩展以满足您的需要.
Below is just a simple example (using jQuery) that may be extended to fit your needs.
下面的代码是一段 JavaScript 代码.使用全局变量来存储值(应避免使用全局变量,但这仅用于示例目的)
The code below is a JavaScript snippet. A global is used to store the value (globals should be avoided but it's just for the purpose of the example)
然后调用一个函数并从 function.php 脚本中获取更新的值.
Then a function is invoked and the updated value is fetched from function.php script.
函数 -prior terminate- 安排自身(使用 setTimeout
)在给定的毫秒数后重新调用(重复取值过程).
The function -prior termination- schedules itself (with setTimeout
) to be re-invoked after a given amount of milliseconds (to repeat the fetch value process).
var global_isTicketAble = 0;
checkTicket();
function checkTicket()
{
$.ajax(
{
url: "application.php",
method: 'GET',
dataType: 'text',
async: true,
success: function( text )
{
global_isTicketAble = text;
// eventually do something here
// with the value just fetched
// (ex. update the data displayed)
setTimeout( checkTicket, 5000 ); // check every 5 sec
}
}
}
注意 $.ajax()
发送请求但不等待响应(因为 async
设置为 true
).当收到请求时,执行指定为 success
的函数.
Note that $.ajax()
sends the request but does not wait for the response (as async
is set to true
). When the request is received the function specified as success
is executed.
完整的 jQuery ajax 函数文档可以在这里找到
Complete jQuery ajax function documentation can be found here
http://api.jquery.com/jquery.ajax/
相关文章