【问题标题】:How to refresh a PHP Page without reload如何在不重新加载的情况下刷新 PHP 页面
【发布时间】:2013-08-20 20:56:29
【问题描述】:

我有一个名为 js.php 的 PHP 页面,我想刷新页面而不重新加载整个页面。我知道这个问题已经被问过太多次了,但我就是无法理解。我尝试了下面的代码,但它不起作用。我还想问,这段代码应该放在哪里。 js.php 或其他文件?有没有其他方法可以在不重新加载的情况下刷新页面? 它比较长,但最终我想刷新页面,特别是 2 个变量,profittext 和 sumtext 请忽略斜杠代码。

js.php

<?php 
//error_reporting(0);//on if need

//require "core/init.php";
//protectpage();
$bidpricepl='';
$offerpricepl='';
$sum='';
$profitText =0;
$sumText=0;
?>

<!DOCTYPE html>
<html>
<head>
<h2>Trade Page</h2>

<script type="text/JavaScript">
<!--
//function timedRefresh(timeoutPeriod) {
    //setTimeout("location.reload(true);",timeoutPeriod);
//}
//   -->
</script>
</head>
<body onload="JavaScript:timedRefresh(2000);">

<script>
function openWin()
{
myWindow=window.open('tradeform.php','pop','width=600,height=600');

myWindow.focus();
}
function openWin1()
{
myWindow=window.open('tradehistory.php','pop','width=1000,height=600');

myWindow.focus();
}
</script>
</head>
<body>
<table style="border:1px solid black;">
<tr>
<th style="border:1px solid black;">User Name</th><th style="border:1px solid black;">Balance</th>
</tr>
<tr>
<td style="border:1px solid black;">Eg.SEYAN</td><td style="border:1px solid black;">Eg. 50000 </td>
</tr>
</table>
<input style="display:inline" type="button" value="Create New Order" onclick="openWin()" />
<input style="display:inline" type="button" value="Trade History" onclick="openWin1()" />

<link rel="stylesheet" type="text/css" href="truefxhp.css" />
<iframe  src="http://webrates.truefx.com/rates/webWidget/trfxhp.jsp?c=EUR/USD,USD/JPY,USD/CAD,EUR/JPY,EUR/CHF,GBP/USD,AUD/USD,USD/CHF" width="400" height="400" seamless></iframe>
</body>
</html>
<table style="border:1px solid black;">
<tr>
<th style="border:1px solid black;">User Name</th><th style="border:1px solid black;">Balance</th><th style="border:1px solid black;">Equity</th>
</tr>
<tr>
<td style="border:1px solid black;">SEYAN</td><td style="border:1px solid black;">50000 </td><td style="border:1px solid black;">50000 </td>
</tr>
</table>
<?php 
echo "<br>";
require_once 'connect.php';
include 'start.php';
include 'functions.php';


$query = "SELECT * FROM opentrades"; //You don't need a ; like you do in SQL
$result = mysql_query($query);
echo "<table border = '1px'>"; // start a table tag in the HTML
echo "<tr><td>" . "Order Number" . "</td><td>" . "Selection" . "</td><td>" . "Date" . "</td><td>" . "Type" . "</td><td>" . "Size" . "</td><td>" . "Bid Price" . "</td><td>" . "Offer Price" . "</td><td>" ."Stop Loss" . "</td><td>" . "Take Profit" . "</td><td>" ."Profit/Loss(USD)"."</td><td>" ."Close"."</td></tr>" ;  //$row['index'] the index here is a field name

