2

我不断收到以下内容的“未定义索引”:

$username = $_POST['username'];
$email = $_POST['email'];
$email1 = "@";
$email_check = strpos($email,$email1);
$pwd = $_POST['pwd'];
$pwd_conf = $_POST['pwd_conf'];
$uLength = strlen($username);
$pLength = strlen($pwd);

我已经尝试过 if(isset()) 但错误只更改为“未定义变量”

if (isset($_POST['username'])) {
    $username = $_POST['username'];
}
if (isset($_POST['email'])) {
    $email = $_POST['email'];
}
$email1 = "@";
$email_check = strpos($email, $email1);
if (isset($_POST['pwd'])) {
    $pwd = $_POST['pwd'];
}
if (isset($_POST['pwd_conf'])) {
    $pwd_conf = $_POST['pwd_conf'];
}
$uLength = strlen($username);
$pLength = strlen($pwd);
4

4 回答 4

1

用这个

if(isset($_POST['username'])) {
    $username = $_POST['username'];
    $uLength = strlen($username);
}
if(isset($_POST['email'])) {
    $email = $_POST['email'];
    $email1 = "@";
    $email_check = strpos($email,$email1);
}

if(isset($_POST['pwd'])) {
    $pwd = $_POST['pwd'];
    $pLength = strlen($pwd);
}
if(isset($_POST['pwd_conf'])) {
    $pwd_conf = $_POST['pwd_conf'];
}
于 2013-01-16T14:23:42.577 回答
1

发生这种情况的原因是您的 $_POST 变量之一没有进入页面。

在您的第一次尝试中,您会得到未识别的索引,因为索引部分 ['xxxx'] 不存在。在第二个中,您的if语句正在工作,因此要么$username$email永远不会设置。当您尝试执行$email_check = strpos($email,$email1);$email 不存在时(当您尝试使用$usernameor时可能会发生这种情况$pwd)并且您得到“未识别的变量”。

有几种方法可以解决这个问题,但我会先检查你的帖子数据,看看你的页面有什么。可能有多种方法可以做到这一点,包括我不知道的 php 中的一种,但我喜欢使用 wireshark 并检查正在发送的 post 数据包;然后从那里调试。

于 2013-01-16T14:24:28.033 回答
0

很可能您正在使用 GET 方法调用您的代码,因此 $_POST 变量显然是空的。

您需要将所有处理程序代码放入此条件中

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
...
}

虽然针对 isset() 检查单独的字段并没有太大意义

如果您仍然收到这些错误 - 请检查您的表格

于 2013-01-16T15:15:45.020 回答
0

我的工作方式:

$errors = array();

if (!isset($_POST['username']))
    $errors[] = 'Please enter a username';

if (!isset($_POST['email']))
    $errors[] = 'Please enter an e-mailaddress';

if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
    $errors[] = 'Please enter a valid e-mailaddress';

if (!isset($_POST['pwd']))
    $errors[] = 'Please enter a password';

if (!isset($_POST['pwd_conf']))
    $errors[] = 'Please confirm your password';

if ($_POST['pwd'] != $_POST['pwd_conf'])
    $errors[] 'Passwords do not match';

if (count($errors) <= 0) {
    $username = $_POST['username'];
    $email = $_POST['email'];
    $pwd = $_POST['pwd'];
    $pwdConfirmation = $_POST['pwd_conf'];

    // Other logic
} else {
    foreach ($errors as $error)
        echo $error;
}
于 2013-01-16T14:28:09.707 回答