Array result returns null

0
  • I need to start from the option selected in <select> , I check with the bank what's related to this option. I made it from as follows, but in the $p_id = $result['p_id']; part, there is null return, I already used this array association code other times and it worked.

  • I also need this to be hidden until the moment of choice of <option> , after displaying what I did, with the records. I do not know the which is wrong, but I accept suggestions for solution and thank you right away.

                      <div class="form-group">
                        <div class="col-sm-3">
                          <label for="exampleInputEmail1">Cliente</label>
                          <select class="form-control m-b-10" required="required" name="e_id" charset="utf-8">
                            <option>Selecione o cliente</option>
                              <?php
                                $select  = "SELECT e_id, nome FROM pessoas"; 
                                $result  = mysqli_query($conexao, $select);
                                while($exibe = mysqli_fetch_assoc($result)){
                                  $e_id = $exibe['e_id'];
                                  echo '<option  charset="utf-8" value = '. $e_id . '>' . $exibe['nome'] . '</option>';
                                }                                    
                                mysqli_free_result($result); 
                              ?>                     
                          </select> 
    
                        </div>                                  
                      </div>
                      </form>
                      <!--<button name="salvar" class="btn btn-success" onclick="showElement();">Pesquisar</button>-->
                      <div id="dados" class="form-group" onload="hideElement();">                                                       
                            <?php
                            if ($e_id <> '') {
                              $select  = "SELECT p_id FROM itens WHERE e_id = '$e_id'";  
                              $result  = mysqli_fetch_assoc(mysqli_query($conexao, $select));
                              $p_id  = $result['p_id'];
                              $select2 = "SELECT descricao where p_id = '$p_id'";
                              $resulta = mysqli_query($conexao, $select2);          
                              if (mysqli_num_rows($resulta) > 0){                            
                              echo '<table class="table table-hover" >
                                  <thead>
                                    <tr>
                                      <th>Descrição</th>
                                  </tr>
                              </thead>
                              <tbody>
                                <tr>';
                                while($exibe = mysqli_fetch_assoc($resulta)){ 
                                    $p_id  = $exibe['p_id'];
                                    echo '<tr>';  
                                      echo '<td value = "">' . $exibe['descricao']      . '</td>';                   
                              }
                            }else{
                              echo '<label for="exampleInputPassword1">Não há registros</label>'; 
                            } 
                          }else{
                            echo '<label for="exampleInputPassword1">Não há registros</label>';
                          }             
                          mysqli_free_result($result);  
                          mysqli_free_result($resulta);                                                                              
                            ?>                                     
                      </tr>
                  </tbody>
              </table>
    
asked by anonymous 19.04.2017 / 02:39

1 answer

0
  

I need to start from the selected option in the select, I see in the database what is related to this option.

You can not use PHP on client-side (@AndersonCarlosWoss).

For this, one can :

  • Make a second page show the description according to the id passed by the get method of the form;
  • Submit the form in event change of <select> with an AJAX request
  • ;
  • Show the request data (contents of the second page) in a div ;

Understanding this, let's go to the code:

index.php

<!DOCTYPE html>
<html>
    <head>
        <title></title>
        <script src="https://code.jquery.com/jquery-3.2.1.min.js"type="text/javascript"></script>

        <script type="text/javascript">

            $(document).ready(function(){
                $('#slcClientes').on('change', function() {
                    var dados = jQuery( '#fmrClientes' ).serialize();
                    $.ajax({
                        type: "POST",
                        url: $('#fmrClientes').attr('action'),
                        data: dados,
                        success: function(data) {
                            $('#dvAlvo').html(data);
                        }
                    });
                });
            });

        </script>

    </head>
    <body>

        <form id="fmrClientes" action="get_obs.php" method="post">
            <select id="slcClientes" name="cl_id">
                <option selected="selected" disabled="disabled">Selecione:</option>
            <?php

                $conexao = mysqli_connect("host", "usuário", "senha", "banco de dados"); // CONFIGURE!!!
                $res = mysqli_query( $conexao, "SELECT 'ClID', 'ClNome' FROM 'clientes' LIMIT 0,10;"); // LIMITEI PARA 10 REGISTROS - SOMENTE PARA FINS DE TESTE
                if(mysqli_num_rows($res) > 0)
                {
                    while( $cl = mysqli_fetch_assoc($res) )
                    {
                        echo "<option value=" . $cl['ClID'] . ">" . $cl['ClNome'] . "</option>";
                    }
                }

            ?>
            </select>
        </form>

        <div id="dvAlvo">
            <!-- ALVO DA REQUISIÇÃO AJAX -->
        </div>

    </body>
</html>

get_obs.php

<?php

    $conexao = mysqli_connect("host", "usuário", "senha", "banco de dados"); // CONFIGURE!!!
    $id = $_POST['cl_id'];
    $res = mysqli_query( $conexao, "SELECT 'ClOBS' FROM 'clientes' WHERE 'ClID' = '" . $id . "';");
    $desc = mysqli_fetch_assoc($res);
    echo nl2br( htmlspecialchars( $desc['ClOBS'] ) );

?>

Structure of the clients table:

+---------+--------------+------+-----+---------+----------------+
| Field   | Type         | Null | Key | Default | Extra          |
+---------+--------------+------+-----+---------+----------------+
| ClID    | int(5)       | NO   | PRI | NULL    | auto_increment |
| ClNome  | varchar(150) | YES  |     | NULL    |                |
| ClObs   | text         | YES  |     | NULL    |                |
+---------+--------------+------+-----+---------+----------------+

Remember to set the $conexao variable in both files!

  

I also need this to be hidden until the moment of choosing the option, after displaying what I did with the records.

Since the% target is empty, content will only be shown on it when you select any option from div . The trigger is in line 7 of select .

The code I wrote is 100% functional. Tested already with PHP 7 . But I treat description as remark in my code.

I did not care about form validation. Do this yourself. This code is for guidance.

Tip if there are a lot of clients: Instead of making a index.php showing all registered clients, I recommend that you create a select with input[type=text] of jQuery .

    
19.04.2017 / 06:28