【问题标题】:Why does reload specific section not work?为什么重新加载特定部分不起作用?
【发布时间】:2021-05-09 13:57:04
【问题描述】:

我尝试了不同的代码,但代码总是不起作用。我想每 5 秒只重新加载特定部分,而不是重新加载整个页面。重要的是不要刷新整个页面,因为它包含聊天功能。如果我要重新加载整个页面,人们就无法输入文本而不每 5 秒丢失一次。就像我说的,我尝试了一些来自 stackOverflow 的代码,但不知何故它们对我不起作用。

这是我的代码

<script type="text/javascript">
                    $(document).ready(function(){
                        setInterval(function(){
                            $("#context").load(window.location.href + " #context" );
                        }, 5000);
                        
                     });
                          </script>
<--variables explained --->
$fid = $_GET['fid']
$username = $_SESSION['username']
$db :
$servername = "localhost";
$dbname = "psychopath";
$username = "root";
$password = "";

try{
    $dsn = "mysql:host=" . $servername . ";dbname=" . $dbname;
    $db = new PDO($dsn, $username, $password);
    echo "";
}catch(PDOException $e){
    echo $e.getMessage();
}
<--- variables END ----->



$display_messages = $db->prepare("SELECT * FROM mychat_from WHERE (user_to_name = ? OR userfrom_name = ?) AND (user_to_id = ? OR userfrom_id = ?)");
                $display_messages->execute([$username , $username, $fid, $fid]);
                while($row=$display_messages->fetch(PDO::FETCH_ASSOC)){
                    $userfrom = $row['userfrom'];
                        if($userfrom != $username){
                            $messagefrom2 = $row['message_from'];
                            $messagefrombreak2 = wordwrap($messagefrom2, 27, "\n", true);
                            ?>
                            
                                <?php
                      ?><section id="context" name="context"><?php echo '<b>&nbsp;'.$userfrom.'</b>said:<br>';
                    echo "<section style='background: black; float: left; margin-left: 5px; border-radius: 7px; color: white;'>&nbsp;<font style='float: left;'> &nbsp;".$messagefrombreak2."</font></section>";?>
                     </section>
                    <?php echo "<br><br>";
                    ?>

这是完整的代码:

<!-- end snippet -->if(isset($_GET['fid'])){
    $fid = $_GET['fid'];

      ?>
    <section>
        <section>
            <?php 
            $get_friend = $db->prepare("do stuff");
            $get_friend->execute([$fid]);
            while($row=$get_friend->fetch(PDO::ROW STUFF)){
            
            }
            ?>
        </section>
        <section>
            <?php
            $username = $_SESSION['username'];
            $get_id = $db->prepare("do stuff");
            $get_id->execute([$fid]);

            while($row=$get_id->fetch(ROW STUFF)){
                
            $get_your_id = $db->prepare("do stugg");
               $get_your_id->execute([$username]);
               while($row=$get_your_id->fetch(ROW STUFF)){

            $display_messages = $db->prepare("DO STUFF");
            $display_messages->execute([$your_id, $your_id, $fid, $fid]);
            while($row=$display_messages->fetch(DO STUFF)){
                $userfrom = $row['userfrom'];
                    if($userfrom != $username){
                        $messagefrom2 = $row['message_from'];
                        $messagefrombreak2 = wordwrap($messagefrom2, 27, "\n", true);
                        ?>
                        
                            <?php
                  ?><div id="container" name="container"><div id="context" name="context"><?php echo '<b>&nbsp;'.$userfrom.'</b>said:<br>';
                echo "<section style='background: black; float: left; margin-left: 5px; border-radius: 7px; color: white;'>&nbsp;<font style='float: left;'> &nbsp;".$messagefrombreak2."</font></section>";?>
                 </div></div>
                <?php echo "<br><br>";
                ?>
                
            <script type="text/javascript">
                   $(document).ready(function(){
                    setInterval(function(){
                        $("#context").load(window.location.href + " #container" );
                    }, 5000);
                   });
                      </script>      
                    <?php
                
            }else{
                $messagefrom = $row['message_from'];
                        $messagefrombreak = wordwrap($messagefrom, 27, "\n&nbsp;&nbsp;", true);
                echo "<font style='float: right;'><b>You </b>said:&nbsp; &nbsp;</font><br>";
                echo "<section style='background: #960505; float: right; margin-right: 5px; border-radius: 7px; color: 
                white;'><font style='float: right;'>&nbsp; ".$messagefrombreak."&nbsp; &nbsp; &nbsp;</font></section><br><br>"; 
    }}}}       
            ?>

        </section>

【问题讨论】:

  • 究竟是什么不起作用? 任何事情会发生吗?是否报告错误?请注意,您正在将页面的“上下文”部分加载到现有的“上下文”部分,这可能是错误的。您应该用 &lt;div&gt; 或作为容器的东西包围 &lt;section&gt;,并将其用作 .load() 的目标。
  • 不,什么都没有发生。没有错误,它只是不工作。
    我已经完成了
    &lt;div id="context" name="context"&gt;&lt;div id="container" name="container"&gt;&lt;?php echo '&lt;b&gt;&amp;nbsp;'.$userfrom.'&lt;/b&gt;said:&lt;br&gt;';echo "&lt;section style='background: black; float: left; margin-left: 5px; border-radius: 7px; color: white;'&gt;&amp;nbsp;&lt;font style='float: left;'&gt; &amp;nbsp;".$messagefrombreak2."&lt;/font&gt;&lt;/section&gt;";?&gt; &lt;/div&gt;&lt;/div&gt; &lt;br&gt; `
  • 即使控制台也不显示错误
  • 您是否检查过“网络”开发人员选项卡以查看是否发出了 HTTP 请求?另请注意,如果在间隔计时器触发时页面不包含“#context”元素,则不会发生任何事情。
  • 容器元素也需要包含“上下文”块,而不是在里面。

标签: javascript php


【解决方案1】:

使用 ajax 和 json

ajax 在页面内调用服务器
json小简单格式
使用setInterval

喜欢

var myVar = setInterval(my_func, 5000);

my_func 是一个将持有 ajax 动作的函数。做你需要的事

【讨论】:

  • 我如何使用json比?你能链接我的东西或给出一个代码示例吗? Ajax 已经在我的脑海中
  • jQuery .load() 方法是 ajax。 JSON 不相关; OP 想要加载页面的一部分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-11-12
  • 2014-02-05
  • 1970-01-01
  • 1970-01-01
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多