ユーザーが別のユーザーをフォローできるフォロー/アンフォローシステムを作成しました。クリックされたボタンに応じてデータベースの内容を挿入および削除するsql/mysqlが機能しています。しかし、PHPの動的データのために、ユーザは、腐食性の出力を表示するためにリフレッシュする必要があります(フォロー/フォロー)。この問題を解決するにはどうすればいいですか?PHPのフォロー/アンフォローボタンが機能していますが、結果を見るためにリフレッシュが必要です
これは、両方の私の機能が使用されます。
機能のために、ユーザが以下の場合、またはに応じて
//use this function to follow an uploader
function follow($userid, $uploader_id){
if(isset($_POST['follow']))
{
if(logged_in()){
if($userid != $uploader_id){
$con = mysqli_connect('localhost', 'root', '', 'db');
$userid = sanitize($userid);
$uploader_id = sanitize($uploader_id);
//prepare statement
$stmt = $con ->prepare("INSERT INTO follow (u_id, uploader_id) VALUES (?, ?)");
$stmt -> bind_param("ss",$userid, $uploader_id);
if($result = $stmt->execute()){
}
else{
echo "Failed to follow";
}
}// users cant follow themselves
}//if not logged in do this
else{
echo "<h1 style='clear:left;font-size:15px; color:red;'>You have to be logged in to follow</h1>";
}
}
}
HTML/PHPのボタンをたどるためにこれは、関数である
//unfollow selected uploader if already following
function unfollow($userid, $uploader_id){
if(isset($_POST['unfollow'])){
$con = mysqli_connect('localhost', 'root', '', 'db');
$userid = sanitize($userid);
$uploader_id = sanitize($uploader_id);
//prepare statement
$stmt = $con ->prepare("DELETE FROM follow
WHERE u_id = ? AND uploader_id = ?");
$stmt -> bind_param("ss",$userid, $uploader_id);
if($result = $stmt->execute()){
}else{
echo "failed to unfollow";
}
}
}
フォローない:
//if the user is not logged in just display a button that send him/her to the login page if clicked
if(!logged_in()){
?>
<form id="follow" action="login.php">
<button style="float:left;" class="follow_btn" type="submit">Follow</button>
</form>
<?php
}else if($uploaderid == $session_user_id){
}
//if the user is not following the uploader show the follow button
else if(!is_following($session_user_id, $uploaderid)){
?>
<form id="follow" method="POST" action="<?php follow($session_user_id, $uploaderid) ?>">
<button style="float:left;" id="follow_btn" class="follow_btn" type="submit" name="follow">Follow</button>
</form>
<?php
//else show the unfollow button
}else{
?>
<form id="follow" method="POST" action="<?php unfollow($session_user_id, $uploaderid) ?>">
<button style="float:left;" id="unfollow_btn" class="follow_btn" type="submit" name="unfollow">Following</button>
</form>
<?php
}
?>
もすぐにページをリロード避けるために、設定されたタイムアウト機能でクリックイベントをラップする必要があります。 – Arjun
提出時にページを更新するだけですか?ボタンタグにonClick = "window.location.reload()"を追加する – brad
@brad私はそれがうまくいっていないことを試みました、私はPHPが最初にリロード要求を無視していると思います。 –