Home > Mobile >  Why the program always returns $everything_Ok = true
Why the program always returns $everything_Ok = true

Time:09-22

<?php

My "if" clause doesn't work. Programm always returns $everything_OK = true; I would like the clause "if" to return a value $everything_OK = faslse but its never hapeppens. Why?
session_start();

if(isset($_POST['name']))
{

    $everything_OK = true;

    $name = $_POST['name'];
    $surname = $_POST['surname'];
    $date = $_POST['date'];

here is a condition that name user must be met and here is a problem, because this "if" always return $everythink_OK = true

if(strlen($name)<2 || strlen($name)>15 || is_numeric($name))
    {
        $everything_OK = false;
        $_SESSION['e_name'] = '<strong>Podaj prawdziwe imię!</strong>';
    }
    
    // if(strlen($surname)<2 || strlen($surname)>15 || is_numeric($surname))
    // {
    //     $wszystko_OK = false;
    //     $_SESSION['e_surname'] = '<strong>Podaj prawdziwe nazwisko!</strong>';
    // }

    // if($everything_OK = true)
    // {
    //     header("Location: profil.php");
    // } 
}
?>


<!DOCTYPE html>
<html lang="pl">
<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>Formularz</title>
</head>
<body>

<style>
    *{text-align: center;
    background-color: gray;
} 
.error
{
    color: red;
    margin-top: 5px;
    margin-bottom: -10px; 

}
/* .error2
{
    color: red;
    margin-top: 10px;
    margin-bottom: 10px; 

}  */
</style>

<form action='index.php' method='Post'>
    <strong>Podaj imię</stron><br>
    <input type='text' name='name'>
    
    <?php
        if(isset($_SESSION['e_name']))
        {
            echo '<div class="error">'. $_SESSION['e_name'].'</div>';
            unset($_SESSION['e_name']);
        }
    ?>
    
    <br><strong>Podaj nazwisko</stron><br>

    <!-- <?php
        if(isset($_SESSION['e_surname']))
        {
            echo '<div class="error2">'. $_SESSION['e_surname'].'</div>';
            unset($_SESSION['e_surname']);
        }
    ?> -->

    <input type='text' name='surname'>
    <br><strong>Podaj datę urodzenia</stron><br>
    <input type='date' name='date'>
    <br><br>
    <input type='submit' value='prześlij'>
    <br>

</form>

</body>
</html>

CodePudding user response:

Your using an assignment: if($everything_OK = true)

You need to use a comparison if($everything_OK == true)

Notice the two equal signs ==

CodePudding user response:

Your POST send the variable name... isset() return always true because is a empty string OR a string. In the 2 cases return true.

You need also to check if is not empty like this:

if(isset($_POST['name']) && $_POST['name'] != '') {...}

CodePudding user response:

You passing name as string value but checking for number

if(strlen($name)<2 || strlen($name)>15 || is_numeric($name))

it's FALSE and never goes through condition.


Can you specify conditions & input params that you use.

  •  Tags:  
  • php
  • Related