保留按钮的单击值并使用Jquery自动刷新 - php

我是入门级php家伙。我试图用后端的php和客户端的Jquery创建一个一对一的聊天程序。我的代码是这样的,单击按钮后,所选用户的值将存储在Jquery变量中,并传递给PHP端,这将带动两个用户之间的对话。
现在,挑战在于如何每隔10秒左右自动刷新一次对话(结果)。

这是我的代码。

<ul class="nav nav-pills nav-stacked mail-nav">

<?php
    $output .= '
    <ul class="nav nav-pills nav-stacked mail-nav">
    <li><a href="#" class="getuser">
    <i class="fa fa-envelope-o fa-fw"></i>'.$user.'</a></li>
    </ul>';
?>

<div class="sender">
    <form id="theform" > 
        <input type="text" name="messages"  placeholder="Send Message">
        <button class="btn btn-primary" id="geek"><i class="fa fa-lg fa-fw fa-paper-plane"></i></button>
    </form>
</div>

这是jQuery脚本

<script>
$(document).ready(function () {
    $(".getuser").click(function () { 
        var value = $(this).text(); 
        load_comment();

        function load_comment() {
            $.ajax({
                url:"getMessage.php",
                method: "GET",
                data: {
                    'getvalue':
                },
                success:function (data) {
                    $('.messages').html(data);
                    ///upon success scroll to bottom of message
                    $("#bottom").get(0).scrollIntoView({
                        behavior: "smooth", block: "end", inline: "nearest"}); 
                    }
                });
            setTimeout(function(){
            location.reload();
            },5000);
        }
    });
</script>

和getMessage.php

<?php
session_start();
require_once('config.php');
$Logged=$_SESSION['t_user'];

if(isset($_GET['getvalue'])) { 
$b = $_GET['getvalue'];
//store $b in a session
$_SESSION['getvalue'] = $b;
$thread = $_SESSION['getvalue'];

$sqlf= $conn->prepare("SELECT * FROM usermessages WHERE (userto = '$b' OR 
userto = '$Logged') 
AND (userfrom = '$Logged' OR userfrom = '$b') ORDER BY timecreated ASC ");

$sqlf->execute();
$row=$sqlf->FetchALL(PDO::FETCH_ASSOC);
$output = '';

if ($sqlf)
{///updating the value of seen
$up=$conn->prepare("UPDATE usermessages SET viewed= 1 WHERE (userto = '$b' 
OR userto = '$Logged') 
AND (userfrom = '$Logged' OR userfrom = '$b')");
$up->execute();
}
?>
<?PHP
foreach ($row as $r) 
{
$sender = $r['userfrom'];
$m = $r['messages'];

///time to get the user's thumb
$stmt = $conn->prepare("SELECT * FROM employees where t_user = ? limit 1 ");
$stmt->bindParam(1, $sender);
$stmt->execute();
$th=$stmt->FetchALL(PDO::FETCH_ASSOC);
foreach ($th as $p) 
{
$thumb = $p['passport'];
}

$output .= '

<div class="message">
<img src="'.$thumb.'" width="50" height ="50">
<p class="info">'.$m.'</p>
</div>';
}
}
echo  "You are chatting with $b";
echo $output;

echo '<div id="bottom"></div>';
?>

参考方案

假设load_comment()是对话功能,只需每10秒调用一次load_comment()函数。

setTimeout(function(){
load_comment();
},10000);

如何在提交时关闭fancybox…需要帮助 - php

当用户提交表单时,我真的需要关闭此弹出框(fancybox)...现在,它会在2秒后重定向到相同的表单(signup.php),但我想说的是“谢谢”消息,然后关闭框...<h1>SIGN UP</h1> <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.&…

将php变量传递给模式Twitter引导程序不起作用 - php

嗨,我正在将php变量传递给twitter boostrap modal。但单击模式内的链接时,我无法获得正确的ID。请参见下面的代码。首先,我创建了一个foreach函数,其中有数据循环,其次,我创建了一个链接,以显示模式3rd,我在模式内部有动作链接,用户可以在其中更新,删除数据。我已经实现的是,我可以将php的变量传递给模式操作,但是当我尝试传递给其他…

代码未在服务器目录php中创建文件 - php

我正在尝试使用以下代码将新文件写入服务器error_reporting(E_ALL); ini_set('display_errors', 1); if($_SERVER['REQUEST_METHOD'] == "POST") { $html = $_POST['html'];…

CodeIgniter更新查询被执行两次 - php

我正在使用CodeIgniter 2.2。每次访问页面时,我都必须用+1更新数据库。代码可以工作,但是每次都会增加+2。示例:如果是total views=2,则在单击页面后total views应该是3,但是数据库中的值是4。我确定我在控制器中仅调用一次模型add_one_to_view_image。控制者 function view(){ $view_i…

preg_replace排除<a href='''> </a> PHP - php

我正在使用preg_replace来替换带有href标签的文本中的关键字,我的正则表达式工作非常好,现在我的代码是:$newstring2 = preg_replace("/\p{L}*?".preg_quote($match[$i])."\p{L}*/ui", "<a href='"…