gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 05:15:26 24 4
gpt4 key购买 nike

我是一名入门级 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>';
?>

最佳答案

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

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

关于php - 保留按钮的点击值并使用 Jquery 自动刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49939932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com