Input text returning bool(false)

Asked

Viewed 370 times

3

I have this HTML code

<!DOCTYPE html>

<HTML> 
    <HEAD>
     <TITLE>Cadastro</TITLE>
     <meta charset="UTF-8">
     <link rel="stylesheet" type="text/css" href="css/bootstrap.css">
    </HEAD>   
    <BODY>
        <div class="container">
            <form method="POST" class="form-horizontal" action="teste.php">
                <div class="form-group">
                    <label for="pesNome" class="col-sm-2 control-label">Nome</label>
                    <div class="col-sm-10">
                        <input type="text" class="form-control" id="pesNome" placeholder="Nome">
                    </div>
                </div>                
                <div class="form-group">
                    <div class="col-sm-offset-2 col-sm-10">
                        <button type="submit" class="btn btn-default">Gravar</button>
                    </div>
                </div>
            </form>
        </div>
    </BODY>
</HTML>

And this PHP code

<!DOCTYPE HTML>
<html>

<link rel="stylesheet" type="text/css" href="css/bootstrap.css">
<?php
        $nome=isset($_POST['pesNome']);
        var_dump($nome);
?>
<a href="index.php">voltar</a>
</html>

My intention is that the var_dump return what was typed in the input pesName, but instead returns me the message bool(false). Can anyone explain to me why ?

  • 3

    I know. You’re doing $nome=isset($_POST['pesNome']);, then the value of $nome will be the return of isset, that checks if a certain value exists. And remember to define the attribute name in the form fields.

  • 1

    The input <input type="text" class="form-control" name="pesNome" id="pesNome" placeholder="Nome"> return value is by input name $nome=$_POST['pesNome'];

  • Thanks, the problem was actually the name attribute. The isset I had actually put as a test after I played the post and returned me an error that there was no such thing. I thought the post caught the name in the id. Valeu!!!

1 answer

3

The isset is a value boolean. Soon you are assigning to the variable $nome a boolean value (true or false).

The correct is to assign the $_POST value to the variable:

$nome = $_POST['pesNome'];

And then check if the variable was fed:

if( isset($nome) ){
    // $nome tem algum valor
}else{
    // $nome não tem valor algum
}
  • Actually you need to check the isset before the assignment, because depending on the server settings, this will result in error when the value does not exist.

  • @Andersoncarloswoss I confess that I have never seen an example of this. It is usually seen in this order. If it depends on the server settings, it would be a peculiar case.

  • Worse than not particular, no. And just analyze by logic: assign a value that may not even exist? This makes no sense. Some do so, wrongly, by the false sense of security generated by the default settings of most servers to supply any error message or alert. The ideal is still to use the filter_input and check that the result is not false: $nome = filter_input(INPUT_POST, 'pesNome')

  • @Andersoncarloswoss Ahhh... if it doesn’t exist it eh empty rs.. gives anyway. I would like an example for me to test here on the server. If you pass I delete the answer rs. But it is interesting your observation. I did not know.

  • @Andersoncarloswoss but I think I’ve seen it $nome = filter_input(INPUT_POST, 'pesNome') somewhere

Browser other questions tagged

You are not signed in. Login or sign up in order to post.