php使用if和else-if语句


php using if and else if statement

我试图创建这个页面,当使用if和else if提交不同的质量值(猫的数量:3)时,它会给出不同的注释。但是,当我点击submit时,注释不会显示,我无法解决问题。

这是我的代码:

<?
if ($_POST['subBtn']) { 
$mass = $_POST['theMass'];
$colour = $_POST['theColour'];
$name = $_POST['theName'];
$comment = $_POST['theComment'];
echo "<p> The name of the cat is <b>" . $name . "</b>, the colour is  <b>" . $colour . "</b>, the mass is <b>" . $mass . "." . $comment . "</p>";}
?>
<?
if ($_POST['subBtn']) {
$mass = $_POST['theMass'];
if ($mass <= 0 AND NULL ) {
$comment = "INVALID";
} else if ($mass >= 0 AND $mass <=2.5) {
$comment = "Skin and bones!!!";
} else if ($mass > 2.5 AND $mass<=5) {
$comment = "Small but healthy";
} else if ($mass > 5 AND $mass<=10) {
$comment = "Getting a little heavy!";
} else if ($mass >10 AND $mass<=15) {
$comment = "You may wanna hide the food!";
} else if ($mass >15 AND $mass<=20) {
$comment = "Are you sure this is a cat?";
} else if ($mass >20) {
$comment = "Need another job too feed your cat! <p>'Comment: OMG'</p>";
} else {
$comment = "<p class='error'>Nothing specific to comment...sorry try again</p>";
}
}
?>
<p>     
<form action="crazy-cats.php" method="post">
Colour of the Cat:
<select name="theColour">
<option value="red">Red</option>
<option value="green">Green</option>
<option value="blue">Blue</option>
<option value="black">Black</option>
</select>  
Mass: <input type="text" name="theMass" value="" /><br />
    <br />
Name: <input type="text" name="theName" value="" /><br />
<input type="submit"name="subBtn" value="submit"/></input>
</form>
</p>

一些小错误,包括PHP标记和提交输入中缺少空格。您还缺少theComment输入。试试这个

<?php
if (isset($_POST['subBtn'])) {
    $mass = $_POST['theMass'];
    $colour = $_POST['theColour'];
    $name = $_POST['theName'];
    $comment = $_POST['theComment'];
    echo "<p> The name of the cat is <b>" . $name . "</b>, the colour is  <b>" . $colour . "</b>, the mass is <b>" . $mass . "." . $comment . "</p>";
    $mass = $_POST['theMass'];
    if ($mass <= 0 AND NULL ) {
        $comment = "INVALID";
    } else if ($mass >= 0 AND $mass <=2.5) {
        $comment = "Skin and bones!!!";
    } else if ($mass > 2.5 AND $mass<=5) {
        $comment = "Small but healthy";
    } else if ($mass > 5 AND $mass<=10) {
        $comment = "Getting a little heavy!";
    } else if ($mass >10 AND $mass<=15) {
        $comment = "You may wanna hide the food!";
    } else if ($mass >15 AND $mass<=20) {
        $comment = "Are you sure this is a cat?";
    } else if ($mass >20) {
        $comment = "Need another job too feed your cat! <p>'Comment: OMG'</p>";
    } else {
        $comment = "<p class='error'>Nothing specific to comment...sorry try again</p>";
    }
echo "my comment on mass is ". $comment;
}
?>
<p>
<form action="test.php" method="post">
    Colour of the Cat:
    <select name="theColour">
        <option value="red">Red</option>
        <option value="green">Green</option>
        <option value="blue">Blue</option>
        <option value="black">Black</option>
    </select>
    Mass: <input type="text" name="theMass" value="" /><br />
    <br />
    Name: <input type="text" name="theName" value="" /><br />
    <input type="hidden" name="theComment" value="<?= $comment ?>" />
    <input type="submit" name="subBtn" value="submit"/></input>
</form>
</p>

当然,您应该真正考虑验证POST变量。

  1. 使用isset检查$_POST
  2. 删除$_POST['theComment']行
  3. 把你的第一个PHP代码放在第二个下面。为我工作