【问题标题】:Using js load in same php file在同一个php文件中使用js加载
【发布时间】:2015-11-04 07:10:27
【问题描述】:
<script>
 setInterval(function(){
         $.ajax({
               url :'user_online.php',

               success: function()
                   {
                       $('.chat-o').load('user_online.php');
                   }
            });
     },1000); 
</script>

假设我有这个脚本。如果 user_online 是我放入的文件,我如何使它工作(意思是,我怎样才能使这个脚本在内部工作——在一个与 url 相同的 php 文件中)

基本上我想将与上面相同的内容应用于另一个页面,使其自动显示结果而无需重新加载:

<?php 
   $gameid = fetchinfo("value", "info", "name", "current_game");
$query  = mysql_query("SELECT * FROM `games` WHERE `id` < $gameid ORDER BY `id` DESC LIMIT 10");
while($rowd=mysql_fetch_array($query)):
  //define stuff
  $lastwinner=$rowd["userid"];
  $winnercos =$rowd['cost'];
  $winnerpercent = $rowd['percent'];
  $winneravatar=fetchinfo("avatar", "users", "steamid", $lastwinner);
  $winnername = fetchinfo("name", "users", "steamid", $lastwinner);
  $steamlink = fetchinfo("steamprofile", "users", "steamid", $lastwinner); ?>
  <div class="cont row">
  <div class="col-xs-24 header">
  </div>
  <div class="col-xs-24 body">
  <div class="col-xs-16 col-sm-16">
  <a rel="nofollow" target="_blank" href=<?php echo $steamlink; ?> class="img hidden-xs">
  <img src=<?php echo $winneravatar; ?> > </a>
  <a rel="nofollow" target="_blank" href=<?php echo $steamlink; ?> class="name"> <?php echo $winnername; ?> </a>
  </div>
  <div class="right text-right">
  <span class="win">
  Win: <span>$ <?php echo round($winnercos, 3); ?> </span></span>
  <span class="chance">
  Chance: <span><?php echo round($winnerpercent, 2); ?>% </span> </span> </div> </div>

用我想要的更新它。谁能告诉我好不好?这是 index.php 的一部分

 <div class="site history col-xs-24 col-sm-18 col-md-14 col-lg-12"> <h2>History</h2> <div id="history">
  <script>
 setInterval(function(){
         $.ajax({
               url :'index.php',

               success: function()
                   {
                       $('.history').html(data);
                   }
            });
     },1000); 
</script> 
   <?php 
   $gameid = fetchinfo("value", "info", "name", "current_game");
$query  = mysql_query("SELECT * FROM `games` WHERE `id` < $gameid ORDER BY `id` DESC LIMIT 10");
while($rowd=mysql_fetch_array($query)):
  //define stuff
  $lastwinner=$rowd["userid"];
  $winnercos =$rowd['cost'];
  $winnerpercent = $rowd['percent'];
  $winneravatar=fetchinfo("avatar", "users", "steamid", $lastwinner);
  $winnername = fetchinfo("name", "users", "steamid", $lastwinner);
  $steamlink = fetchinfo("steamprofile", "users", "steamid", $lastwinner); ?>
  <div class="cont row">
  <div class="col-xs-24 header">
  </div>
  <div class="col-xs-24 body">
  <div class="col-xs-16 col-sm-16">
  <a rel="nofollow" target="_blank" href=<?php echo $steamlink; ?> class="img hidden-xs">
  <img src=<?php echo $winneravatar; ?> > </a>
  <a rel="nofollow" target="_blank" href=<?php echo $steamlink; ?> class="name"> <?php echo $winnername; ?> </a>
  </div>
  <div class="right text-right">
  <span class="win">
  Win: <span>$ <?php echo round($winnercos, 3); ?> </span></span>
  <span class="chance">
  Chance: <span><?php echo round($winnerpercent, 2); ?>% </span> </span> </div> </div>

【问题讨论】:

    标签: javascript php integrate


    【解决方案1】:

    你已经在user_online.php中使用了ajax做了一些事情,所以需要在.load()中再次加载数据。所以

    改变这个,

    success: function(data)
         {
             $('.chat-o').html(data);
         }
    

    如果您在user_online.php 中仅使用php,请确保您echo 的值。

    【讨论】:

    • 什么错误?第一个代码部分只是我希望它看起来像但从同一文件中自加载的一个示例。问题是我想将其应用于第二个代码部分
    • 错误是什么?我没有任何错误。好的,我想要的是使第二个代码自动更新而无需重新加载页面。忽略第一个代码框。
    • 你尝试过的都是正确的。只需将 success() 替换为我提供的内容即可。
    • 如果我当前的
      也用不同的 id 替换 chat-o 对吗? php 文件也称为 index.php
    • 你现在改的id,应该是你想显示数据的地方,不用reload。
    猜你喜欢
    相关资源
    最近更新 更多
    热门标签