Form php takes different value than typed

Asked

Viewed 269 times

4

I have a PHP form. When I give Submit it should take the values coming for $_POST. But it turns out that the returned data comes only value 1.

I don’t 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’s PHP. I got 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;
    }
}
  • Another detail, can be the tags inputs without closing.

  • Tags <input> don’t need to close.

  • Could you show the PHP code? It doesn’t seem to be a problem in HTML.

  • Problem with the <form> Not closed is not: I tested in three browsers and all returned the data correctly. While testing I thought of a possibility: is there any script interfering?

  • No Ustavo, it is closed, just forgot to put here. : / Like this, I’m doing the CRUD on the same page. So I’m taking the form action to make such a change in the bank.

  • 1

    Now that you have posted the PHP code we found the error, a little too late for me, but the problem has been solved. The test closed or was no longer by Rafael and flpms, and because I never close tags that don’t need to be closed.

  • But thank you Gustavo. It was very helpful. :)

Show 2 more comments

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 if 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'];
  • Thank you very much Ccastro. Really, she was returning the true as you spoke. Gracias :)

  • just positivar there ;)

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']:''; 

That is, 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 inserting in the bank.

Browser other questions tagged

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