刷新页面如果数据库发生变化。

huangapple go评论100阅读模式
英文:

Refresh page if there is change in database

问题

我正在尝试使用Ajax和PHP,如果数据库中的orderStatus发生更改,就刷新页面。我将当前的orderStatus设置为预定义数据,然后使用Ajax从数据库获取当前的orderStatus,最后比较它们是否不同。如果它们不同,我想刷新页面。

PHP(autorefresh.php)

<?php

$orderId = $_POST["orderId"];

$query = "SELECT * FROM orderinhomeonlinecall WHERE orderId='$orderId'";
$result = mysqli_query($db, $query);
 
while($row = mysqli_fetch_array($result))
{
    $orderStatus = $row['orderStatus'];

    $data = array(
        'orderStatus' => $orderStatus
    );
    echo json_encode($data);
}
?>

Javascript

<script type="text/javascript">
var predefined_val = '<?php echo $orderStatus; ?>';// your predefined value.
$(document).ready(function(){
    setInterval(function(){
        $.ajax({
            type: "POST",
            url: "autorefresh.php", //put relative url here, script which will return php
            data: {orderId: <?php echo $orderId; ?>}, // if any you would like to post any data
            success:function(response){
                var data = response; // response data from your php script
                if(predefined_val !== data){
                    window.location.href=window.location.href;
                }
            }
        });                     
    },5000);// function will run every 5 seconds
});
</script>

请注意,这是您提供的代码的翻译部分。如果您有任何问题或需要进一步的解释,请随时提问。

英文:

I am trying to refresh my a page if there is a change in orderStatus from database using Ajax and PHP. I set the current orderStatus as predefined data and then use Ajax to get the current orderStatus from database and finally compare if they are not the same. I want to refresh the page if they are not the same.

PHP (autorefresh.php)

&lt;?php

$orderId = $_POST[&quot;orderId&quot;];

