按钮上的PHP条件 [英] php condition on button

查看:53
本文介绍了按钮上的PHP条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个问题:在下面的代码上,我制作了一个带有条件的表单,如果"profileid"在数组中,则打印添加为朋友"按钮,否则删除为朋友"按钮,但第二个条件不起作用,则不打印任何东西,当我第一次加载页面时,如果数组中已经有朋友ID",那么就存在添加到朋友"按钮.

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.

这是我的代码:

<?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 } ?>

推荐答案

我是PHP的初学者,请尝试回答.我发现回声存在问题.您曾经使用'来覆盖.

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>";

谢谢!

这篇关于按钮上的PHP条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