Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 回声&x27;正在删除错误消息_Php_Html_Function_Header_Echo - Fatal编程技术网

Php 回声&x27;正在删除错误消息

Php 回声&x27;正在删除错误消息,php,html,function,header,echo,Php,Html,Function,Header,Echo,我们有一个从数据库验证用户名和密码的登录页面,但是我们希望它在输入字段上方而不是页面顶部显示无效用户名和密码消息。我们知道我们可以编辑php代码并将其放在div中以显示它所在的位置,我们会输出标题,因此一旦我们将php从页面顶部移动,它就会生成一条错误消息。还有别的办法吗?还是以不同的方式使用标题?下面是我们的代码和我们想要的图像 <?php error_reporting (E_ALL ^ E_NOTICE); $username = $_POST['us

我们有一个从数据库验证用户名和密码的登录页面,但是我们希望它在输入字段上方而不是页面顶部显示无效用户名和密码消息。我们知道我们可以编辑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>



如果
语句错误,您的
[代码上没有这样的
$usrname
变量]

if(($usrname=="")&&($pass_word==""))
应该是

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

我建议使用Javascript: 如果在logindiv中的所需位置放置跨度。e、 g.该跨度的id为“errormessage”。现在,您可以使用Javascript将消息放置在span中。 HTML:

您尚未登录,请登录。
PHP:

//发生了一个错误。。。
echo“document.getElementById('errormessage').innerHTML='输入的密码不正确!'”;
使用span的一个好处是可以使用CSS编辑errormessage的布局。
我希望这是对您的问题的回答。

它将它输出到最上面,因为这是您告诉它输出的地方-在HTML实际开始之前。解决此问题的一种方法是保存字符串以显示在变量中,然后在HTML中的适当位置显示变量。

尝试以下方法:

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


它在变量为usr/username的情况下工作。谢谢,我不知道变量是不同的,所以谢谢你。但仍然没有回答我们的问题。谢谢,谢谢!工作完美。
  //an error has occurred...
  echo "<script>document.getElementById('errormessage').innerHTML='The password entered is incorrect!'</script>";
<?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>