密码中的错误消息在页面加载时可见?

时间:2014-09-14 15:20:59

标签: php

如何在单击“提交”按钮之前阻止显示错误消息?

出于某种原因,'else'语句的内容在页面加载中可见 - 消息“NO”只应在输入“a”之外的其他内容后出现..我做错了什么?

<form method="POST">
  <input type="text" class="textfield" id="cursor" name="pass"> 
  <input type="submit" class="button" name="submit" value="OK"> 
</form>

<?php
$pass = $_POST['pass'];

    if($pass == 'a') {      
        echo "YES";
} 
    else{
        echo "NO";
}
?>

3 个答案:

答案 0 :(得分:1)

添加此PHP代码:

<?php
if(isset($_POST['pass'])) {
    // your check here
}
?>

答案 1 :(得分:0)

测试提交按钮是否在提交的数据中。

if (isset( $_POST['submit'] )) {

答案 2 :(得分:0)

<form method="POST">
  <input type="text" class="textfield" id="cursor" name="pass"> 
  <input type="submit" class="button" name="submit" value="OK"> 
</form>

<?php
$pass = $_POST['pass'];
//You need to check if the form is submitted
    if($_POST['submit']){
        if(isset($pass) && !empty($pass) && $pass == 'a') {      
            echo "YES";
    } 
        else{
            echo "NO";
    }
?>