while($row = mysql_fetch_assoc($result)){   //Creates a loop to loop through results
if ($row['selection']=='eur/usd')// TO RETRIEVE BID AND OFFER FOR EACH ROW
            {
            $bidpricepl=$bid;

            $offerpricepl=$bid1;

            }
    elseif ($row['selection']=='usd/jpy')
            {
            $bidpricepl=$bid2;

            $offerpricepl=$bid3;

            }
    elseif ($row['selection']=='usd/cad')
            {
            $bidpricepl=$bid4;

            $offerpricepl=$bid5;

            }
    elseif ($row['selection']=='eur/jpy')
            {
            $bidpricepl=$bid6;

            $offerpricepl=$bid7;

            }
    elseif ($row['selection']=='eur/chf')
            {
            $bidpricepl=$bid8;

            $offerpricepl=$bid9;

            }
    elseif ($row['selection']=='gbp/usd')
            {
            $bidpricepl=$bid10;

            $offerpricepl=$bid11;

            }
    elseif ($row['selection']=='aud/usd')
            {
            $bidpricepl=$bid12;

            $offerpricepl=$bid13;

            }
    elseif ($row['selection']=='usd/chf')
            {
            $bidpricepl=$bid14;

            $offerpricepl=$bid15;

            }
    if ($row['type']=="buy")
        {
            //$last3charsoffer = substr($row['offerprice'], -6);
            //$offernodecimal = str_replace('.', '', $last3charsoffer);
            //$last3charsoffer1 = substr($offerpricepl, -6);
            //$offernodecimal1 = str_replace('.', '', $last3charsoffer1);

            //$pips2 = ltrim($pips2, '0');
            //$calcpips2=$calcpips/$minipipskiller;
            //$last3charsoffer = substr($row['offerprice'], -6);
            //$offernodecimal = str_replace('.', '', $last3charsoffer);
            //$last3charsoffer1 = substr($offerpricepl, -6);
            //$offernodecimal1 = str_replace('.', '', $last3charsoffer1);
            //$minipipskiller='10';
            //$offeropen=$row['offerprice'];// to define variable
            //$pips=$offerpricepl-$offeropen;// to calculate difference STEP 1
            //$calcpips = str_replace('.', '', $pips); //removing the deci
            //$calcpips = ltrim($calcpips, '0');// remove zeros in front
            //$calcpips2=$calcpips/$minipipskiller;// to divide by 10 to cut mini pips  
            $minipipskiller='10';
            $offeropen=$row['offerprice'];
            $pips=$offerpricepl-$offeropen;
            $closedb=$offeropen;
            $pips1=round($pips, 6);
            $pips2 = str_replace('.', '', $pips1);
                if ($pips2<0)
            {
                $pips2 = str_replace('-', '', $pips2);
                $pips2 = ltrim($pips2, '0');
                $pips2 = -1 * abs($pips2);
            }
            else {
                $pips2 = ltrim($pips2, '0');
            }
            $pips3=$pips2/$minipipskiller;
        }// PIP COUNTING
    elseif ($row['type']=="sell")//FOR PIP COUNTING
        {
            //$last3charsbid = substr($row['bidprice'], -6);
            //$bidnodecimal = str_replace('.', '', $last3charsbid);
            //$last3charsbid1 = substr($bidpricepl, -6);
            //$bidnodecimal1 = str_replace('.', '', $last3charsbid1);
            $minipipskiller='10';
            $bidopen=$row['bidprice'];
            $pips=$bidopen-$bidpricepl;
            $closedb=$bidopen;
            $pips1=round($pips, 6);
            $pips2 = str_replace('.', '', $pips1);
            if ($pips2<0)
            {
                $pips2 = str_replace('-', '', $pips2);
                $pips2 = ltrim($pips2, '0');
                $pips2 = -1 * abs($pips2);
            }
            else {
                $pips2 = ltrim($pips2, '0');
            }
            $pips3=$pips2/$minipipskiller;
        }
        //echo $pips3;


            $ticksize= "0.0001";// FOR PROFIT AND LOSS
            $lot1 = "100000";
            $sizecalc=$row['size'] * $lot1;

                if ($row['type']=="buy")
            {   
                $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
            }
            if ($row['type']=="sell")
            {
                $profitandloss=$sizecalc*$ticksize*$pips3; //per TRADE
            }
            //echo $lot1;
            //echo $ticksize;

            $zero= '0';
            //if($profitandloss<$zero){
            //      echo "<div style=\"color: red;\">$profitandloss</div>";
            //}
            //elseif ($profitandloss>$zero){
                //  echo "<div style=\"color: green;\">$profitandloss</div>";
            //}
            if($profitandloss<$zero) {
                  $profitText = "<div style=\"color: red;\">$profitandloss</div>";
                } elseif ($profitandloss>$zero) {
                    $profitText = "<div style=\"color: green;\">$profitandloss</div>";
                }
                // for profit and loss counting

$sum+= $profitandloss;
             //

        echo "<tr><td>" . $row['trade_id'] .         
        "</td><td>" . $row['selection'] . 
        "</td><td>" . $row['date'] .
        "</td><td>" . $row['type'] .
        "</td><td>" . $row['size'] .
        "</td><td>" . $row['bidprice'] .
        "</td><td>" . $row['offerprice'] .
        "</td><td>" . $row['stoploss'] .
        "</td><td>" . $row['takeprofit'] .
        "</td><td>" . $profitText . 
        "</td><td><a href ='delete.php?id=".
        $row['trade_id']."'>X</a>
         </td></tr>";  
$profitandloss=0;

if($sum<$zero) {
      $sumText = "<div style=\"color: red;\">$sum</div>";
} elseif ($sum>$zero) {
    $sumText = "<div style=\"color: green;\">$sum</div>";
}
}

echo "</table><br>";



//$result_array = $codes->result_array();
//$results = array();
//$today = time();

