pass back data from bootstrap modal to his parent window

3k views Asked by At

Someone could help me with a little piece of javascript code ? I'm trying to develope a little webapp, but I'm in trouble with simple communication from a modal to his parent window :( I'm using jquery, bootstrap, bootstrap.datetimepicker and bootstrap.validator1000hz.

I have to call a function from parent window with 'sendData' input field as parameter so I need to grab the value of 'sendData' from the modal. Submit button is correctly disabled until user input the required date (because Validator plug-in) but when I click on it modal disappear end nothing is printed to console. Thanks in advance !

This is my modal

<!-- Modal -->

  <div id="modalPrint" class="modal fade" role="dialog">
  <div class="modal-dialog">

    <!-- Modal content-->
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">&times;</button>
        <h4 class="modal-title">Seleziona intervallo di stampa</h4>
      </div>
      <div class="modal-body">    
          <form>
          <div class="form-group">
               <div class="form-group">
                       <label>Indicare la data d'invio</label>
                          <div class='input-group date col-sm-3' id='sendData' data-date-format="DD/MM/YYYY">
                            <input type='text' class="form-control" required >
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                          </div>
                          <div class="help-block with-errors"></div>
                    </div>
               <button type="submit" id="print" class="btn btn-default btn-sm pull-right" data-dismiss="modal">
                                        <span class="glyphicon glyphicon-print"></span>
                                        <span style="font-size:smaller;">Stampa</span>
               </button> 
          </div>
         </form>

      <div class="modal-footer">

      </div>
    </div>
  </div>
</div>
<script>
    $('#modalPrint').validator().on('submit', function () {
         console.log('ok')
     });
</script>
1

There are 1 answers

5
S.A.Parkhid On

In Modal You can use this :

<button id="print" onclick="SendData('+yourData +')" class="btn btn-default btn-sm pull-right" data-dismiss="modal">
                                        <span class="glyphicon glyphicon-print"></span>
                                        <span style="font-size:smaller;">Stampa</span>
</button> 

Then define SendData function in your js like this :

function SendData (_data){
 var serialedjs = "...";
 $.ajax({
               type: "POST",
               url: 'page.aspx/YourWebMethodName',
               data: serialedjs,
               contentType: "application/json; charset=utf-8",
               dataType: "json",
               beforeSend: function (msg) {
                   //close modal here
                   $("div.modal-body").append(
                       '<img class="spinner" src="Content/ajax-loader.gif" />'
                   );
               },
               success: function (data) {
               },
               complete: function (data) {

               }
           });
}
}

Update :

jquery ajax is an asynchronous HTTP (Ajax) request. it sends data while your page doesn't start any post back to server. here is an ajax call with jquery:

$.ajax({
   url: 'http://yourpage_url/yourpage.html',
   data: {
      'yourDataKey1': 'yourDataValue1',
      'yourDataKey2': 'yourDataValue2',
      'yourDataKey3': 'yourDataValue3'
   },
   dataType: 'jsonp',
   contentType: "application/json; charset=utf-8",
   success: function(data) {
     alert('finish');
   },
   type: 'GET'
});

as an another exmaple you can see below:

$.ajax({
  url: "/api/getWeather",
  data: {
    zipcode: 97201
  },
  success: function( result ) {
    $( "#weather-temp" ).html( "<strong>" + result + "</strong> degrees" );
  }
});

which Calls a local script on the server /api/getWeather with the query parameter zipcode=97201 and replace the element #weather-temp's html with the returned text to show status to user on page.

if you are using php the echo on the page will send as result , if you use asp.net the Response.Write Method writes the result and give it back to you. Please see the picture below: enter image description here

So in url paramter of data object in ajax call , you will set the url of page which will process your request.

So you need add this script in top of your page:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>

or you can download jquery and put it near of your project files and reference them :

<script src="/libs/jquery/3.1.1/jquery.min.js"></script>

for reading more about jquery ajax , please visit here: https://learn.jquery.com/ajax/key-concepts/