【问题标题】:When button is submit, PHP doesn't know当按钮提交时,PHP 不知道
【发布时间】:2014-11-10 03:43:51
【问题描述】:
    <html>
 <div class="panel-body">
                        <form method="post" action="index.php">
                            <div class="form-group">
                                <input class="form-control" placeholder="username" name="username" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="password" name="password" type="password" value="">
                            </div>
                            <input type='submit' name="submit" value='Login'>
                        </form>
                        <?php
                        if(isset($_POST['submit']))
                        {    
                            $username = sanitize($_POST['username']);
                            $password = sanitize($_POST['password']);
                            if($username && $password)
                            {
                                $query = mysql_query("SELECT Name, Password FROM users WHERE Name = '$username' LIMIT 1");
                                if(mysql_num_rows($query) == 1)
                                {
                                    while($row = mysql_fetch_assoc($query))
                                    {
                                        $dbusername = $row['Name'];
                                        $dbpassword = $row['Password'];
                                    }
                                    $somename = hash( 'whirlpool', $password);
                                    $somename = strtoupper($somename);
                                    if($username == $dbusername && $somename == $dbpassword)
                                    {
                                        $_SESSION['username'] = $dbusername;
                                        header('location: /pcp/home.php');
                                    }
                                    else $error = "Wrong password!";
                                }
                                else $error =  "Username doesn't exist!";
                            }
                            else $error = "Type name and password!";
                        }
                        ?>
                    </div>
</html>

当我使用正确的密码和用户提交按钮时,它不会进入 home.php,但当我重新加载时,它会进入。

它正在使用引导程序,这是原因吗?如果是这样,你能帮我解决它吗?

【问题讨论】:

  • 您需要在输出页面之前完成所有代码工作。你的 header() 不能像这样在 html 中出现。

标签: php redirect button submit


【解决方案1】:

如果你想将你的php代码和表单元素一起添加,你需要编写

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>" >

或者不。只需将表单页面和 php 页面分开即可。 这意味着

index.php

  <html>
 <div class="panel-body">
                        <form method="post" action="check.php">
                            <div class="form-group">
                                <input class="form-control" placeholder="username" name="username" type="text" autofocus>
                            </div>
                            <div class="form-group">
                                <input class="form-control" placeholder="password" name="password" type="password" value="">
                            </div>
                            <input type='submit' name="submit" value='Login'>
                        </form>

                    </div>
</html>

ckeck.php

               <?php
                if(isset($_POST['submit']))
                {    
                    $username = sanitize($_POST['username']);
                    $password = sanitize($_POST['password']);
                    if($username && $password)
                    {
                        $query = mysql_query("SELECT Name, Password FROM users WHERE Name = '$username' LIMIT 1");
                        if(mysql_num_rows($query) == 1)
                        {
                            while($row = mysql_fetch_assoc($query))
                            {
                                $dbusername = $row['Name'];
                                $dbpassword = $row['Password'];
                            }
                            $somename = hash( 'whirlpool', $password);
                            $somename = strtoupper($somename);
                            if($username == $dbusername && $somename == $dbpassword)
                            {
                                $_SESSION['username'] = $dbusername;
                                header('location: /pcp/home.php');
                            }
                            else $error = "Wrong password!";
                        }
                        else $error =  "Username doesn't exist!";
                    }
                    else $error = "Type name and password!";
                }
                ?>

【讨论】:

  • 非常感谢,它对我帮助很大。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-17
  • 2011-05-06
  • 2015-08-07
相关资源
最近更新 更多