dongzeao5047 2015-10-28 02:31
浏览 50

刷新div内容而不关闭下拉列表

I'm trying to learn php, mysql and javascript/jquery by building a live bidding system. I want to refresh the value of a bid button and dropdown automatically, but only if the value has changed. I've made a bidbuttons.php that outputs the buttons after querying the db for the current values, and I load and reload the page with this jquery I found elsewhere. I have tried several different methods of refreshing the div, and the best I came up with unfortunately closed the dropdown on the timer. This current version was described as exactly what I need but doesn't seem to work, in fact it logs me out. would prefer the div to refresh and the dropdown to close ONLY if the bidbuttons.php is updated. This is my first post, I hope I did this right.

<div id="bidbuttons"></div>

<script type="text/javascript">    
    var loadUrl = "includes/bidbuttons.php?id='.$item->id.'";  
    $("#bidbuttons").load(loadUrl).fadeIn("slow");
    var auto_refresh = setInterval(function () {
      $.get(loadUrl, function(data) {
        if (data.changed) {
          $("#bidbuttons").load(loadUrl).fadeIn("slow");
        }
      });
    }, 10000);
</script>

and my bidbuttons.php

    <?php
    include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start();

if (login_check($mysqli) == true) { 
$getid = (int)$_GET['id'];

// get current price
    $result = $mysqli->query("SELECT ammount, bidder_id FROM bids WHERE ammount=(SELECT MAX(ammount)) && item_id like $getid order by ammount desc");
    while($row = $result->fetch_assoc()) {
        $bidder_id = $row["bidder_id"];
        $current = $row['ammount'];
        echo ($mysqli->error);
        $result->free();
    }
//get item info
    $results = $mysqli->query("SELECT * FROM items WHERE id like $getid ");
        while($row = $results->fetch_assoc()) {
            if (empty ($current)) { $current = $row["start"];}

            $title =  $row["title"];
            $item->thenext = ($current + $row["raise"]);
            $buyout = $row["buyout"];
            $raise =  $row["raise"];
            $sold = $row["sold"];

        }
        $results->free();


echo ('<div id="buttons">
    <a class="btn btn-primary btn-lg" style="margin-bottom: 10px; margin-top: 10px;  width: 80%;" href="includes\placebid.php?itemid=' . $getid . '&ammount=' . $item->thenext .'">Bid $' . $item->thenext . '</a>
    ');
if ($buyout){echo ('
<a class="btn btn-success btn-lg" style="margin-bottom: 10px; width: 80%;"  href="includes\placebid.php?itemid='.$getid.'&ammount='.$buyout.'">Buyout $'.$buyout.'</a>

');}
echo '</div><!-- buttons -->';
echo ('
    <div class="dropdown">
        <button style="margin-bottom: 10px; width: 80%;" type="button" class="btn btn-info btn-lg dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">Custom Bid<span class="caret"></span></button>
<ul class="dropdown-menu">

');
// build the custom bid dropdown
    if (!$buyout) { $maxcust = $item->thenext*25;
        for ($cust = $item->thenext; $cust <= $maxcust; $cust = $cust + $raise) {
            echo ('<li><a href="includes\placebid.php?itemid=' . $id . '&ammount=' . $cust .'">' .$cust. '</a></li>');
        }
    }
    if ($buyout) {
        for ($cust = $item->thenext; $cust <= $buyout; $cust = $cust + $raise) {
        echo ('<li><a href="includes\placebid.php?itemid=' . $id . '&ammount=' .         $cust .'">' .$cust. '</a></li>');
        }   
}

echo ('
    </ul> 
    </div><!-- dropdown -->
'); 

}
mysqli_close($mysqli);
?>
  • 写回答

1条回答 默认 最新

  • douhao2856 2015-10-28 04:53
    关注

    I think the problem may be in that your PHP file is generating HTML which is continually replacing elements on your page due to them being loaded via AJAX.

    Just an idea but you could make your PHP output a JSON instead that contains the database results, then have jQuery read the output. Your code would then process that JSON file and create or update the HTML elements accordingly (rather than replace them).

    This way you have more fine grained control in the JavaScript for when your dropdown should close.

    See this answer if you'd like to output from MySQLi to JSON.

    评论

报告相同问题?

悬赏问题

  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行