PHP 未执行 isset 代码

发布于 2024-09-10 15:38:03 字数 459 浏览 13 评论 0原文

我想知道下面的代码是否正确,我遇到了很多错误,但不确定问题是否真的存在。代码如下:

用户将单击“退出组”。

<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>

单击“退出组”时应执行的代码如下:

if(isset($_GET['logout'])){ 

    //CODE TO BE EXECUTED        
                            } 

但是,当用户单击时我尝试执行的代码“退出小组”甚至没有被执行。 大括号内的代码没有任何问题,很多人都检查过。但我想知道我的问题是否出在上面的代码中?谢谢。

I was wondering if my code below is even correct, I've been having numerous errors with this, but am not sure if the problem really exists here. The code is below:

The user will click 'Exit Group'.

<p class="logout"><a id="exit" name="logout" href="#">Exit Group</a></p>

The code that should be execute when 'Exit Group' is clicked is below:

if(isset($_GET['logout'])){ 

    //CODE TO BE EXECUTED        
                            } 

However, the code I am trying to execute when the user clicks 'Exit Group' is not even being executed. There is nothing wrong with the code within the braces, as numerous people have checked it. But I was wondering if my problem may lie in the code above? Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

夜巴黎 2024-09-17 15:38:03

如果您单击该链接,则不会发生任何事情,因为该 URL 仅包含片段标识符#不会甚至不会发出 GET 请求。

通常使用这种链接跳转到页面内的元素(例如 Top 跳转到 ID 为 的元素顶部)。 这完全在浏览器中处理

如果您只将片段标识符放在那里,则什么也不会发生。如果链接应该执行一些 JavaScript 并且实际上不应该链接到其他内容,则经常使用这种方法。


您正在服务器端测试 $_POST 数组。但是,如果您通过表单发起 POST 请求,则该数组仅包含元素。这意味着您需要创建一个带有提交按钮的表单,例如:

<form action="" method="POST">
    <input type="submit" name="logout" value="Exit Group" />
</form>

name 属性开始发挥作用,它将成为 $_POST 数组中的键。但是在普通链接上分配它不会有任何效果。


您也可以使用链接来执行此操作,但使用以下方式的 GET 请求:

<a id="exit" href="?logout=1">Exit Group</a>
<!--                 ^-- parameter must be part of the URL, name has no effect    -->

if(isset($_GET['logout'])){    
    //CODE TO BE EXECUTED        
} 

请注意,您必须在此处传递参数logout


您似乎混淆了 GETPOST 请求。如果您有一个表单,则表单元素名称将作为参数传输到服务器。这意味着给定这种形式:

<form method="POST">
    <input type="text" name="foo" value="" />
    <input type="text" name="bar" value="" />
    <input type="submit" name="send" value="Send" />
</form>

如果用户单击提交按钮,服务器端的 $_POST 数组将具有键:

$_POST['foo']
$_POST['bar']
$_POST['send']

但这不适用于链接。单击链接将创建一个普通的 GET 请求,这里的参数必须是 URL 的一部分,附加在问号 ? 后面,并用 & 符号 < code>&:

<a href="somePage.php?foo=1&bar=1&andMore=0"> Link </a>

将导致

$_GET['foo']
$_GET['bar']
$_GET['andMore']

您可能应该阅读有关 HTTP 协议

If you click the link, nothing happens because the URL only contains the fragment identifier #. Not even a GET request will be issued.

You use this kind of link normally to jump to an element inside the page (e.g. <a href="#top">Top</a> to jump to an element with ID top). This is completely handled in the browser.

And if you only put the fragment identifier there, just nothing will happen. This is very often used if the link should execute some JavaScript and should actually not link to something else.


You are testing the $_POST array at the server side. But this array only contains elements, if you initiate a POST request by a form. That means you need to create a form with a submit button, e.g.:

<form action="" method="POST">
    <input type="submit" name="logout" value="Exit Group" />
</form>

Here comes the name attribute into play, which will be the key in the $_POST array. But assigning this on a normal link will have no effect.


You could do it also with the link, but with a GET request this way:

<a id="exit" href="?logout=1">Exit Group</a>
<!--                 ^-- parameter must be part of the URL, name has no effect    -->

and

if(isset($_GET['logout'])){    
    //CODE TO BE EXECUTED        
} 

Note that you have to pass a parameter logout it here.


It seems you have mixed up GET and POST requests. If you have a form, the name s of the form elements will be transmitted as parameters to the server. That means given this form:

<form method="POST">
    <input type="text" name="foo" value="" />
    <input type="text" name="bar" value="" />
    <input type="submit" name="send" value="Send" />
</form>

if the user clicks on the submit button, the $_POST array at the server side will have the keys:

$_POST['foo']
$_POST['bar']
$_POST['send']

This does not work with links though. A click on a link will create a normal GET request, and here, the parameters must be part of the URL, appended after a question mark ? and separated by an ampersand &:

<a href="somePage.php?foo=1&bar=1&andMore=0"> Link </a>

will result in

$_GET['foo']
$_GET['bar']
$_GET['andMore']

You probably should read about the HTTP protocol.

吹梦到西洲 2024-09-17 15:38:03

a 不是表单控件。如果它位于表单中,它必须是inputselect

对于手动链接,请执行 href="/page?logout"

a isnt a form control. it needs to be an input or select if it's within a form.

For manual linking, do href="/page?logout"

无力看清 2024-09-17 15:38:03

您使用的是常规超链接,不会发布任何表格。您需要在带有 method="post" 的表单中使用某种提交按钮来执行此操作。常规链接只会导致 GET 请求,并且不会以这种方式发布任何内容。

编辑:添加了简单的示例:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title>Form test</title>
    </head>
    <body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
        <pre><? print_r($_POST)?></pre>
<?endif;?>
        <? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
        <? // we'll end up back in this file when the form is submitted.     ?>
        <form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
            <input type="text" name="textbox" 
                value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
            <input type="submit" name="submitbutton" value="Submit" />
        </form>
    </body>
</html>

You're using a regular hyperlink, no form will get posted. you need a submit button of some kind in a form with method="post" to do that. regular links just result in GET requests and nothing will ever be posted that way.

edit: added simple example:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN">
<html>
    <head>
        <title>Form test</title>
    </head>
    <body>
<?if ($_SERVER['REQUEST_METHOD'] == 'POST'):?>
        <pre><? print_r($_POST)?></pre>
<?endif;?>
        <? // $_SERVER['REQUEST_URI'] holds the current URL, so we know that ?>
        <? // we'll end up back in this file when the form is submitted.     ?>
        <form method="post" action="<?= $_SERVER['REQUEST_URI']; ?>">
            <input type="text" name="textbox" 
                value="<?= isset($_POST['textbox'])?$_POST['textbox']:'Type something' ?>" />
            <input type="submit" name="submitbutton" value="Submit" />
        </form>
    </body>
</html>
微凉徒眸意 2024-09-17 15:38:03

仅当您使用包含 method=post 的表单时才会填写 $_POST

$_POST will only be filled if you use a form with method=post.

狼性发作 2024-09-17 15:38:03

是的。 POST 和 GET 是两个不同的东西;)

if(isset($_GET['logout']))

Yes. A POST and a GET are two different things ;)

if(isset($_GET['logout']))
白首有我共你 2024-09-17 15:38:03

应为

然后 logoff 将位于 $_GET 数组中。

This <a id="exit" name="logout" href="#"> should be <a id="exit" href="?logoff=true#">.

Then logoff will be in the $_GET array.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文