Keep the fields filled in editing mode

1

Colleagues.

I have a code, which is not my own, which at the time of registering the user clicks a button Add more sizes and more fields appear. See:

Sofarsogood,sizesandstocksgotothedatabase.Theproblemistimetoedit.Iwouldlikethesefieldstoalreadyappear"open" so I can include the information that will come from the database. Look at 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>
    
asked by anonymous 06.04.2017 / 20:57

1 answer

0

I was able to solve it by bringing PHP itself.

$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;     
 }
    
06.04.2017 / 23:35