[英]isset code not being executed PHP
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.
谢谢。
If you click the link, nothing happens because the URL only contains the fragment identifier #
. 如果单击链接,则不会发生任何事情,因为URL仅包含片段标识符
#
。 Not even a GET
request will be issued. 甚至没有一个
GET
请求将被发出。
You use this kind of link normally to jump to an element inside the page (eg <a href="#top">Top</a>
to jump to an element with ID top
). 您通常使用这种链接跳到页面内的元素(例如,
<a href="#top">Top</a>
跳到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.
如果链接应执行某些JavaScript并且实际上不应链接至其他内容,则通常使用此方法。
You are testing the $_POST
array at the server side. 您正在服务器端测试
$_POST
数组。 But this array only contains elements, if you initiate a POST
request by a form. 但是,如果您通过表单启动
POST
请求,则此数组仅包含元素。 That means you need to create a form with a submit button, eg: 这意味着您需要使用提交按钮创建一个表单,例如:
<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. name
属性开始发挥作用,它将成为$_POST
数组中的键。 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: 您也可以使用链接来执行此操作,但是使用GET请求可以这样:
<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. 请注意,您必须在此处传递参数
logout
。
It seems you have mixed up GET
and POST
requests. 看来您混淆了
GET
和POST
请求。 If you have a form , the name
s of the form elements will be transmitted as parameters to the server. 如果您有一个form ,则表单元素的
name
s将作为参数传输到服务器。 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
数组将具有以下键:
$_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 ?
单击链接将创建一个普通的
GET
请求,并且此处的参数必须是URL的一部分,并附加在问号后面?
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 . 您可能应该阅读HTTP协议 。
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. 您需要使用
method="post"
形式的某种提交按钮来执行此操作。 regular links just result in GET requests and nothing will ever be posted that way. 常规链接只会导致GET请求,并且不会以这种方式发布任何内容。
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>
a
isnt a form control. a
心不是表单控件。 it needs to be an input
or select
if it's within a form. 它需要作为
input
或select
是否在表单中。
For manual linking, do href="/page?logout"
对于手动链接,请执行
href="/page?logout"
仅当您使用带有method=post
的表单时,才会填充$_POST
。
Yes. 是。 A POST and a GET are two different things ;)
POST和GET是两回事;)
if(isset($_GET['logout']))
This <a id="exit" name="logout" href="#">
should be <a id="exit" href="?logoff=true#">
. 此
<a id="exit" name="logout" href="#">
应该是<a id="exit" href="?logoff=true#">
。
Then logoff
will be in the $_GET
array. 然后
logoff
将在$_GET
数组中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.