Wickham Posted January 22, 2009 Report Share Posted January 22, 2009 I've got a form where people will be able to enter a vote from 0 to 10 inclusive. So far I've dealt with the empty box (rejected), entries more than 10 (rejected) and entries 1 to 10 (accepted) but 0 gets treated as an empty box because I can see the echo statement it produces. How do I get 0 in the form treated as a true number? Here's a snippet if(empty($_POST['rating'])) {echo " Rating must be between 0 and 10. Use your back button to try again."; }elseif($rating > 10) {echo " Rating must be less than 10. Use your back button to try again."; }elseif($rating {echo " You rated {$item} as {$rating} out of 10. Thank You. Use your back button to vote for another song.";}elseif($rating = 0) {echo " You rated {$item} as {$rating} 0 out of 10. Thank You. Use your back button to vote for another song.";}} else { echo " Please submit form.";......................... The last one elseif($rating = 0) doesn't work nor does elseif($rating == 0) The form works well except for rejecting 0. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 22, 2009 Report Share Posted January 22, 2009 You need to change this line: elseif($rating = 0) to elseif($rating == 0) The double equals (==) checks to see if it is equal to a value, while a single equals (=) sets the value. For example, look at the different between: $i = 3; if ($i == 3) Quote Link to comment Share on other sites More sharing options...
Wickham Posted January 22, 2009 Author Report Share Posted January 22, 2009 (edited) OK, It works on its own, I had it in the wrong order before. However, it's now confused with the Empty situation. If I have the 0 statement first:- $rating = $_POST['rating']; $item = $_POST['item']; if($rating == 0) {echo " You rated {$item} as 0 out of 10. Thank You. Use your back button to vote for another song.";}elseif(empty($_POST['rating'])) {echo " Rating must be between 0 and 10 inclusive. Use your back button to try again."; }elseif($rating > 10) {echo " Rating must be equal to or less than 10. Use your back button to try again."; }elseif($rating {echo " You rated {$item} as {$rating} out of 10. Thank You. Use your back button to vote for another song.";}} else .......... Someone entering 0 gets the "0 out of ten" (correct) but someone submitting an empty input box also gets "0 out of 10 Thank You" instead of Try Again. If I put the Empty situation first:- if(empty($_POST['rating'])) {echo " Rating must be between 0 and 10 inclusive. Use your back button to try again."; }elseif($rating == 0) {echo " You rated {$item} as 0 out of 10. Thank You. Use your back button to vote for another song.";} then someone entering an empty box gets Try Again but someone entering 0 also gets Try Again, so the first conditional statement sets the value for the next or to put it another way, once the 0 is a number the Empty statement also treats an empty box as a number 0. > PHP test This is testing WampServer2. if(isset($_POST['sendit'])) { $rating = $_POST['rating']; $item = $_POST['item']; if(empty($_POST['rating'])) {echo " Rating must be between 0 and 10 inclusive. Use your back button to try again."; } elseif($rating == 0) {echo " You rated {$item} as 0 out of 10. Thank You. Use your back button to vote for another song.";} elseif($rating > 10) {echo " Rating must be equal to or less than 10. Use your back button to try again."; } elseif($rating {echo " You rated {$item} as {$rating} out of 10. Thank You. Use your back button to vote for another song.";} } else { echo " Please submit form."; ?> </pre> <form action="test-form-conditional-statementa.php" method="post"> Song 1 Song 2 Rating (out of 10 please): </form> <br>}<br>?><br><p>Other page content here.</p> <br><br><br Edited January 22, 2009 by Wickham Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 22, 2009 Report Share Posted January 22, 2009 Could you bypass this by only allowing people to vote 1 through 10, and 0 would represent no vote? Quote Link to comment Share on other sites More sharing options...
Wickham Posted January 22, 2009 Author Report Share Posted January 22, 2009 Yes I could, but I'm trying to learn. I realise that 0 is treated as false in Boolean so your code gets one part of the code working to make it true but for some reason the Empty statement and the 0 statement are linked and give the same result so depending on the order, one of them is wrong. Somewhere I need to convert a true to false or vice versa. Quote Link to comment Share on other sites More sharing options...
falkencreative Posted January 22, 2009 Report Share Posted January 22, 2009 OK, got it... Instead of checking the value, your first if statement should check the string length: if(strlen($rating) == 0) Quote Link to comment Share on other sites More sharing options...
lwsimon Posted January 22, 2009 Report Share Posted January 22, 2009 the == comparison operator does type coersion. When you say "if ( $var == 0 )", you're really saying "If you typecast 0 to the type of $var, are they equivalent?" The === operator, on the other hand, tests both value and type. I'm not sure if I"m being clear - its been a long day at work - so here are some examples $var = 0; if( $var == 0 ){ ... } //this executes if( $var == false ){ ... } //this executes if( $var === 0 ){ ... } //this executes if( $var === false ){ ... } //this does not execute PHP manual entry for comparison operators: http://us2.php.net/operators.comparison Quote Link to comment Share on other sites More sharing options...
Wickham Posted January 23, 2009 Author Report Share Posted January 23, 2009 (edited) With the code I've got in post 3, with the Empty statement first, using elseif($rating == 0) for the second statement the 0 input is treated like an empty box and echos Rating must be between 0 and 10 inclusive. Use your back button to try again. which is the incorrect text for 0 input but it's correct for Empty box input. Edit: my guess is that the 0 statement would treat 0 as a number (not false) but the processor gets to the Empty statement first where it treats 0 as Empty (Empty is true - there is no input) and echos the Empty text "Try Again" and stops and never reaches the 0 statement. It's the same using if(strlen($rating) == 0) If I reverse the order and put the Empty statement second using if($rating == 0) for the 0 statement first I get You rated Song 1 as 0 out of 10. Thank You. Use your back button to vote for another song. for both a 0 input (correct) and an empty box input (wrong - it must now be treating an empty box as Empty - there is an input of 0 which is now a number therefore Empty is false). If i use if(strlen($rating) == 0) for the first 0 statement both empty and 0 inputs wrong, they have echo text swapped. I haven't tried lwsimon's solution yet. Edited January 23, 2009 by Wickham Quote Link to comment Share on other sites More sharing options...
Wickham Posted January 23, 2009 Author Report Share Posted January 23, 2009 I've found the answer. The problem as I said was that when someone enters 0 it's treated like Empty so all my different attempts made Empty the same text as 0, except one where the Empty and 0 produced different echo text, but swapped. I could have swapped the text but that would have been cheating. It was using elseif(strlen($rating) == 0) but 0 is false so I realised that if I made that elseif(strlen($rating) == 1) it would make it true, even though it's checking whether 0 is in the string. It can also be elseif(strlen($rating) == true). I also decided to change the order so that the more correct options were first and the Empty statement was last if there were no numbers and 0 was not a string 0. I also changed =1 (equal to or over 1) - after numbers over 10 have been rejected. > PHP test This is testing WampServer2. if(isset($_POST['sendit'])) { $rating = $_POST['rating']; $item = $_POST['item']; if($rating > 10) {echo " Rating must be equal to or less than 10. Use your back button to try again."; } elseif($rating >= 1) {echo " You rated {$item} as {$rating} out of 10. Thank You. Use your back button to vote for another song.";} elseif(strlen($rating) == true) //ie true not false 0 or elseif(strlen($rating) == 1) {echo " You rated {$item} as 0 out of 10. Thank You. Use your back button to vote for another song.";} elseif(empty($_POST['rating'])) {echo " Nothing entered. Rating must be between 0 and 10 inclusive. Use your back button to try again."; } } else { echo " Please submit form."; ?> </pre> <form action="test-form-conditional-statementf2.php" method="post"> Song A Song B Rating (out of 10 please): </form> <br>}<br>?><br><p>Other page content here.</p> <br><br><br><br Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.