//foreach($codes->result_array() as $row)
//{
//    if(strtotime($row['exp_date']) <= $today)
//    {//-- Keep this
 //      $results[] = $row;
    //var allLinks = document.links;

// Bind the event handler to each link individually
//for (var i = 0, n = allLinks.length; i < n; i++) {
    //allLinks[i].addEventListener('click', function (event) {});
   // allLinks[i].onclick = function () {
        // Do something            

?>

JavaScript

<html>
    <head>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">

            $(document).ready(function() {
             var reloadData = 0; // store timer

                load data on page load, which sets timeout to reload again
               loadData();
            });

            function loadData() {
                $('#load_me').load('js.php', function() {
                   if (reloadData != 0)
                       window.clearTimeout(reloadData);
                   reloadData = window.setTimeout(loadData, 1000)
               }).fadeIn("slow"); 
            }
        </script>
    </head>
    <body>
        <div id="load_me"></div>
    </body>
</html>

【问题讨论】:

  • Ajax 是您正在寻找的。在您的 javascript 中使用 ajax 调用
  • AJAX。如果您发现浏览器实现令人生畏,您可以使用 jquery 来实现 ajax
  • 通过使用 Ajax,您可以重新加载页面的某些部分。
  • ppls,OP 已经在使用 jQuery 和 AJAX。
  • 我如何在我的代码中实现它,比如我在哪里输入什么和东西。我不知道该怎么做..

标签: php javascript html refresh


【解决方案1】:
【解决方案2】:

你需要使用 ajax 来为这个 Check here 使用 jquery 加载函数

【讨论】:

    【解决方案3】:

    这是因为您的 ajax 请求转到同一页面。 您可以将 ajax 发送到其他 php(test.php) 文件 在 test.php 中只生成你想在你的 div 中显示的内容

    <div id="load_me">
    $data
    </div>
    

    只是回显 $data

    然后退出。

    更改 $('#load_me').load('test.php', function().....

    【讨论】:

      【解决方案4】:
      <html>
          <head>
              <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
              <script>
                  $(document).ready(function() {
                      function loadData() {
                          $('#load_me').load('js.php', function() {
                             if (window.reloadData != 0)
                                 window.clearTimeout(window.reloadData);
                             window.reloadData = window.setTimeout(loadData, 1000)
                         }).fadeIn("slow"); 
                      }
                   window.reloadData = 0; // store timer load data on page load, which sets timeout to reload again
                     loadData();
                  });
              </script>
          </head>
          <body>
              <div id="load_me"></div>
          </body>
      </html>
      

      【讨论】:

      • 这段代码一遍又一遍地复制我的代码
      • 你所说的复制是什么意思,你能纠正我吗:你需要load_me div 内容以间隔刷新......对吗?
      • 是的,这是正确的。最终我希望 js.php 动态刷新。
      • 这就是我的代码所做的!!你试过了吗?此代码将不断替换 &lt;div&gt; 内容直到 php.js 响应。请尝试代码然后给我反馈
      • 是同一个页面js.php吗?我的意思是你在哪里有 div load_me 是否在同一页面 js.php 中,你从中获取 div 内容?
      【解决方案5】:

      您的页面中有一个错误会阻止脚本运行

      <html>
          <head>
              <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js">
      
                  $(document).ready(function() {
                   var reloadData = 0; // store timer
      
                      // ----- load data on page load, which sets timeout to reload again
                     loadData();
                  });
      
                  function loadData() {
                      $('#load_me').load('js.php', function() {
                         if (reloadData != 0)
                             window.clearTimeout(reloadData);
                         reloadData = window.setTimeout(loadData, 1000)
                     }).fadeIn("slow"); 
                  }
              </script>
          </head>
          <body>
              <div id="load_me"></div>
          </body>
      </html>
      

      查看标记的行 // -----

      【讨论】:

      • 应该刷新的数据在html表中。它仍然无法正常工作!
      • js.php 是一个单独的页面。发布 js.php 代码(如果不是太长)并删除任何敏感数据,即。密码等
      • AJAX 背后的想法是你的 js.php 收集一条信息并将其返回给调用页面。 js.php 页面应该只包含 php 代码。从您的代码的外观来看,您应该将 js.php 放在 iframe 中并忘记 ajax 调用。
      • 如果是这样,我还能在不刷新的情况下重新加载 iframe 吗?
      【解决方案6】:

      你可以把这段代码保存在Js.php中

      如果您没有得到想要的结果,t## 您可以尝试使用 filrebug 和 chjeck 使用 ctr+shift+j 来检查任何 javascript 错误

      否则使用 iframe。

      【讨论】:

      • 我如何使用 iframe 来做到这一点?
      猜你喜欢
      • 1970-01-01
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-12
      • 2019-03-02
      • 2020-10-09
      相关资源
      最近更新 更多