簡體   English   中英

php使用if和else if語句

[英]php using if and else if statement

我正在嘗試創建此頁面,以便在使用if和else if提交不同質量值(貓: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>

一些小錯誤,包括您的PHP標記和Submit輸入上的空格。 您還缺少您的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代碼放在第二個下面。 為我工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM