<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>无标题文档</title>
</head>
<body>
<?php
require("action/login.php");
$loginObj = new login();
?>
<div class= "login_div">
<form id = "login_form" action="action/login.php" method="post" target="temp">
<h5>Name:
<span>
<?php
$loginObj->commitForm();
if(!($loginObj->state)){echo $loginObj->errMassage;}
?>
</span>
</h5>
<input name ="username" type="text">
<input type="submit" >
</form>
<iframe id="temp_iframe" name="temp" style="display:none;"></iframe>
</div>
</body>
</html>
<?php
class login
{
var $errMassage = "";
var $state = false;
var $name = "";
function __construct()
{
$this->name = $_POST["username"];
}
function commitForm()
{
if($this->name == '1234')
{
$this->state = true;
}
else
{
$this->errMassage = "error";
}
return $this->state;
}
}
好……运行index.php,提示“PHP Notice: Undefined index: username in C:\inetpub\wwwroot\Applications\page_A\action\login.php on line 10”
求问,这到底为什么?
php的提示信息,不是错误,关闭就行了
http://www.jb51.net/article/30328.htm
你这都还没提交呢,首次加载页面没有post数据, $_POST["username"]也就不存在了。必须是提交过后才有
构造函数那里改成isset($_POST['user_name']) ? trim($_POST['user_name']) : '';要进行排除未定义值
<?php
$loginObj->commitForm();
if(!($loginObj->state)){echo $loginObj->errMassage;}
?>
这部分是不是应该写在表单外面,每次提交就会运行这段代码
error_reporting(E_ALL & ~E_NOTICE);把这个放在页面里就好了
可以不要构造函数,在函数commitForm()函数中直接使用$_POST['username'];
function commitForm()
{
if($_POST['username'] == '1234')
{
$this->state = true;
}
else
{
$this->errMassage = "error";
}
return $this->state;
}
如何要使用构造函数就用三元表达式进行一个判断是否存在$_POST['username']