简体   繁体   中英

Follow / unfollow button using php and html

I made a follow unfollow button for my social media wesbiste using html/css and php. If I am not following the button should print follow and I need to be able to follow him. If I am following the user the button should print unfollow and I should unfollow him.

HTML

<form>
<?php
if ($userid != $followerid) {
    if ($isFollowing) {
       echo '<input type="submit" name="unfollow" value="unfollow", style="width:100%;background-image:url(&quot;none&quot;);background-color:#da052b;color:#fff;padding:16px 32px;margin:0px 0px 6px;border:none;box-shadow:none;text-shadow:none;opacity:0.9;text-transform:uppercase;font-weight:bold;font-size:13px;letter-spacing:0.4px;line-height:1;outline:none>';              
    } else {
        echo '<input type="submit" name="follow" value="Follow", style="width:100%;background-image:url(&quot;none&quot;);background-color:#da052b;color:#fff;padding:16px 32px;margin:0px 0px 6px;border:none;box-shadow:none;text-shadow:none;opacity:0.9;text-transform:uppercase;font-weight:bold;font-size:13px;letter-spacing:0.4px;line-height:1;outline:none>';
    }
}
?>
</form>

PHP

if (isset($_POST['follow'])) {
    if ($userid != $followerid) {
        if (!DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
            if ($followerid == 6) {
                DB::query('UPDATE users SET verified=1 WHERE id=:userid', array(':userid'=>$userid));
            }
            DB::query('INSERT INTO followers VALUES (null, :userid, :followerid)', array(':userid'=>$userid, ':followerid'=>$followerid));
        } else {
            echo 'Already following!';
        }
        $isFollowing = True;
    }
}

if (isset($_POST['unfollow'])) {
    if ($userid != $followerid) {
        if (DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
            if ($followerid == 6) {
                DB::query('UPDATE users SET verified=0 WHERE id=:userid', array(':userid'=>$userid));
            }
            DB::query('DELETE FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid));
        }
        $isFollowing = False;
    }
}
if (DB::query('SELECT follower_id FROM followers WHERE user_id=:userid AND follower_id=:followerid', array(':userid'=>$userid, ':followerid'=>$followerid))) {
    $isFollowing = True;
}

When I press the button it gives me an error and all the posts made by the user are gone and the url changing to: http://localhost/profile.php?unfollow=unfollow&postbody=&postimg=

It could be that your problem is here:

<form>

Change it to

<form action="somewhere.php" method="post">

add method="post"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM