0

phpダイ関数の質問。die()を使用すると、すべてのページ要素がクリーンアップされます。エラーメッセージをエコーし​​、すべてのページをクリーンアップする方法はありません。die()を使用してコードを停止し、メッセージを呼び出すと、別のページにジャンプするように見えます。

これが私のコードです

    <?PHP
$message="";
if(isset($_POST['submit'])){

    $name=$_POST['name'];
    $password=$_POST['password'];

    //Field check
    if($name && $password){$message=$name . $password;}
    else{die($message="please enter name and password");}

    //Check name    
    if($name=="alex" && $password==123){$message="Welcome ". $name;}    
    else{$message="wrong user or password";}
    }
?>

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

1 に答える 1

3

スクリプトを上から下まで読む必要があります。これには、以外のものも含まれ<?php ?>ます。スクリプトを使用die()すると、その場で停止します。

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

出力します

<html>
  <p>something</p>

あなたの場合は

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

また、「=」ではなく「==」を使用して比較していることにも注意してください。

于 2012-12-06T02:40:20.437 に答える