Fill modal to edit with information coming from the database based on id

Asked

Viewed 86 times

0

I need to fill a Modal in edit mode based on the received id. My code:

$("body").on('click', '#editfoto', function () {
        $('#modalEdit').modal('show');

        id = $(this).data("idedit");       

        $.get("editafoto.php?id=" + id, function (data, status) {
        }).done(function (data) {
            
            descricaofoto = data['descricaofoto'];
            datavisita = data['datavisita'];
            
            //preenche os campos com dados do banco
            $('#descricaofoto').val(descricaofoto);
            $('#datavisita').val(datavisita);
     

            //console.log("STATUS : ", data);
        }).fail(function (jqXHR, textStatus) {
            console.log("Request failed: " + textStatus);
        });

    });
<div class="modal fade" id="modalEdit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="exampleModalLabel">Editar Foto</h5>
                        <button type="button" class="close" data-dismiss="modal" aria-label="Fechar">
                            <span aria-hidden="true">&times;</span>
                        </button>
                    </div>
                    <div class="modal-body">
                        <form class="editfoto" method="POST" enctype="multipart/form-data">
                            
                            <div class="row justify-content-md-left">
                                <div class="col col-lg-3">
                                    <label class="control-label" for="datavisita">Data Vísita</label>
                                </div>
                                <div class="col col-lg-6">    
                                    <input type="text" name="dtvisita" id="dtvisita" required autocomplete="off" class="form-control  form-control-sm form-group small" />
                                </div>
                            </div>

                            <div class="row justify-content-md-left">
                                <div class="col col-lg-3">
                                    <label class="control-label" for="idvisita">Visíta</label>
                                </div>
                                <div class='col col-md-6'>   
                                    <input type="text" name="idvisita" id="idvisita" readonly class="form-control  form-control-sm form-group small" />
                                </div>
                            </div>
                            <br />
                            <div class="row justify-content-md-left">
                                <div class="col col-lg-3">
                                    <label class="control-label" for="percentandamento">Porcentagem Andamento</label>
                                </div>
                                <div class="col col-md-6 slidecontainer">
                                    <input type="range" name="percentandamento" id="percentandamento" required min="1" max="100" class="form-control-range slider" oninput="disp.value = percentandamento.value">
                                    <output  id="disp"></output>
                                </div>
                            </div>
                            <div class="row justify-content-md-left">
                                <div class="col col-lg-3">
                                    <label class="control-label" for="caminhofoto">Foto</label>
                                </div>
                                <div>   
                                    <input type="file" name="caminhofoto" id="caminhofoto" required class="form-control-file form-control-sm form-group small" accept="image/png,image/jpg" />
                                </div>
                            </div>
                            <div class="row justify-content-md-left">
                                <div class="col col-lg-3">
                                    <label class="control-label" class="control-label" for="descricaofoto">Descrição Foto</label>
                                </div>
                                <div class='col-md-auto'>                
                                    <textarea name="descricaofoto" id="descricaofoto" cols="25" rows="3" class="form-control  form-control-sm form-group small" ></textarea>
                                </div>
                            </div>  
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-dismiss="modal">Fechar</button>
                        <button type="submit" id="btnEdit" class="btn btn-primary">Salvar</button>
                    </div>
                    </form>
                </div>
            </div>
        </div>

ajax output in the edita.php file is working, but is making an error of Uncaught TypeError: data is not a function.

  • I think you’ll have to use an Ajax.

  • have any idea how I might fill the form with ajax?

  • In this answer has more or less what you want to do.

  • Would not be data['descricaofoto']? Brackets instead of brackets. Or data.descricaofoto?

  • he is not filling the fields of modal, I tested data['descricaofoto'] and data.descricaofoto

  • It seems to me that the id’s are incorrect.

  • If I put: alert(descricaofoto); he shows: undefined

Show 3 more comments

1 answer

1

Jb, since you are using Jquery, you can use ajax, after the return has been successfully effected you can handle the responses, mapped the DOOM via jquery.

    $.ajax({
    	method: 'GET',
    	url: 'url/path',
    	dataType: 'JSON',
    })
    .done(function (response) {
        const data = response.data 
    	$('input[name="dtvisita"]').val(data.idvisita)
    	$('input[name="percentandamento"]').val(percentandamento)
    })
    .fail(function(error) {
    	console.error(error)
    })

  • in the.php edit file I need to return a json, or object?

  • it worked, I’m just not getting the file path. I need to display in the modal a photo for editing that comes from the database its path.

  • Uncaught DOMException: Failed to set the 'value' property on 'HTMLInputElement': This input element accepts a filename, which may only be programmatically set to the empty string.

Browser other questions tagged

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