猿问

显示不同的按钮或刷新

我一直在尝试为论坛帖子制作一个喜欢/不喜欢的按钮,我已经取得了一些成功,下面的代码可以正常工作,但我无法刷新页面。


例如 - 当我单击提交按钮(喜欢)时,数据库已更新,但未显示不同的按钮。如果我刷新页面,现在有一个不同的按钮,那么现在有一种方法可以用 jquery 来做到这一点,所以一旦点击了类似的按钮,它就会显示按钮不像


/* Check Database */

$likes = DB::run("SELECT * FROM likes WHERE id=? AND lid=? AND uid=? AND pid=?", [1, 1, $CURUSER['id'], $row['id']])->fetch();


/* Check Result */

var_export($likes);


/* If No Result Like */

if (!$likes) {

echo

"<form action='' method='post'>

<input type='submit' name='like' value='Like' />

</form>";


if(isset($_POST['like']))

{

    DB::run("INSERT INTO likes (id, lid, uid, pid) VALUES (?, ?, ?, ?)", [1, 1, $CURUSER['id'], $row['id']]);

}


}


/* If Result UnLike */

if ($likes) {

echo

"<form action='' method='post'>

<input type='submit' name='unlike' value='Unlike' />

</form>";


if(isset($_POST['unlike']))

{

DB::run("DELETE FROM likes WHERE id=? AND lid=? AND uid=? AND pid=?", [1, 1, $CURUSER['id'], $row['id']]);

}


}


/* Show Results */

echo '</br>';

echo $likes['id'];

echo $likes['1id'];

echo $likes['uid'];

echo $likes['pid'];


达令说
浏览 101回答 2
2回答

繁星点点滴滴

表单显示后数据库被修改,因此新的“喜欢”状态不会反映在页面上。一种解决方案是在检查“喜欢”状态并显示表单之前修改数据库。下面,我使用 PHP 的header.&nbsp;刷新页面不是绝对必要的,但它确实有助于防止用户刷新时重复提交。这称为Post/Redirect/Get 模式。Post/Redirect/Get (PRG) 是一种 Web 开发设计模式,它允许重新加载、共享或添加表单提交后显示的页面而不会产生不良影响,例如再次提交表单。if(isset($_POST['like'])) {&nbsp; &nbsp; DB::run("INSERT INTO likes (id, lid, uid, pid) VALUES (?, ?, ?, ?)", [1, 1, $CURUSER['id'], $row['id']]);&nbsp; &nbsp; header('Location: '.$_SERVER['REQUEST_URI']);&nbsp; &nbsp; exit;} elseif (isset($_POST['unlike'])) {&nbsp; &nbsp; DB::run("DELETE FROM likes WHERE id=? AND lid=? AND uid=? AND pid=?", [1, 1, $CURUSER['id'], $row['id']]);&nbsp; &nbsp; header('Location: '.$_SERVER['REQUEST_URI']);&nbsp; &nbsp; exit;}/* Check Database */$likes = DB::run("SELECT * FROM likes WHERE id=? AND lid=? AND uid=? AND pid=?", [1, 1, $CURUSER['id'], $row['id']])->fetch();if (!$likes) {&nbsp; &nbsp; /* If No Result Like */&nbsp; &nbsp; echo "<form action='' method='post'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type='submit' name='like' value='Like' />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>";} else {&nbsp; &nbsp; /* If Result UnLike */&nbsp; &nbsp; echo "<form action='' method='post'>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input type='submit' name='unlike' value='Unlike' />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </form>";}

慕标5832272

没有人可以帮助你,因为我们看不到你的$likes结果,这里是伪代码,它是如何工作的,在哪里$likes硬编码为 false 开始(不喜欢),当你点击喜欢时,会显示不同按钮。在你得到这个之后,你应该调试$likes每个时刻包含的内容以及为什么它没有进入你的 if 语句:<?php/* Check Database */$likes = false;/* Check Result */var_export($likes);/* If No Result Like */if (!$likes) {echo"<form action='' method='post'><input type='submit' name='like' value='Like' /></form>";if(isset($_POST['like'])){&nbsp; &nbsp; $likes = true;}}/* If Result UnLike */if ($likes) {echo"<form action='' method='post'><input type='submit' name='unlike' value='Unlike' /></form>";if(isset($_POST['unlike'])){&nbsp; &nbsp; $likes = false;}}/* Show Results */
随时随地看视频慕课网APP
我要回答