Ivan007 Posted November 20, 2022 Share Posted November 20, 2022 Quote <?php //It's a simple program to valid age but it only print 'you can vote' despite any value I input. $age=(isset($_POST['age'])); if(isset($_POST['age'])){ if($age >=18){ echo "you can vote!!!"; }elseif($age < 18){ echo "You are not allowed to vote"; }else{ echo "enter a valid age"; } ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Document</title> </head> <body> <form method="post"> <input type="text" name="age"> <input type="submit"> </form> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/315558-i-cant-get-the-right-result/ Share on other sites More sharing options...
DanRz Posted November 20, 2022 Share Posted November 20, 2022 (edited) Try this... <?php //It's a simple program to valid age but it only print 'you can vote' despite any value I input. if($_POST['age']){ $age = int $_POST['age']; if($age >= 18){ echo "you can vote!!!"; }elseif($age < 18){ echo "You are not allowed to vote"; }else{ echo "enter a valid age"; } } ?> Also on your form use this instead... it means the user can only put a number in... <input type="number" name="age"> Edited November 20, 2022 by DanRz Quote Link to comment https://forums.phpfreaks.com/topic/315558-i-cant-get-the-right-result/#findComment-1602755 Share on other sites More sharing options...
Solution Steveinid Posted November 20, 2022 Solution Share Posted November 20, 2022 Give this a try for your code: <?php if (!isset($_POST['age'])) { echo 'enter a valid age'; } else { $age = $_POST['age']; if($age >=18) { echo "you can vote!!!"; } else { echo "You are not allowed to vote"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/315558-i-cant-get-the-right-result/#findComment-1602805 Share on other sites More sharing options...
ginerjm Posted November 21, 2022 Share Posted November 21, 2022 Since posted values, excluding radio and checkboxes, are always set, the test for isset doesn't really do anything. You have to validate the input value by checking the value. Quote Link to comment https://forums.phpfreaks.com/topic/315558-i-cant-get-the-right-result/#findComment-1602814 Share on other sites More sharing options...
Steveinid Posted November 21, 2022 Share Posted November 21, 2022 3 hours ago, ginerjm said: Since posted values, excluding radio and checkboxes, are always set, the test for isset doesn't really do anything. You have to validate the input value by checking the value. When you initially arrive to the page there is nothing set so it tells the visitor to 'Enter a valid age'. Quote Link to comment https://forums.phpfreaks.com/topic/315558-i-cant-get-the-right-result/#findComment-1602815 Share on other sites More sharing options...
ginerjm Posted November 21, 2022 Share Posted November 21, 2022 On that first page visit all you have to do is see if there is ANY post values and avoid the validation code.. Simply if ($_SERVER['REQUEST_METHOD'] <> 'POST') { (displayypage) (exit) } Quote Link to comment https://forums.phpfreaks.com/topic/315558-i-cant-get-the-right-result/#findComment-1602823 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.