PHP die() clean all page

前端 未结 1 1728
余生分开走
余生分开走 2021-01-27 05:54

A php die function question. when I use die(), it clean all page elements. Is any way to echo error message and not clean all page, It looks like jump to another page when I use

相关标签:
1条回答
  • 2021-01-27 06:23

    You should read your script for top to bottom, including anything outside of <?php ?>. When using die() your script stops then and there.

    <?php $a = "something"; ?>
    <html>
      <p><?php echo $a?></p>
      <?php die(); ?>
      <p>Never here</p>
    </html>
    

    Would output

    <html>
      <p>something</p>
    

    In your case do

    <?php
    if(isset($_POST['submit'])){
    
        $name=$_POST['name'];
        $password=$_POST['password'];
    
        //Field check
        if(!$name || !$password) {
           $message="please enter name and password");
    
        //Check name and password    
        } elseif ($name=="alex" && $password=="alex1") {
           $message="Welcome ". $name;
    
        } else {
           $message="Username or password incorrect"
        }
    ?>
    <html>
    <p>SIGN UP</p>
        <form action="testing.php" method="POST">
                <input type="text" name="name" placeholder="Enter Name" />
                <input type="password" name="password" placeholder="Enter Password"/>
                <input type="submit" name="submit" value="Sign up"/>
        </form>
        <div><?php echo $message?></div>
    </html>
    

    Also note that I'm using '==' to compare, not '='.

    0 讨论(0)
提交回复
热议问题