php使用if和else if语句

时间:2016-04-12 22:37:48

标签: php select

我正在尝试创建此页面,当使用if和else if提交不同的质量(cat:3)质量时,会给出不同的注释。但是当我点击提交时,评论没有出现,我无法弄清楚问题。

这是我的代码:

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

2 个答案:

答案 0 :(得分:0)

一些小问题,包括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 :(得分:-1)

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