Popular form already created with PHP(Mysql)

Asked

Viewed 248 times

0

I need to populate a form with Mysql database information in the onChange event of a Select input.

I know how to create a form from 0 by filling the values property, but filling a form already created I’m not getting.

input type="button" class="botao_form1" onclick="ativarform()" value="EDITAR">
                                <input type="submit" class="botao_form1" value="CADASTRAR">
                                <script>
                                    function ativarform(){
                                        document.getElementById("sobrenome_cliente").disabled = false;
                                        document.getElementById("idade_cliente").disabled = false;
                                        document.getElementById("telefone_cliente").disabled = false;
                                        document.getElementById("estado_cliente").disabled = false;
                                        document.getElementById("endereco_cliente").disabled = false;                                           
                                        document.getElementById("cidade_cliente").disabled = false; 
                                    }

                                    function actionform(){
                                        /* AQUI O FORM DEVE RECEBER AS INFORMAÇÕES DO BANCO */
                                        document.getElementById("sobrenome_cliente").disabled = true;
                                        document.getElementById("idade_cliente").disabled =  true;
                                        document.getElementById("telefone_cliente").disabled =  true;
                                        document.getElementById("estado_cliente").disabled =  true;
                                        document.getElementById("endereco_cliente").disabled =  true;                                       
                                        document.getElementById("cidade_cliente").disabled = true;      
                                    }
                                </script>
                                <?php 
                                        function popula(){
                                            $conexao = conectar();
                                            $nome = 'nome_ciente';
                                            $sql = mysqli_query($conexao, "SELECT * FROM cliente WHERE nome='$nome'");
                                            $row = mysqli_fetch_array($sql);
                                        }
                                ?>
  • I couldn’t quite understand the scope of your problem. What exactly aren’t you getting? What have you tried? Had an error message?

1 answer

1

Patrick, I don’t think you understand what it’s like to change a form without reloading the page (I believe this is what you’re trying to do).

The operation is more or less like this:

inserir a descrição da imagem aqui

The form "chat" with the server without reloading with the page, then vc "need" to call a second page that assembles the options (or until the whole select), so would be like this, your static page with the two forms:

One to choose:

<form>
 <select id="marca">
  <option value="volvo">Volvo</option>
  <option value="saab">Saab</option>
  <option value="mercedes">Mercedes</option>
  <option value="audi">Audi</option>
 </select> 
</form>

Another to be populated by the choice of the first:

<form>
 <select id="modelos">
 </select> 
</form>

And a php page to receive and assemble options

<?php 
  function popula(){
           $conexao = conectar();
           $nome = 'nome_ciente';
           $sql = mysqli_query($conexao, "SELECT * FROM cliente WHERE nome='$nome'");
           $row = mysqli_fetch_array($sql);
         }
?>

And a loaded script:

$("select#marca").on("change", function () {
  $.getJSON( "/pesquisa.php", function( data ) {
    var items = [];
    $.each( data, function( key, val ) {
      items.push("<option value="+key+">"+val+"</option>");
    });

    $( "select#modelo").html(items);
  });    
});

Browser other questions tagged

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