Form php receives value other than typed

4

I have a form in PHP. When I submit it, I should get the values coming from $ _POST. But it turns out that the returned data comes only with value 1.

I do not know what's going on. Everything seems normal. :

<form method="POST" action="">
    <input type="hidden" name="action" value="cadastrar_studante">
    <div class="row mt20">
        <div class="col-xs-6">
            <div class="form-group">
                <label class="control-label" for="name-student-form">Nome</label>
                <input type="text" class="form-control" name="name-student-form" id="name-student-form">
            </div>
        </div>
        <div class="col-xs-6">
            <div class="form-group">
                <label class="control-label" for="email-student-form">Email</label>
                <input type="email" class="form-control" name="email-student-form" id="email-student-form">
            </div>
        </div>
    </div>

    <div class="row">
        <div class="col-xs-12">
            <button type="submit" class="btn btn-primary pull-right">Cadastrar</button>
        </div>
    </div>
</form>

Here is PHP. I get the data for $ _POST. I'm using PHP PDO.

if( in_array("cadastrar_studante", $_POST) ) {
    $name_student = isset( $_POST['name-student-form'] );
    $email_student = isset( $_POST['email-student-form'] );

    //echo $name_student . " <--> " . $email_student;

    $students->set_name_student( $name_student );
    $students->set_email_student( $email_student );

    /** Inserir aluno */
    if( $students->insert() ) {
        $echo = <<<MSG
<script type="text/javascript">
    jQuery(document).ready(function($)) {
        $("#modal-feedback").find(".modal-body").html("Aluno cadastrado com sucesso.");
        $("#modal-feedback").modal("show");
    }
</script>
        MSG;
        echo $echo;
    }
}
    
asked by anonymous 23.02.2014 / 02:17

2 answers

6

Friend, the error is here.

$name_student = isset( $_POST['name-student-form'] );
$email_student = isset( $_POST['email-student-form'] );

The function isset() only returns whether the variable is set or not, so it always returns 1, which is true .

The right thing is

$name_student = $_POST['name-student-form'];
$email_student = $_POST['email-student-form'];
    
23.02.2014 / 02:53
5

You can do it like this!

$name_student  = isset($_POST['name-student-form'])? $_POST['name-student-form'] :'';  
$email_student = isset($_POST['email-student-form'])?$_POST['email-student-form']:''; 

In other words, if the index does not exist then the variable will receive a default value that can be empty or null. I recommend that you do a validation before entering the bank.

    
23.02.2014 / 15:17