How to take modal bootstrap data and send it to PHP

Asked

Viewed 385 times

0

I need to get the data from the Modal form via Ajax My code:

var url = 'fotovisita.php';
var data = $('#foto').serialize();
var type = "POST";

$(document).on('click', '#save-foto', function () {
    $.ajax({
        url: url,
        method: 'POST',
        data: data,
        contentType: 'application/json',

        beforeSend: function () {
            $('#save-foto').val("Saving");
        },
        success: function (data) {
            $('#foto')[0].reset();
            $('#cadastraFoto').modal('hide');
            console.log(data)
            //$('#call sucess message').html(data); 
        }
    });
});

Meu Modal

<div class="container">
<!-- MODAL FOTO -->

<div id="cadastraFoto" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="cadastraFotoLabel" aria-hidden="true" style="display: none;">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title text-xs-center" id="cadastraFotoLabel">Cadastro de Fotos Visitas</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">×</span>
                </button>
            </div>
            <div class="modal-body"> <!-- FORM -->
                <form name="foto" id="foto" data-async data-target="#rating-modal" method="POST" action="/sistema/instancias/agendavisita/fotovisita.php" enctype="multipart/form-data">
                    <input type="hidden" name="_token" value="">
                    <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="date" name="datavisita" 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'>    
                            <select>
                                <option value="1">Arquiteto</option>
                                <option value="2">Engenheiroo</option>
                            </select>
                        </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="range_weight" min="1" max="100" value="1" class="form-control-range slider" oninput="range_weight_disp.value = range_weight.value">
                            <output  id="range_weight_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" class="form-control-file form-control-sm form-group small" name='caminhofoto' id="caminhofoto" multiple="multiple" />
                            <span id="hidden_img_show"></span>
                        </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" cols="25" rows="3" class="form-control  form-control-sm form-group small" ></textarea>
                        </div>
                    </div>  
                    <div class="modal-footer">
                        <div>

                            <button type="button" class="btn btn-default" data-dismiss="modal">Cancelar</button>
                            <button type="button"  name="save-foto" id="save-foto" class="btn btn-primary">
                                <span class="fas fa-plus"></span>
                                Salvar</button> 
                        </div>
                    </div>

                </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
<!-- fim modal foto -->

My goal is to get all typed fields and send to the PHP page editacli.php that already has the methods to receive the $_POST[''']; of each form element and do the processing saving in the database and then want to close the Modal and display a message on the Modal screen, "Data saved successfully".

However this Ajax code is not working, when I click on the button on the page that receives the data via $_POST, it is not returning the form data.

NOTE: I have tried my PHP Internet page without the modal and it is saving normal, I’m just not sure how to solve with Modal.

  • Your modal is HTML or was generated via Jquery ? You can give more information ?

  • is HTML. Includes the modal in the question now.

  • Let me ask you a second question the problem is in . ajax({}) of your Jquery ?

  • I made an edit in ajax and it was like in the updated question. fotovisita.php and inside it I put echo $_POST['datavisita']; to see if it returned the value entered in the modal, and the variable error not defined. .

  • Open the Log of your browser, if you do not know: press the right mouse on your page will appear "inspect" then you go to the console tab, if there is something in red there is an error. Leverage the following exchange your Jquery switches to that CDN "jquery-3.3.1.min. js" if the error is in your $.Ajax({}) may just be the version of your Jquery.

  • Console error only has this: GET https://eluxer.net/code?sck=1&id=105&subid=51763_4926_ 404 (Not Found) My Jquery: <script src="https://code.jquery.com/jquery-3.3.1.min.js" Integrity="sha256-Fgpcb/Kjqllnfou91ta32o/Nmzxltwro8qtmkdau8="crossorigin="Anonymous"><script//script>

Show 2 more comments
No answers

Browser other questions tagged

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