变量不会回声

时间:2010-12-25 04:27:56

标签: php variables

我有以下代码,当你输入一个值时,var $ username不会回显。     

//TODO: SET AUTH TOKEN as random hash, save in session
$auth_token =  rand();   

if (isset($_POST['action']) && $_POST['action'] == 'Login')
{

    $errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH ARE THEN ECHOED
    $username = $_POST['username'];

    if ($username = '')
    {
        $errors['username'] = 'Username is required';
    }

    echo $username; // var_dump($username) returns string 0
}


require_once 'login_form.html.php';

 ?>

login_form是这样的:

<form method="POST" action="">
<input type="hidden" name="auth_token" value="<?php echo $auth_token ?>">
Username: <input type="text" name="username">
Password: <input type="password" name="password1">
<input type="submit" name="action" value="Login">
</form>

身份验证令牌部分并不重要,只是当我在用户名文本框中输入值并按下登录按钮时,用户名不会回显,var_dump返回字符串(0),print_r只是空白。

4 个答案:

答案 0 :(得分:3)

愚蠢的错误

if ($username = '')   <-- this is an assignment

应该是这个

if ($username == '')  <-- this is comparison

答案 1 :(得分:2)

if ($username = '')

您错过了=,因此您要为$username分配一个空字符串。将其更改为

if ($username == '')
               ^-- note the 2 equal signs.

答案 2 :(得分:2)

此行是作业,而不是比较:

if ($username = '')

你想:

if ($username == '')

答案 3 :(得分:1)

//TODO: SET AUTH TOKEN as random hash
$auth_token =  rand();   

if (isset($_POST['action']) && $_POST['action'] == 'Login')
{

    $errors = array(); //USED TO BUILD UP ARRAY OF ERRORS WHICH ARE THEN ECHOED
    $username = $_POST['username'];

    if ($username == '') // **You are assiging not comparing** 
    {
        $errors['username'] = 'Username is required';
    }

    echo $username; // var_dump($username) returns string 0
}


require_once 'login_form.html.php';

 ?>

在您的登录表单中:(操作属性..)

 <form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
 <input type="hidden" name="auth_token" value="<?php echo $auth_token ?>">
 Username: <input type="text" name="username">
 Password: <input type="password" name="password1">
 <input type="submit" name="action" value="Login">
 </form>
相关问题