Questions to send data via POST

Asked

Viewed 869 times

2

Hello, I’m trying to send over $_POST some data for validation and later insertion in the database, what I could do in another project exactly the way I’m trying to do now, but I’m not getting it.

This is my form code:

<div class="container-fluid">
<div class="panel panel-primary">
    <div class="panel-heading">
        CADASTRAR USUÁRIO
    </div><br>

    <div id="filtros" style="padding-left: 20px">
        <a id="voltar_pagina" href="menu.php" class="btn btn-primary" title="Voltar ao início">
            <span class="glyphicon glyphicon-home"></span>
        </a>
        <a id="voltar_lista" href="javascript:history.back()" class="btn btn-primary" title="Voltar">
            <span class="glyphicon glyphicon-chevron-left"></span>
        </a>                        
    </div>

    <div class="panel-body">

        <form id="form_cad_usuario" data-toggle="validator" class="form-horizontal" method="POST" action="../config/usuario/processa_cad_usuario.php">

            <!--DADOS DO USUÁRIO-->
            <div class="page-header">
                <h4>Dados do Usuário</h4s>
                <hr>
            </div>


            <div class="form-group">                 
                <label for="ativo" class="col-sm-2 control-label">Ativo</label>
                <div class="col-sm-10">
                    <div class="checkbox">
                        <label>
                            <input id="ativo" type="checkbox">
                        </label>
                    </div>
                </div>
            </div>

            <div class="form-group">
                <label for="nome_usuario" class="col-sm-2 control-label">Nome</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="nome_usuario" placeholder="Nome" data-error="Por favor, informe o nome completo para o usuário." required>
                    <div class="help-block with-errors"></div>
                </div>
            </div>

            <!--DADOS DE ACESSO-->
            <div class="page-header">
                <h4>Dados de Acesso</h4>
                <hr>
            </div>

            <div class="form-group">
                <label for="login_usuario" class="col-sm-2 control-label">Usuário</label>
                <div class="col-sm-10">
                    <input type="text" class="form-control" id="login_usuario" placeholder="Usuário" data-error="Por favor, digite um nome para o usuário." required>
                    <div class="help-block with-errors"></div>
                </div>
            </div>

            <div class="form-group">
                <label for="senha_usuario" class="col-sm-2 control-label">Senha</label>
                <div class="col-sm-10">
                    <input type="password" class="form-control" id="senha_usuario" placeholder="Senha" data-error="Por favor, digite um senha para o usuário." required>
                    <div class="help-block with-errors"></div>
                </div>
            </div>

            <!--DADOS DA  REGIONAL-->
            <div class="page-header">
                <h4>Regionais Permitidas</h4>
                <hr>
            </div>

            <?php
            $resultado = lista_regionais();

            while ($registro = mysqli_fetch_assoc($resultado)) 
            {
                $id_regional = $registro['id'];
                $nome_regional = $registro['nome'];

            ?>

            <div class="col-sm-2">
                <div class="form-group">
                    <div class="col-sm-1">
                        <div class="checkbox">
                            <label>
                                <label class="col-md-10" for="<?php echo"regional_$id_regional" ?>">
                                    <input type="checkbox" name="<?php echo"regionais[$id_regional]"; ?>" id="<?php echo"regional_$id_regional" ?>"> 
                                    <small><?php echo $nome_regional; ?></small>
                                </label>
                            </label>
                        </div>
                    </div>
                </div>
            </div>

            <?php
            }
            ?>

            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <button id="botao_salvar" type="submit" class="btn btn-primary pull-right">Salvar <span class="glyphicon glyphicon-floppy-save"></span></button>
                </div>
            </div>

        </form>
    </div>
</div>

Here is the script 'processa_cad_usuario.php':

<?php
session_start();

include_once ('../../dao/usuario_dao.php');

$_SESSION['cadastro_inserido'] = null;

var_dump($_POST['nome_usuario']);

//cadastra_usuario($nome, $login, $senha, $permissao, $flag_ativo, $criado, $modificado);

When trying to access, for example, $_POST['nome_usuario'] php tells me that such an index is not set and when doing the var_dump() return me Null. Any suggestions?

Additionally, I’d like to clear up an extra question. At the end of the form I have some checkboxes with city names that are generated dynamically and I would like to know how to pass all that are selected to the page that processes the registration and how I should insert them into the database and what structure to use. This bearing in mind that at some point I will need them to give the user permission to access data from the cities for which he has permission. Thank you so much to those who can help. I embrace you all.

3 answers

1

In inputs inside the form instead of putting "id" put "name". For example:

 <input type="text" class="form-control" name="login_usuario" placeholder="Usuário" data-error="Por favor, digite um nome para o usuário." required>
  • Thank you for coming back, Daniel. That’s right, I thought when giving Ubmit the values of the fields would be searched through id. When I select the checkbox and give a var_dump() returns me a String: "On", I would like an orientation of the best way to deal with the checkbox of cities to save in the user’s table and then give the user viewing permissions based on those cities. Thank you in advance.

  • You can name the checkbox as an array and then you can pick up the selected values with a foreach. In this question you are explaining well, take a look: http://answall.com/questions/5675/howto checkse-um-checkbox-est%C3%A1-checked-with-php

0

PHP is correct to say that there is no index 'username'. Because when working with forms and php, when using both the HTTP request method: $_POST like the: $_GET, the information is sent by the name you assigned to your form field. Both $_POST and $_GET is an associative array with all keys and information values sent.

It is interesting to use both the id property and the name property, since php by default uses name, while other languages require id to handle elements such as CSS and Javascript.

So for proper operation and good use it is recommended that your input look similar to:

<input type="text" class="form-control" id="nome_usuario" name="nome_usuario" placeholder="Nome" data-error="Por favor, informe o nome completo para o usuário." required>

0

Just add the "name" attribute to your inputs with the same id value:

<input type="text" class="form-control" id="nome_usuario" name="nome_usuario" ... >

The id in this case, is used to link the field with the label and perhaps for javascript validations, but it is not sent to the server. The correct is to always use the attribute "name" to pick up data on the server with POST.

  • OK, this part was clear to me. As for the question of sending via POST the selected cities any suggestions? Thank you for responding.

Browser other questions tagged

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