Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何ajaxify一个在后端执行SQL的PHP函数?_Php_Sql_Ajax_Function - Fatal编程技术网

如何ajaxify一个在后端执行SQL的PHP函数?

如何ajaxify一个在后端执行SQL的PHP函数?,php,sql,ajax,function,Php,Sql,Ajax,Function,我已经分解了我的问题,提供了一个没有开销的简明示例。 但足以给你一个想法 我有一个简单的index.php <?php include 'myClass.php'; ?> <html> <head></head> <body> <div> <?php myClass->printTable(); ?> </div> </body> </html>

我已经分解了我的问题,提供了一个没有开销的简明示例。 但足以给你一个想法

我有一个简单的index.php

<?php
    include 'myClass.php';
?>

<html>
<head></head>
<body>
    <div> <?php myClass->printTable(); ?> </div>
</body>
</html>

该函数返回一个完整的表,其中包含在后端准备的数据

<?php

function printTable()
{        
    // printing static table header
    echo '    <table class="table" style="zoom: 0.75;">
                <thead class="thead-dark">
                    <tr>
                        <th scope="col">Date</th>           // current date
                        <th scope="col">Order Number</th>   // an int
                        <th scope="col">Current Value</th>  // an int
                    </tr>
                </thead>
                <tbody>
        ';

    $result = mysqli_query($this->link, "SELECT * FROM `someData`");
    while ($row = mysqli_fetch_assoc($result))
    {   
        $orderNumber = $row['orderNumber'];
        $currentValue = $row['currentValue'];
        $date = $this->getDate($orderNumber);   // member function that returns a timestamp from the database

        //printing actual table
        echo '      <tr>
                        <td>'. $date .'</td>
                        <td>'. $orderNumber .'</td>
                        <td>'. $currentValue .'</td>
                    </tr>
            ';
    }

echo '       </tbody>
        </table>
    ';      
}

?>

高级:您需要一个PHP文件(“endpoint”,例如“localhost/data.PHP”),只返回
printTable
中的HTML代码。然后使用JavaScript(例如jQuery-
$.ajax
,您可以查看它的详细工作原理)每n秒获取一次此页面的内容并插入到您的页面中。

我正在寻找从后端获取一些数据并将其显示在页面上的一个div中的广泛或不特定的方法

解决方案是创建一个单独的PHP(fetch.PHP)文件,只回显我需要在div中显示的数据

从包含div的页面中,我将执行以下操作:

<div id="result"></div>

<script>

function load_data()
{
    $.ajax({
        url:"fetch.php",
        method:"POST",
        success:function(data)
        {
            $('#result').html(data);
        }
    });
}

load_data()

</script>


尝试一下你发现的其中一个想法可能会有所帮助;然后让我们知道你尝试了什么,以及你在哪里被卡住了。
setInterval(function(){
    load_data()
}, 1000);