[英]PHP in bootstrap contact form does not work correctly, someone can figure out what I did wrong?
[英]Struggling with PHP, can't figure out what I did wrong
好吧,我可能会遗漏一些非常明显的东西,但是我无法终生弄清楚它是什么。
我遇到的问题是,当我使用$ _POST [“]时,用户输入到输入字段'username'和'password'中的信息不会传递
这是我使用的表格,非常基本:
<form method="post">
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot <a href="#">password?</a></span>
</div>
</form>
这是我编写的PHP,它在同一文档中用于测试目的,问题不在于缺少标头。 我也已经在单独的文件中对其进行了测试,但是没有区别。
<?php
$username = 'username';
$password = '123';
if ($username == 'username') {
echo 'username retrieved <br />';
}
if ($password == '123') {
echo 'password retrieved <br />';
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo ' method retrieved <br />';
if (isset($_POST['username']) && isset($_POST['password'])) {
$u_username = $_POST['username'];
echo $u_username . $username . '<br />';
$u_password = $_POST['password'];
echo $u_password . $password . '<br />';
if ($u_username === $username && $u_password === $password) {
echo 'username and password retrieved <br />';
} else {
echo 'username and/or password not retrieved <br />';
}
}
}
预先谢谢您,我问这个问题很愚蠢,但是我非常感谢您提供任何反馈!
您缺少action="login.php"
因为您的文件名在<form>
是如下所示的login.php:
<form method="post" action="login.php">
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="username" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="password" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot <a href="#">password?</a></span>
</div>
</form>
和PHP代码是这样的
$username = 'username';
$password = '123';
if ($username == 'username') {
echo 'username retrieved <br />';
}
if ($password == '123') {
echo 'password retrieved <br />';
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo ' method retrieved <br />';
if (isset($_POST['username']) && isset($_POST['password'])) {
$u_username = $_POST['username'];
echo $u_username . $username . '<br />';
$u_password = $_POST['password'];
echo $u_password . $password . '<br />';
if ($u_username == $username && $u_password == $password) {
echo 'username and password retrieved <br />';
} else {
echo 'username and/or password not retrieved <br />';
}
}
}
即使我认为前两个测试都没有用
我使用的是PHPStorm版本,该版本具有与POST使用相关的错误,这就是GET起作用的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.