我有一个函数,显示文件是否存在,如果存在,他将返回文件路径,如果不存在,他仍然会给我路径,这样他就可以创建文件:
function getnamechats($user1,$user2){
$filename1="chats/chat".$user1."&".$user2.".json";
$filename2="chats/chat".$user2."&".$user1.".json";
if (file_exists($filename1)) {
return $filename1;
}
else if (file_exists($filename2)) {
return $filename2;
}
else{ return $filename1;}
}它可以很好地创建/打开文件来编写它,我已经测试了很多次,而且我的json文件每次都会被更新:
function send_chat($nick,$chat){
global $userid;global $chatparse;
$heasda=getnamechats($userid,$chatparse);
$ok=$heasda;
// read/write
$filename = "$heasda";
$fopen = fopen($filename,"r");
$fgets = fgets($fopen);
fclose($fopen);
$decode = json_decode($fgets,true);
// limit 10
end($decode);
if(key($decode) >= 10){
array_shift($decode);
$new_key =10;
}
else{
$new_key = key($decode);
$new_key++;}
$format = array($nick,$chat);
$decode[$new_key] = $format;
$encode = json_encode($decode);
// write
$fopen_w = fopen($filename,"w");
fwrite($fopen_w,$encode);
fclose($fopen_w);
}但是,在打开/创建它来读取它的函数中,我得到了以下错误:第一个变量是正确的(1),但是第二个变量(在&之后的虚拟变量)不工作,错误HTMLParagraphElement出现了,例如:
聊天/聊天1&对象HTMLParagraphElement.json
然后,在触发新的msg时,我再次调用getnamechats()函数,以检查该文件是否仍然存在,如果仍然存在,它将将变量$heasda发送给show_chat($heasda),基本上它将执行与send_chat相同的操作,但它将读取它:
function show_chat($heasda){
print_r($heasda);
$filename = $heasda;
$fopen = fopen($filename,"r");
$fgets = fgets($fopen);
fclose($fopen);
$decode = json_decode($fgets,true);
$val .= "<table id='table' class=\"table table-condensed\">";
foreach($decode as $post){
$val .= "<tr><td><b style=\"color:#{$post[0]}\">{$post[0]}</b>: {$post[1]}</td></tr>";}
$val .= "</table>";
return $val;
}
if(isset($_POST["chat"]) && $_POST["chat"] != ""){
$nick = $_SESSION['iduser'];
$chat = $_POST["chat"];
send_chat($nick,$chat);
}
if(isset($_GET["chat"]) && $_GET["chat"] != ""){
global $userid;global $chatparse;
$heasda=getnamechats($userid,$chatparse);
echo show_chat($heasda);
exit;
}
?>就像有人说的,代码也可以是JavaScript,我已经读过了,但我仍然不太明白:
function autoloadpage() {
$.ajax({
url: "?chat=1&chat-pars="+secnum,
type: "POST",
success: function(data) {
$("div#chat").html(data);
}
});
}发布于 2020-08-07 19:15:45
secnum是一个DOM元素,而不是其中的文本。你得拿到短信。
您还应该调用encodeURIComponent,以防它包含在URL中具有特殊意义的字符。
function autoloadpage() {
$.ajax({
url: "?chat=1&chat-pars="+encodeURIComponent(secnum.innerText),
type: "POST",
success: function(data) {
$("div#chat").html(data);
}
});
}https://stackoverflow.com/questions/63307805
复制相似问题