Keep fields filled in edit mode

Asked

Viewed 63 times

1

Colleagues.

I have a code, which is not my own, which when registering the user click on a button Add more sizes and more fields appear. See:

inserir a descrição da imagem aqui

So far all right, sizes and stocks go to the database. The problem is time to edit. I would like these fields to already appear "open" so I can include the information coming from the database. See the code:

<table border="0">  
<tr class='linhas'>
    <td  style="padding: 5px"><input type="text" name="Tamanho[]" class="form-control" placeholder="Tamanho"></td>
    <td  style="padding: 5px"><input type="text" name="Estoque[]" class="form-control pull-left" placeholder="Estoque"></td>
    <td  style="padding: 5px"><button type="button" class="removerCampo btn btn-danger" title="Remover linha"><i class="fa fa-minus-square" aria-hidden="true"></i> Remover</button></td>
</tr>
<tr><td colspan="3"><button type="button" class="adicionarCampo btn btn-primary" title="Adicionar item"><i class="fa fa-plus-square" aria-hidden="true"></i> Adicionar mais tamanhos</button></td></tr>
</table>    

Jquery

<script type="text/javascript">
$(function () {
  function removeCampo() {
    $(".removerCampo").unbind("click");
    $(".removerCampo").bind("click", function () {
       if($("tr.linhas").length > 1){
        $(this).parent().parent().remove();
       }
    });
  }

  $(".adicionarCampo").click(function () {
    novoCampo = $("tr.linhas:first").clone();
    novoCampo.find("input").val("");
    novoCampo.insertAfter("tr.linhas:last");
    removeCampo();
  });
});
</script>
  • This way you can’t answer your question, give more details. Which language are you using to make the connection to the bank? Do you already have the information in the view? What format is coming?

  • Hello Junior. The problem is in jquery and not in PHP or Mysql. I need that when editing, appear the amount of fields related to the amount of registered information based on the post code.

  • Yes, but how will you get how many items are registered and what information is due? Will this have to come from Mysql and PHP not? You are already bringing?

  • That... I am, yes. I can bring the information from the database, all the fields of the form are already filled with the information, only these are missing...

  • But we need to know how this information is coming, what is the name of the variable? what is the structure of the data? has as you "print" the value of this variable?

  • Hello Junior. I managed to bring. I did inside the same PHP loop.

Show 1 more comment

1 answer

0

I managed to resolve bringing from PHP even.

$sql = mysqli_query($this->conexao,"SELECT * FROM produtos WHERE IDProdutos = '".$idProdutos."';");
     $visualizar = "";

    while($jmTamanhos = mysqli_fetch_object($sql)){  
        if($jmTamanhos->Tamanho != ""){
           $visualizar .=  "<tr class='linhas'>
                            <td  style=\"padding: 5px\"><input type=\"text\" name=\"Tamanho[]\" class=\"form-control\" placeholder=\"Tamanho\" value='".$jmTamanhos->Tamanho."'></td>
                            <td  style=\"padding: 5px\"><input type=\"text\" name=\"Estoque[]\" class=\"form-control pull-left\" placeholder=\"Estoque\" value='".$jmTamanhos->Estoque."'></td>
                            <td  style=\"padding: 5px\"><button type=\"button\" class=\"removerCampo btn btn-danger\" title=\"Remover linha\"><i class=\"fa fa-minus-square\" aria-hidden=\"true\"></i> Remover</button></td>
                        </tr>";      
        }
     }
     return $visualizar;     
 }

Browser other questions tagged

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