简体   繁体   English

刷新div内容而不关闭下拉菜单

[英]Refresh div contents without closing dropdown

I'm trying to learn php, mysql and javascript/jquery by building a live bidding system. 我正在尝试通过构建实时出价系统来学习php,mysql和javascript / jquery。 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. 我制作了一个bidbuttons.php,它在查询db以获取当前值后输出按钮,并使用在其他地方找到的jquery加载和重新加载页面。 I have tried several different methods of refreshing the div, and the best I came up with unfortunately closed the dropdown on the timer. 我尝试了几种不同的刷新div的方法,但是我想出的最好的方法是关闭了计时器的下拉列表。 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. 宁愿div刷新,而下拉菜单仅在bidbuttons.php更新后关闭。 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 和我的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);
?>

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. 我认为问题可能在于您的PHP文件正在生成HTML,由于它们是通过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 . 只是一个想法,但是您可以使PHP输出为JSON而不是包含数据库结果的JSON ,然后让jQuery读取输出 Your code would then process that JSON file and create or update the HTML elements accordingly (rather than replace them). 然后,您的代码将处理该JSON文件,并相应地创建或更新HTML元素(而不是替换它们)。

This way you have more fine grained control in the JavaScript for when your dropdown should close. 这样,您可以在JavaScript中获得更精细的控制,以确保下拉列表关闭的时间。

See this answer if you'd like to output from MySQLi to JSON. 如果您想从MySQLi输出到JSON, 请参见此答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM