如何从PHP中的sql server数据库中获取数据而不刷新页面 [英] how to fetch data from sql server database in php without refreshing the page

查看:80
本文介绍了如何从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.因为如果是一个,那么一些东西必须改变。

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 termination-自行调度(使用 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/

这篇关于如何从PHP中的sql server数据库中获取数据而不刷新页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