我是一名PHP初学者。在我的代码中,我希望PHP脚本只有在点击提交按钮(set)时才能执行。所以当页面刷新时,isset()函数应该返回false。下面是test.php的isset()如何工作?
<html>
<head>
<title>A BASIC HTML FORM</title>
<?PHP
if (isset($_POST['Submit1'])) {
$username = $_POST['username'];
if ($username == "letmein") {
print ("Welcome back, friend!");
}
else {
print ("You're not a member of this site");
}
}
else{
$username = "";
}
?>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="test.php">
<Input Type = "text" Value ="<?php print $username?>" Name ="username">
<Input Type = "Submit" Name = "Submit1" Value = "Login">
</form>
</body>
</html>
代码点击执行脚本的登录按钮后,输出为“你是不是会员”,但是当我刷新页面,从剧本的消息仍然屏幕上。我相信isset()应该返回false,直到我再次点击按钮?代码有什么问题。 谢谢。
isset()用于检查是否定义了一个变量,而不是检查按钮按下。 –
如果刷新发布的表单,通常会再次发布表单数据...在当前地址之后的地址字段中按回车键,然后重新加载页面而不使用表单数据。 – jtheman
当您“刷新”时,刷新页面包括POST数据(因为这是最后一次“请求”)。你将不得不重新输入页面的地址来清除它。有时候你会看到“你真的想再次发送这个表单吗?”当你刷新时提示 - 这就是为什么... – Floris