【问题标题】:PHP jquery refresh DIVPHP jquery刷新DIV
【发布时间】:2014-02-17 10:00:08
【问题描述】:

我正在使用 PHP 和 JQuery 刷新 DIV。我正在测试我将使用的实际文件(.php)和我制作的 test.html 文件。出于某种原因,test.html 文件有效,而 .php 文件无效。

test.html:

<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">// <![CDATA[
    $(document).ready(function() {
    $.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
    setInterval(function() {
    $('#results').load('content.php');
    }, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
    });
    // ]]></script>
</head>

<body>
<div id="results">Loading users...</div>
</body>

</html>

view_conversation.page.inc.php(消息系统的一部分):

<html>

<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script type="text/javascript">// <![CDATA[
    $(document).ready(function() {
    $.ajaxSetup({ cache: false }); // This part addresses an IE bug. without it, IE will only load the first number and will never refresh
    setInterval(function() {
    $('#list').load('content.php');
    }, 3000); // the "3000" here refers to the time to refresh the div. it is in milliseconds.
    });
    // ]]></script>
</head>
<?php



    $errors = array();
    $valid_conversation = (isset($_GET['conversation_id']) && validate_conversation_id($_GET['conversation_id']));
    if ($valid_conversation === false){
            $errors[] = 'Invalid Conversation ID.';
    }
    if (isset($_POST['message'])){
            if (empty($_POST['message'])){
                    $errors[] = 'You must enter a message.';
            }

            if (empty($errors)){
                    add_conversation_message($_GET['conversation_id'], $_POST['message']);
            }
    }

 if (empty($errors) === false){
        foreach ($errors as $error){
            echo $error;
        }
}


    if ($valid_conversation){
            /*if (isset($_POST['message'])){
                    update_conversation_last_view($_GET['conversation_id']);*/
                    $messages = fetch_conversation_messages($_GET['conversation_id']);
            }else{
                    $messages = array();
                    update_conversation_last_view($_GET['conversation_id']);
            }

   session_start();
$_SESSION['messages']=$messages; 
    ?>

    <a href="index.php?page=inbox">Inbox</a>
    <a href="index.php?page=logout">Logout</a>

            <form action="" method="post">
                    <p><textarea name="message" rows="10" cols="85"></textarea></p>
                    <p><input type="submit" value="Add Message" /></p>
            </form>

            <?php
            //var_dump( $messages );
            if($messages){
            ?><html><body>
            <div id='list'>Loading messages...</div>
            </body></html><?php





    }
    ?>
    </html>

我的问题是:为什么 test.html 工作而 view_conversation.inc.php 不工作(顺便说一句,content.php 文件只包含用于测试目的的回声)

请帮帮我。

提前谢谢你。

【问题讨论】:

  • 刷新后加载 .php 文件时是否出现任何错误?
  • 不,.php 根本不刷新,它只是显示原始加载消息...
  • 我也尝试将 div 添加到 php 的开头,但得到了相同的结果
  • 为什么在 .php 文件的 html 标签中包含 php?
  • 好地方。我已经改变了它,但不幸的是它没有改变结果

标签: php jquery html reload


【解决方案1】:

session_start(); 应该在 PHP 文件的开头,在任何输出之前。在文件顶部尝试&lt;?php session_start(); ?&gt;。 (在&lt;html&gt; 标签之前)。

【讨论】:

  • 谢谢你;稍后当我通过会话传递变量时,它将非常有用。但现在 content.php 只是 echo 'test';
  • 我刚改了。我仍然得到相同的结果。
  • 但我明白,当我使用 $_SESSION 中的变量实际放置适当的内容时,这肯定很重要
  • 您的代码有两个&lt;html&gt; 标签。从if ($messages) 条件中删除&lt;html&gt;&lt;body&gt; 及其结束标记。将您的&lt;body&gt; 标签放在&lt;form&gt; 之前,并在&lt;/html&gt; 之前添加&lt;/body&gt;
  • 如果您仍然收到“正在加载消息...”,则您的 javascript 无法正常工作。您在控制台中收到任何错误吗? jQuery 加载是否正确?
猜你喜欢
  • 2012-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-08
  • 2012-05-20
  • 2011-03-06
相关资源
最近更新 更多