-1

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

在此处输入图像描述

<?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

4 回答 4

1

您的if陈述是错误的[您的代码中没有这样$usrname的变量]

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

应该

if(($username=="")&&($pass_word==""))
于 2013-11-06T15:30:32.007 回答
0

我建议使用 Javascript:如果您在 logindiv 中所需的位置放置一个跨度。例如,该 span 的 id 为“errormessage”。现在您可以使用 Javascript 将消息放置在 span 中。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>";

使用跨度的一个好处是您可以使用 CSS 编辑错误消息的布局。我希望这是您问题的答案。

于 2013-11-06T15:40:10.757 回答
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: ");不会抛出已发送的标头错误。

于 2013-11-06T15:43:43.043 回答
-1

它在顶部输出它,因为这是你告诉它输出的地方 - 在 HTML 的实际开始之前。解决它的一种方法是将要显示的字符串保存在变量中,然后将变量显示在 HTML 中的适当位置。

于 2013-11-06T15:40:27.460 回答