Jump to content

I can't get the right result


Ivan007
Go to solution Solved by Steveinid,

Recommended Posts

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>

 

Link to comment
Share on other sites

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 by DanRz
Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.