PHP / HTML提交表单后无法跳转到另一个.php页面?

时间:2014-10-26 20:32:55

标签: php html html5

在简单的login.php中点击“登录”后,它会输出一个“未找到对象”并且链接“localhost / post?username = asd& password = asd& Submit = Log + In”。任何人都可以帮我找出什么是错的?

这是初始页面login.php

的编码
<!DOCTYPE html>
<html>

<?php $error=""; //sets the error var to empty?>

<head></head>

<body>

<form name="form1" method="check_login.php" action="post">
    Username <input name="username" type="text" id = "username" placeholder="Username">
    <br><br>
    Password <input name="password" type="password" id = "password" placeholder="********">
    <br><br>
    <input name="Submit" type="submit" value="Log In">
    <br><br>
</form>

</body>

</html>

这是check_login.php

<?php

    //sets the host/username/password/database name into variables
    $host = "localhost";
    $user = "root";
    $pass = "enterpasshere";
    $myDB = "abc";
    $error = "";

    if (empty($_POST['username']) || empty($_POST['password'])) {
        $error = "Username or Password is invalid"; 
    } 
        else 
    {   
        $username = $_POST['username'];     //gets the username input
        $password = $_POST['password']; //gets the password input

        $connection = mysql_connect($host, $user, $pass); //connects to the database
        mysql_select_db($myDB); //selects the database

        $result = mysql_num_rows(mysql_query("SELECT * FROM user WHERE username='$username' AND password='$password'")); //performs the query and gets the number of rows

        if($result == 1){ //if the query was right
            header("location: home.php");//Redirecting to other page
        } else {
            $error = "Wrong username or password";
        }

        mysql_close(); //Make sure to close out the database connection
    }

?>

我曾经把表单的动作作为?php($ _SERVER [“PHP_SELF”]);?和login.php里面的check_login.php中的代码,但我有错误打印的问题,所以我想我会这样做。

1 个答案:

答案 0 :(得分:1)

您的行动和方法是倒退的。方法是post或get,action是控制器的路径

<form name="form1" method="check_login.php" action="post">

应该是:

<form name="form1" action="check_login.php" method="post">

您获得localhost/post?username=asd&password=asd&Submit=Log+In因为表单提交到post,并假设get,因为method无效,因此将表单值附加到查询字符串网址。