按“输入”键时,按钮会执行错误的代码。按键

时间:2017-04-22 22:54:08

标签: php html forms post html-form

我正在编写用于登录用户的代码。

登录和搜索工作之前,但是,自从我将两者合并后,登录表单并没有实现它自己的代码,而是运行搜索代码

编辑:我发现了为什么会发生这种情况,因为我一直在按Enter而不是选择登录。所以现在我想知道,如何按Enter键并实现登录代码。

以下是headerPublic.php,其中包含搜索代码

<?php
//---------------------BEGIN SEARCH FROM THE SEARCH BAR IN PUBLIC HEADER----------------------------------


    if(isset($_POST["search_button"]))
        {                   

             //PHP SEARCH CODE

        }


//---------------------END SEARCH FROM THE SEARCH BAR IN PUBLIC HEADER----------------------------------
?>

<html>
    <head>
        <title>VCR Exchange</title>
    </head>

    <body>
            <nav>

                <ul>
                    <li><a href="index.php">Home</a></li>
                    <li><a href="about.php">About</a></li>


<!------------------------------------SEARCH BAR---------------------------------------------->
                    <li>    
                    <form role="search" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
                      <div class="form-group">
                        <input type="search" name="search">
                      </div>
                      <button type="submit" value = "search" name="search_button">Search</button>

                </li>
<!------------------------------------SEARCH BAR---------------------------------------------->

                    <li><a href="register.php">Register</a></li>
                    <li><a href="login.php">Log In</a></li>
                </ul>

            </nav>

这是login.php。

<?php require('connect.php'); ?>
<?php require('headerPublic.php'); ?>
<?php require('session.php'); ?>

         <form class="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">

            <label for="inputEmail" >Email address</label>
            <input type="email" name="email" placeholder="Email address">

            <label for="inputPassword" >Password</label>
            <input type="password" name="password" placeholder="Password">

            <button name="login" type="submit">Login</button>

         </form>

    </body>
</html>


<?php

    // IF LOGIN BUTTON IS CLICKED:
    if (isset($_POST['login']))
    {

        //LOG IN CODE

    }

?>

1 个答案:

答案 0 :(得分:0)

搜索表单缺少结束表单标记

 <!------------------------------------SEARCH BAR---------------------------------------------->
                        <li>    
                        <form role="search" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
                          <div class="form-group">
                            <input type="search" name="search">
                          </div>
                          <button type="submit" value = "search" name="search_button">Search</button>
                        </form>
                    </li>
    <!------------------------------------SEARCH BAR---------------------------------------------->
相关问题