如果if语句在php中为true,如何在新页面中打印或回显某些文本
大家好,我是新的编码,现在我正在学习下面的php和html我提交了代码,我想打印在一个新的页面,如果给定的用户名和密码是正确的,但它是在同一页打印,任何人可以帮我提前谢谢如果if语句在php中为true,如何在新页面中打印或回显某些文本,php,html,Php,Html,大家好,我是新的编码,现在我正在学习下面的php和html我提交了代码,我想打印在一个新的页面,如果给定的用户名和密码是正确的,但它是在同一页打印,任何人可以帮我提前谢谢 <!doctype html> <html> <body> <form action="#" method="post"> <input type="text" name="username" placeholder
<!doctype html>
<html>
<body>
<form action="#" method="post">
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel">
</form>
<?php
$a = 123;
$b = 234;
$c = $_POST["username"];
$d = $_POST["password"];
$e = "Hello World!!";
$f = "Error 404 Page Not Found";
if(isset($_POST['submit'])){
if ($a == $c && $b == $d )
{
print "$e";
}
else
{
echo "$f";
}
}
?>
</body>
</html>
创建一个新的.php
文件,您可以将php
代码放在其中&在html
文件中,将表单标记更改为
您可以使用header函数,如下所示:
if ($a == $c && $b == $d )
{
header("location: newpage.php");
exit;
}
else
{
echo "$f";
}
或者确实如上所述,我将表单重定向到新页面首先,这将是
html
部分,在一个单独的部分中,如login.html
:-
<!doctype html>
<html>
<body>
<form action="login.php" method="post"><!-- if you will not give action then form will posted to the same page -->
<input type="text" name="username" placeholder="Enter The Username"><br>
<input type="password" name="password" placeholder="Enter Password"><br>
<input type="submit" name="submit" value="Login">
<input type="reset" value="Cancel"><!-- about this i cannot say anything -->
</form>
</body>
</html>
<?php
$original_user_name = 123; // take variable name that are self descriptive
$original_user_password = 234;
$form_username = $_POST["username"];
$form_password = $_POST["password"];
// $e = "Hello World!!"; no need of extra variable
// $f = "Error 404 Page Not Found"; no need of extra variable
if(isset($_POST['username']) && isset($_POST['password'])){ // check with POSTED values not with button value
if ($original_user_name == $form_username && $original_user_password == $form_password){
echo "hello World!";
}else{
echo "Error 404 Page Not Found!";
}
}else{
echo "please fill both user name and password!";
}
?>
注意:-两个文件必须在同一工作目录中。将php代码放在其他页面中,并在
中提供该页面的链接。您的
将表单发布到自身,因此使用
发布到其他页面。一些建议是,使用有意义的变量名。因此,不要使用$c
而是使用$username
和$password
而不是$d
等。好的,我会尝试一下,谢谢:)同时考虑使用更好的变量名!非常感谢,它奏效了:)真的。你不认为这一定是一个评论吗?根据问题,这就是答案。那么我为什么要评论它呢?你很快就会得到它。它已经在评论中得到了回答,但无论如何,添加另一个他可以使用的方法
——与评论的状态不完全相同。你很快就会理解这个网站,就像@Anant所说的那样