PHP 未执行 isset 代码
我想知道下面的代码是否正确,我遇到了很多错误,但不确定问题是否真的存在。代码如下:
用户将单击“退出组”。
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您单击该链接,则不会发生任何事情,因为该 URL 仅包含片段标识符
#
。 不会甚至不会发出GET
请求。通常使用这种链接跳转到页面内的元素(例如
Top
跳转到 ID 为的元素顶部)。 这完全在浏览器中处理。
如果您只将片段标识符放在那里,则什么也不会发生。如果链接应该执行一些 JavaScript 并且实际上不应该链接到其他内容,则经常使用这种方法。
您正在服务器端测试
$_POST
数组。但是,如果您通过表单发起POST
请求,则该数组仅包含元素。这意味着您需要创建一个带有提交按钮的表单,例如:name
属性开始发挥作用,它将成为$_POST
数组中的键。但是在普通链接上分配它不会有任何效果。您也可以使用链接来执行此操作,但使用以下方式的 GET 请求:
并
请注意,您必须在此处传递参数
logout
。您似乎混淆了
GET
和POST
请求。如果您有一个表单,则表单元素的名称
将作为参数传输到服务器。这意味着给定这种形式:如果用户单击提交按钮,服务器端的
$_POST
数组将具有键:但这不适用于链接。单击链接将创建一个普通的
GET
请求,这里的参数必须是 URL 的一部分,附加在问号?
后面,并用 & 符号 < code>&:将导致
您可能应该阅读有关 HTTP 协议 。
If you click the link, nothing happens because the URL only contains the fragment identifier
#
. Not even aGET
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 IDtop
). 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 aPOST
request by a form. That means you need to create a form with a submit button, e.g.: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:
and
Note that you have to pass a parameter
logout
it here.It seems you have mixed up
GET
andPOST
requests. If you have a form, thename
s of the form elements will be transmitted as parameters to the server. That means given this form:if the user clicks on the submit button, the
$_POST
array at the server side will have the keys: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&
:will result in
You probably should read about the HTTP protocol.
a
不是表单控件。如果它位于表单中,它必须是input
或select
。对于手动链接,请执行
href="/page?logout"
a
isnt a form control. it needs to be aninput
orselect
if it's within a form.For manual linking, do
href="/page?logout"
您使用的是常规超链接,不会发布任何表格。您需要在带有
method="post"
的表单中使用某种提交按钮来执行此操作。常规链接只会导致 GET 请求,并且不会以这种方式发布任何内容。编辑:添加了简单的示例:
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:
仅当您使用包含
method=post
的表单时才会填写$_POST
。$_POST
will only be filled if you use a form withmethod=post
.是的。 POST 和 GET 是两个不同的东西;)
Yes. A POST and a GET are two different things ;)
此
应为
。
然后
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.