2016-04-12 2 views
0

Я пытаюсь создать эту страницу, которая дает разные комментарии, когда различные значения массы (из кошек: 3) передаются с использованием if и else if. Однако комментарий не появляется, когда я попал в submit, и я не мог понять проблему.php using if и else if statement

Это мой код:

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

ваш отсутствует 'эхо $ комментарий;' ' –

+0

elseif' все одно слово – Derek

+0

@OliverQueen его в силе, как 2 http://php.net/manual/en/control-structures.elseif.php –

ответ

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.

+0

Что же касается недостающей массы '$ comment'? –

+0

Я добавил комментарий в –

+0

ваше смешение комментария пользователя и комментарий OP –

-1
  1. Проверьте $ _POST с isset.
  2. Удалить строку $ _POST ['theComment'].
  3. Поместите свой первый PHP-код ниже второго. Работал для меня!
+0

не оставлять комментарии как ответы –

+0

Извините, я не могу оставлять комментарии. Кстати, что-то не так с ответом? Я просто попробовал, и он работает. – Roman

+0

1. он делает это, 2. почему? 3. Ни в коем случае это не сработало –