猿问

PHP 请求未验证密码是否正确,然后继续打开 HTML 按钮

我不知道在这里做什么。PHP post请求(request.php)只是在单击按钮(correct.html)后继续下一页,即使我没有在输入中键入任何内容。我希望它验证密码是否正确,如果是这样=继续更正.html,否则提醒用户。


PHP 代码:


<?php 

$pass = $_POST['password'];


if ($pass == "password")

{

   include "correct.html";

}


else

{

   echo "Password incorrect";

}

?>

代码:


<div class="input-container">

    <input class="input-field" type="password" placeholder="Your passphrase" /><br>

    <i class="fa fa-user icon"></i>

</div><br>


<div id=continue>

    <form action="request.php" method="post">

     <button class="button" name="password" value="password" style="vertical-align:middle"><span>Confirm</span></button>

    </form>

</div>


天涯尽头无女友
浏览 128回答 3
3回答

潇湘沐

您应该首先为输入类型密码或按钮添加名称属性值,如下所示<div id=continue>&nbsp; &nbsp; &nbsp; &nbsp; <form action="request.php" method="post">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <input class="input-field" type="password" name="password" placeholder="Your passphrase" />&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <button class="button" name="submit" value="submit" style="vertical-align:middle"><span>Confirm</span></button>&nbsp; &nbsp; &nbsp; &nbsp; </form></div>之后你的php代码应该是这样的&nbsp; &nbsp; <?php&nbsp; &nbsp; if(isset($_POST['submit']))&nbsp; &nbsp; {&nbsp; &nbsp; $pass = $_POST['password'];&nbsp; &nbsp; if ($pass == "password")&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;include "correct.html";&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp;echo "Password incorrect";&nbsp; &nbsp; }&nbsp; &nbsp; }?>

小怪兽爱吃肉

这是你必须具备的:form<div id=continue>&nbsp; &nbsp; <form action="request.php" method="post">&nbsp; &nbsp; &nbsp; &nbsp; <!-- Note that input and button are under the SAME <form> -->&nbsp; &nbsp; &nbsp; &nbsp; <input class="input-field" type="password" name="password" placeholder="Your passphrase" />&nbsp; &nbsp; &nbsp; &nbsp; <!-- Note `name` attributes of `button` and `input`&nbsp; -->&nbsp; &nbsp; &nbsp; &nbsp; <button class="button" name="button" value="submit" style="vertical-align:middle"><span>Confirm</span></button>&nbsp; &nbsp; </form></div>在服务器上:<?php// for debugging purposes, remove when you wantprint_r($_POST);// as 'password' is now NAME of INPUT, `$pass` stores the value from the INPUT$pass = $_POST['password'];if ($pass == "password"){&nbsp; &nbsp;include "correct.html";}else{&nbsp; &nbsp;echo "Password incorrect";}

慕尼黑的夜晚无繁华

您应该阅读有关 PHP 中的表单提交的信息。在此特定实例中,您的表单未作为按钮需要提交属性提交提交,即 .<button type="submit"此外,要调试 post 数据,可以使用 。var_dump($_POST); die();
随时随地看视频慕课网APP
我要回答