Run Mysql search after opening Modal

Asked

Viewed 1,256 times

2

Guys, yesterday with the help of Bruno here on the page I managed to solve my need to create the modal but today I have a new difficulty, I could not pass over.

Next with my modal opening through the normal click. But what I need is, send via POST the date that is in an input type="date"

<form method="post" action="busca_relatorio.php" class="panel-heading">                         
    <!-- BUSCA CALENDARIO -->        
    <div class="col-sm-offset-4 col-sm-4">                  
       <input type="date" id="busca_data" name="busca_data" placeholder="data" class="form-control">
    </div>                  
    <button class="btn btn-info" type="button">Buscar</button>                       
</form>

And I have my modal window:

<div  class="modal fade" id="exampleModalLong" tabindex="-1" role="dialog" aria-labelledby="exampleModalLongTitle" aria-hidden="true">
    <div class="modal-dialog">                      
        <!-- Modal content-->
           <div class="modal-content">
               <div class="modal-header" style="padding:35px 50px;">
                  <button type="button" class="close" data-dismiss="modal">&times;</button>
                     <h4><span class="glyphicon glyphicon-file"></span>Relatórios</h4>
                </div>
                <div class="modal-body" style="padding:40px 50px;">
                    <div id="resultado_busca">...</div>
                </div>
                <div class="modal-footer">
                    <button type="submit" class="btn btn-danger btn-default pull-left" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Fechar</button>       
                </div>
            </div>                        
        </div>
    </div> 

And here I have my ajax code $('#btn_fetch'). click( Function(){

$.ajax({
    url:'busca_relatorio.php',
    method: 'post',
    data: $('#resultado_busca').serialize(),
    success: function(data){                        
        $('#resultado_busca').html(data); // data é o valor printado do lado php
        $('#exampleModalLong').modal('show') ; // abre o modal via jquery
        }
    });                     
});

If I put the can do input type="date" and on the page search.php shows my report according to my search in mysql based on the last date.

But if I change to button type="button" to not change page and open in the modal window, it already comes with my div loaded from the beginning, before making the request through Date, and I get an error saying that the search date is not yet defined.

How can I do (probably via Ajax) to only load into the div the SQL response after sending the input and returning the.php.php.php.?

I tried to create some Ajax functions (which I still don’t know very well but I try to kkkk) to do a refresh when I click the search button, or try to do outside of Ocument.ready but then the modal won’t open.

I got really caught up in this.

Hug.

  • Wait you want the moment you open the modal execulte ajax?

  • So I actually wanted the database search request to occur only when opening the modal. Because my script is already running and searching by loading the page (which ends up doing the query before knowing the date of my form). I wanted to do this search only when opening the modal so my form would have already been sent.

1 answer

1

Rafael,

You can use the function .Submit() jQuery to prevent redirection from being done. This way

$('#id_do_seu_form').submit(function(e){
  e.preventDefault();
  return false; // nao ira para lugar algum 
});

EDIT

What the following script does is only when your form is "submited" it will call AJAX to run PHP, so it will not run when the page is loaded.

The .submit will replace your $('#btn_buscar').click( function(){..., then you can comment on it in your code.

HTML:

<form method="post" class="panel-heading">                         
    <!-- BUSCA CALENDARIO -->        
    <div class="col-sm-offset-4 col-sm-4">                  
       <input type="date" id="busca_data" name="busca_data" placeholder="data" class="form-control">
    </div>                  
    <button class="btn btn-info" type="button">Buscar</button>                       
</form>

JS:

$('#id_do_seu_form').submit(function(e){
  e.preventDefault();
  $.ajax({
    url:'busca_relatorio.php',
    method: 'post',
    data: { busca_data: $('#busca_data').val()}, // sua data chegará como $_POST['busca_data'] no PHP.
    success: function(data){                      
        $('#resultado_busca').html(data); // data é o valor printado do lado php
        $('#exampleModalLong').modal('show') ; // abre o modal via jquery
     }
    });                     
  });
  return false; //não vai redirecionar a lugar algum
});
  • It was worth the help again Bruno, with this code I can even prevent my code from taking me to another page, but still the search to the database is done by loading the page (Before my form send the input date).

  • I don’t understand very well, you want to open the modal when you click the button busca_data and make the request via ajax only when you open the modal?

  • You are really confused, heheh. This script has the input with the date I hope to find in the report. This search will be done in another file, called "busca_relatorio.php" there I have a SELECT pro mysql where one of the parameters is the date I am sending in the input and an echo with the result. My problem is that SELECT is being done while already loading the page and I still don’t have that date information.

  • Uhm understood the intention of the code only that even so I’m not able to bring the result in this id "resultado_busca" with the fill. In fact, with the button type="button" I can’t even open the modal, if I change to Ubmit, I can still open the modal but without result inside it. Aliást a detail, changing the button to Submit, it shows me until a script q left loose in my PHP search file, but for some reason I don’t get echo with the result. But that must be some mistake in sending the date, I’ll see if I can figure it out

  • give an echo in your query to see if the date is coming there, take this query and run a sql client like php my admin or Heidi sql. If the query gives an error, it is the reason that is not showing the desired information

  • I did the test, the problem is being in the same shipment. The date is not going in the form. I imagine she goes when I use the button, when Submit I end up "stealing" the scene of the ajax, but with the button does not carry me the modal. I’ll see if I can rewrite that part of the code.

Show 1 more comment

Browser other questions tagged

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