$query = &quot;SELECT * FROM orderinhomeonlinecall WHERE orderId=&#39;$orderId&#39;&quot;;
$result = mysqli_query($db, $query);
 
  while($row = mysqli_fetch_array($result))
  {
      $orderStatus = $row[&#39;orderStatus&#39;];

      $data = array(
        &#39;orderStatus&#39;   =&gt; $orderStatus
       );
       echo json_encode($data);
}
?&gt;

Javascript

&lt;script type=&quot;text/javascript&quot; &gt;
var predefined_val = &#39;&lt;?php echo $orderStatus; ?&gt;&#39;;// your predefined value.
$.document(ready(function(){
    setInterval(function(){
        $.ajax({
            type:&quot;POST&quot;,
            url:&quot;autorefresh.php&quot;, //put relative url here, script which will return php
            data:{orderId: &lt;?php echo $orderId; ?&gt;}, // if any you would like to post any data
            success:function(response){
                var data = response; // response data from your php script
                if(predefined_val !== data){
                    window.location.href=window.location.href;
                }
            }
        });                     
    },5000);// function will run every 5 seconds
}));

</script>

答案1

得分: 0

以下是翻译好的代码部分:

<script type="text/javascript">
var predefined_val = '<?php echo $orderStatus; ?>';// 您的预定义值。
$(document).ready(function () {
    setInterval(function () {
        $.ajax({
            type: "POST",
            url: "autorefresh.php", // 放置相对路径的URL,将返回PHP的脚本
            data: {orderId: '<?php echo $orderId; ?>'}, // 如果您想要发布任何数据
            dataType: "json",
            success: function (response) {
                var data = response; // 来自您的PHP脚本的响应数据
                if (predefined_val !== data.orderStatus) {
                    window.location.href = window.location.href;
                }
            }
        });
    }, 5000);// 函数将每5秒运行一次
});
</script>
英文:

The below code should work, Need to mention dataType:&quot;json&quot; else use JSON.stringify(data) to parse response

&lt;script type=&quot;text/javascript&quot;&gt;
var predefined_val = &#39;&lt;?php echo $orderStatus; ?&gt;&#39;;// your predefined value.
$(document).ready(function () {
    setInterval(function () {
        $.ajax({
            type: &quot;POST&quot;,
            url: &quot;autorefresh.php&quot;, //put relative url here, script which will return php
            data: {orderId: &lt;?php echo $orderId; ?&gt;}, // if any you would like to post any data
            dataType: &quot;json&quot;,
            success: function (response) {
                var data = response; // response data from your php script
                if (predefined_val !== data.orderStatus) {
                    window.location.href = window.location.href;
                }
            }
        });
    }, 5000);// function will run every 5 seconds
});
&lt;/script&gt;

答案2

得分: -2

我已经通过创建两个文件(autorefresh.php、index.php)并测试带有表的数据库进行了测试,对我有效。我认为下面的代码可能会有帮助,如果不行,请分享你的代码,我会检查并修复它。

autorefresh.php

<?php

// 创建连接
$db = new mysqli("localhost", "root", "","test");

$orderId = $_POST["orderId"];

$query = "SELECT * FROM orderinhomeonlinecall WHERE orderId='$orderId'";

$result = mysqli_query($db, $query);

while($row = mysqli_fetch_array($result))
{
    $orderStatus = $row['orderStatus'];

    $data = array(
        'orderStatus'   => $orderStatus
    );
    echo json_encode($data);
}
?>

index.php

<?php
$orderStatus ='pending';
$orderId = 1;
?>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript">
var predefined_val = '<?php echo $orderStatus; ?>';// your predefined value.
$(document).ready(function () {
    setInterval(function () {
        $.ajax({
            type: "POST",
            url: "autorefresh.php", //put relative url here, script which will return php
            data: {orderId: <?php echo $orderId; ?>}, // if any you would like to post any data
            dataType: "json",
            success: function (response) {
                var data = response; // response data from your php script
                if (predefined_val !== data.orderStatus) {
                    window.location.href = window.location.href;
                }
            }
        });
    }, 5000);// function will run every 5 seconds
});
</script>
英文:

I have tested this by creating two files(autorefresh.php,index.php) and test db with table and it is working for me. I think the below code would be helpful, If not please share you code, i will check and fix it.

autorefresh.php
<?php

// Create connection
$db = new mysqli(&quot;localhost&quot;, &quot;root&quot;, &quot;&quot;,&quot;test&quot;);

$orderId = $_POST[&quot;orderId&quot;];

$query = &quot;SELECT * FROM orderinhomeonlinecall WHERE orderId=&#39;$orderId&#39;&quot;;

$result = mysqli_query($db, $query);

  while($row = mysqli_fetch_array($result))
  {
      $orderStatus = $row[&#39;orderStatus&#39;];

      $data = array(
        &#39;orderStatus&#39;   =&gt; $orderStatus
       );
       echo json_encode($data);
}
?&gt;

index.php

&lt;?php
$orderStatus =&#39;pending&#39;;
$orderId =1;
?&gt;
&lt;script src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
var predefined_val = &#39;&lt;?php echo $orderStatus; ?&gt;&#39;;// your predefined value.
$(document).ready(function () {
    setInterval(function () {
        $.ajax({
            type: &quot;POST&quot;,
            url: &quot;autorefresh.php&quot;, //put relative url here, script which will return php
            data: {orderId: &lt;?php echo $orderId; ?&gt;}, // if any you would like to post any data
            dataType: &quot;json&quot;,
            success: function (response) {
                var data = response; // response data from your php script
                if (predefined_val !== data.orderStatus) {
                    window.location.href = window.location.href;
                }
            }
        });
    }, 5000);// function will run every 5 seconds
});
&lt;/script&gt;

huangapple
  • 本文由 发表于 2020年1月7日 02:36:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/59617231.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定