简体   繁体   English

每30秒或在用户提交表单时重新加载页面

[英]Reloading a page include every 30 seconds or when user submits form

I have a PHP page with a lot of includes which make up various parts of the page for a video website. 我有一个PHP页面,其中包含很多内容,这些内容构成了视频网站页面的各个部分。

I have a comments section which submits information into a database (Which works fine). 我有一个注释部分,它将信息提交到数据库中(哪个工作正常)。 But I need to make it so when this is done only the included page/div refreshes. 但是我需要这样做,以便在完成此操作时仅刷新包含的页面/ div。

This is the PHP: 这是PHP:

<form id="song-comment-form">
    <input type="hidden" value="<?=$rSong->id?>" class="song-id">
    <textarea class="editor" id="song-comment-textarea"></textarea><br>
    <input type="submit" value="Submit"><input type="button" value="Cancel" id="hide-song-comment-form">
    <hr>
</form>
<div id="player-song-comments">
    <?php $showComments?>
</div>

Here is my attempt at doing it with Javascript: 这是我尝试使用Javascript进行的尝试:

<script>

var $comments = $("#player-song-comments");
setInterval(function () {
    $comments.load("/template/sections/player_comments.php #player-song-comments");
}, 30000);

</script>

This should reload just the comments section but instead, everything from this point onwards goes blank. 这应该仅重新加载注释部分,但是从现在开始,所有内容都变为空白。

How it looks now when I press submit: 现在按提交时的外观: 在此处输入图片说明

When I reload that page manually: 当我手动重新加载该页面时:

在此处输入图片说明

I don't want the whole page to refresh because it contains a video. 我不希望整个页面刷新,因为它包含视频。

How can I make just that Refresh after submit is pressed OR every 30 seconds? 我怎样才能使提交后或每30秒刷新一次?

UPDATE: I have tried using JQuery to execute this. 更新:我尝试使用JQuery执行此操作。 I'm getting an error: 我收到一个错误:

Fatal error: Call to a member function query() on a non-object in /home/content/58/12052758/html/template/sections/header.php on line 42 致命错误:在第42行的/home/content/58/12052758/html/template/sections/header.php中的非对象上调用成员函数query()

    <script> 
        /*wait for the DOM to be loaded 
        $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#song-comment-form').ajaxForm(function() { 
                alert("Thank you for your comment!"); 
            }); 
        }); */   //THIS IS LINE 42
        $(document).ready(function() { 
        var options = {
            url: 'template/sections/player_comments',
            target: '#player-song-comments', // target element(s) to be updated with server response
            type: 'post' // 'get' or 'post', override for form's 'method' attribute
        }; 

    // bind form using 'ajaxForm' 
    $('#song-comment-form').ajaxForm(options); 
            $(document).ready(function() { 
            // bind 'myForm' and provide a simple callback function 
            $('#song-comment-form').ajaxForm(function() { 
                $("#player-song-comments").load('template/sections/player_comments.php');
                alert("Thank you for your comment! The site is currently in maintenance and the comment won't show until you revisit this video"); 
            }); 
        }); 
}); 
    </script> 

For those interested. 对于那些感兴趣。 Here is the whole page: http://pastebin.com/c0kQ3tGp 这是整个页面: http : //pastebin.com/c0kQ3tGp

You seems to load comments in PHP and as far as I know, PHP is only parsed once. 您似乎在PHP中加载了注释,据我所知,PHP仅被解析了一次。

The simplest workaround I know is to use an iframe that you would refresh, but I'm not sure this is a good practice tho. 我知道最简单的解决方法是使用要刷新的iframe,但是我不确定这是否是一个好习惯。

So there are two parts to your question: 因此,您的问题分为两部分:

How can I make just that Refresh after submit is pressed 我怎样才能使提交后刷新

You can use jquery-form for that. 您可以使用jquery-form In your case, you can initialize your form to something like this: 在您的情况下,您可以将表单初始化为以下形式:

$(document).ready(function() { 
    var options = {
        url: 'your_form_action',
        target: '#player-song-comments', // target element(s) to be updated with server response
        type: 'post' // 'get' or 'post', override for form's 'method' attribute
    }; 

    // bind form using 'ajaxForm' 
    $('#song-comment-form').ajaxForm(options); 
}); 

There are many other options you can play with. 您可以使用许多其他选项

OR every 30 seconds? 还是每30秒?

Try change your 尝试改变你的

$comments.load("/template/sections/player_comments.php #player-song-comments");

to

$comments.load("/template/sections/player_comments.php"); // remove the selector

According to the docs , the selector is used to insert fragments of the remote document. 根据docs ,选择器用于插入远程文档的片段。 In other words, when load was executed, jQuery parses the returned document to find the element #player-song-comments . 换句话说,当执行load时,jQuery解析返回的文档以查找元素#player-song-comments This element, along with its contents, is inserted into the element with an ID of result, and the rest of the retrieved document is discarded.... I assume #player-song-comments is not part of your response? 该元素及其内容将插入带有ID结果的元素中,而其余的检索文档将被丢弃。...我假设#player-song-comments不是您的响应的一部分?

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

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