php condition on button

僤鯓⒐⒋嵵緔 提交于 2019-12-25 08:12:19

问题


I have this problem: on the following code i made a form with a condition, where if the "profileid" is in array friends then print the button "add to friends" else "remove to friends" but the second condition don't work it don't prints anything and when i load the page for the first time, there is ever the button "add to friends" either if there's already the "friend id" in the array.

Here is my code:

<?php
        $userid = $_SESSION['userid'];
        $profileid = $_SESSION['profileID'];
        $compressed_friends=mysql_query("SELECT friends FROM users WHERE id LIKE '$userid'");
        $friends = explode (',',$compressed_friends);
        if(isset($_POST['addFriends']))
        {
            $compressed_friends=$profileid.','.$compressed_friends;
            mysql_query("UPDATE users SET friends='$compressed_friends' WHERE id='$userid'");
        }
        elseif(isset($_POST['removeFriends']))
        {
            array_filter($friends,$profileid);
            $compressed_friends=implode(',', $friends);
            mysql_query("UPDATE users SET friends='$compressed_friends' WHERE id='$userid'");
        }

        else
        {
        ?>
        <form role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
        <?php
            if(!in_array($profileid, $friends))
            {
                echo' <button type="submit" name="addFriends" class="btn btn-primary col-lg-3">Add to friends</button>';
            }
            elseif(in_array($profileid, $friends))
            {
                echo '<button type="submit" name="removeFriends" class="btn btn-danger col-lg-3">Remove to Friends</button>';
            }
        ?>
        </form>
        <?php } ?>

回答1:


Let me try to answer as I am a beginner in PHP. I found there is the problem on echo. You used ' to cover the ".

  echo' <button type="submit" name="addFriends" class="btn btn-primary col-lg-3">Add to friends</button>';

You can try this out, perhaps it will solve your problem.

  echo  "<button type='submit' name='addFriends' class='btn btn-primary col-lg-3'>Add to friends</button>";

Thanks!



来源:https://stackoverflow.com/questions/28484951/php-condition-on-button

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!