如何在新页面中打印或回显一些文本,如果 if 语句在 php 中为 true


How to print or echo some text in new page if the if statement is true in php

大家好 我是编码新手,现在我正在下面学习 PHP 和 html,我提交了代码,我想在新页面中打印 Hello World,如果给定的用户名和密码正确,但它在同一页面打印 任何人都可以帮助我 提前致谢

<!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>

您可以使用标头函数,它看起来像这样:

 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>

现在在login.php:-

<?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文件,您可以在其中放置PHP代码,并在html文件中,将表单标签更改为< form action="path of that php file" method="post">