回声错误消息

时间:2013-11-06 15:29:33

标签: php html function header echo

我们有一个登录页面,用于验证数据库中的用户名和密码,但是我们希望它在输入字段上方而不是在页面顶部显示无效的用户名和密码消息。我们知道我们可以编辑并将php代码放在div中以显示它所在的位置,我们放出了标题,因此只要我们从页面顶部移动php就会生成错误消息。有替代方案吗?或者使用标题的方式不同?我们的代码在下面,是我们想要的图像......

enter image description here

<?php
        error_reporting (E_ALL ^ E_NOTICE);
        $username = $_POST['usrname'];
        $pass_word = $_POST['pass_word'];
        if(($usrname=="")&&($pass_word==""))
        {
            //Do nothing as nothing has been posted......
        }
        else
        {
            $con = mysql_connect("localhost"," "," ");
            if (!$con)
            {
                die('Could not connect: ' . mysql_error());
            }
            mysql_select_db("", $con);

            $result = mysql_query("SELECT * FROM `leicester_login` WHERE `user_name`='$username'", $con);
            $num_row = mysql_num_rows($result);
        }

        if($username=="")
        {
            //do nothing as nothing has been posted
        }
        else
        {
            if($num_row == 0)
            {
            echo "<p>The username <b>". $username ."</b> doesnt exist!</p>";
            }
            else
            {
                $return_pass = mysql_fetch_array($result);
                if($pass_word == $return_pass['pass_word'])
                {
                    session_start();
                    if(isset($_SESSION['username']))
                    {
                        //do nothing as session already exists...
                        header("Location:  ");
                    }
                    else
                    {
                        $_SESSION['username']= $username;
                        header("Location:  ");
                        exit;
                    }
                }
                else
                {
                    echo "<p>The password entered is incorrect!</p>";
                }
            }
        }
    ?>


    <html lang="en">
      <head>
      </head>

      <body>

        <div class="container">

    <form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" class="form-signin">
        <h2 class="form-signin-heading"><img src="" alt="Logo"/>Leicester</h2>
        <?php

    ?>
        <h6 class="form-signin-heading">You are not logged in, please login.</h6>

            <input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">
            <input type="password" name="pass_word" id="pass_word" class="input-block-level" placeholder="Password">
        <center><button align="center" class="btn btn-large btn-primary" type="submit">Sign in</button></center>
    <a href="shires.php" class="btn btn-link">< Go Back</a>
          </form>
      </div> <!-- /container -->

      </body>
    </html>

4 个答案:

答案 0 :(得分:1)

您的 if 声明错误[您的代码中没有 $usrname 变量

if(($usrname=="")&&($pass_word==""))

应该是

if(($username=="")&&($pass_word==""))

答案 1 :(得分:0)

我建议使用Javascript: 如果您在logindiv中将span放在所需的位置。例如该span的id为'errormessage'。现在,您可以使用Javascript将消息放入范围中。 HTML:

   <h6 class="form-signin-heading">You are not logged in, please login.</h6>

      <span id="errormessage"></span>

        <input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">

PHP:

  //an error has occurred...
  echo "<script>document.getElementById('errormessage').innerHTML='The password entered is incorrect!'</script>";

使用span的一个好处是您可以使用CSS编辑errormessage的布局。 我希望这是你问题的答案。

答案 2 :(得分:0)

试试这个:

<?php
    error_reporting (E_ALL ^ E_NOTICE);
    $username = $_POST['usrname'];
    $pass_word = $_POST['pass_word'];
    if(($usrname=="")&&($pass_word==""))
    {
        //Do nothing as nothing has been posted......
    }
    else
    {
        $con = mysql_connect("localhost"," "," ");
        if (!$con)
        {
            die('Could not connect: ' . mysql_error());
        }
        mysql_select_db("", $con);

        $result = mysql_query("SELECT * FROM `leicester_login` WHERE `user_name`='$username'", $con);
        $num_row = mysql_num_rows($result);
    }

    if($username=="")
    {
        //do nothing as nothing has been posted
    }
    else
    {
       $message = null; //create a container for the messages

        if($num_row == 0)
        {
        $message = "<p>The username <b>". $username ."</b> doesnt exist!</p>";
        }
        else
        {
            $return_pass = mysql_fetch_array($result);
            if($pass_word == $return_pass['pass_word'])
            {
                session_start();
                if(isset($_SESSION['username']))
                {
                    //do nothing as session already exists...
                    header("Location:  ");
                }
                else
                {
                    $_SESSION['username']= $username;
                    header("Location:  ");
                    exit;
                }
            }
            else
            {
                $message = "<p>The password entered is incorrect!</p>";
            }
        }
    }
?>


<html lang="en">
  <head>
  </head>

  <body>

    <div class="container">

<form action="<?php $_SERVER['PHP_SELF']; ?>" method="post" class="form-signin">
    <h2 class="form-signin-heading"><img src="" alt="Logo"/>Leicester</h2>
    <?php

     //if there's something in the container, echo it out.

     if (!is_null($message)) {
      echo $message;
     }
?>
    <h6 class="form-signin-heading">You are not logged in, please login.</h6>

        <input name="usrname" id="usrname" type="text" class="input-block-level" placeholder="Username">
        <input type="password" name="pass_word" id="pass_word" class="input-block-level" placeholder="Password">
    <center><button align="center" class="btn btn-large btn-primary" type="submit">Sign in</button></center>
<a href="shires.php" class="btn btn-link">< Go Back</a>
      </form>
  </div> <!-- /container -->

  </body>
</html>

每当调用echo时,它会在那里输出一些东西,如果它在任何输出之前(如html部分),它将在HTML开始之前显示。如果你把一些东西放在一个容器里(比如$message)并用html输出它会显示在预定的位置。作为副作用,header("Location: ");不会抛出已发送错误的标头。

答案 3 :(得分:-1)

它正在输出它,因为这是你告诉它输出的地方 - 在实际启动HTML之前。解决此问题的一种方法是保存字符串以显示在变量中,然后在HTML中的适当位置显示变量。