Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Twitter Bootstrap Modal Form Submission

I am a beginner in Twitter Bootstrap and I am trying to process a form from a modal box, which is also loaded with Ajax. The problem is that I don't know how to use it. I have searched on google for hours, but I couldn't find a good example.

I have used jquery ui before and I guess it might be almost the same. I would like to know the following:

  1. How do I load a file which contains a form with Ajax
  2. Does it work to simply use the selectors (e.g. $('#item');) just after the form was loaded, to get the values typed in the forms
  3. How do I bind the Submit button of the modal to send the form through Ajax to another file

I will appreciate the help and I can provide a form sample below:

    <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h3>Submit a link</h3>
    </div>
    <div class="modal-body">
    <div id="msgholder1"></div>
    <div id="msg-loader"></div>
    <form action="../ajax/controller.php" data-async data-target="#msgholder1" id="add-link-form" method="POST">
    <table id="theform">
    <tr>
    <td>URL:</td>
    <td><input type="text" name="url" size="45" class="text ui-widget-content ui-corner-all" id="url" /></td>
    </tr>
    <tr>
    <td>Quality:</td>
    <td><select name="quality" id="quality">
          <option value="0">Pick One ...</option>
          <option value="1">CAM</option>
          <option value="2">TS</option>
          <option value="3">DVD</option>
    </select><br />
    </td>
    </tr>
    <tr>
    </fieldset>
    </form>
    </div>
    <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
    <button class="btn btn-primary">Save changes</button>
    </div>

Any kind of related documentation is also useful.

Thanks!

like image 590
Comforse Avatar asked Dec 02 '22 21:12

Comforse


1 Answers

After some hours of research and work around, I've got the following working Ajax Modal script:

<script type="text/javascript">
$(document).ready(function() {
  $('#addlink').on('click', function(event) {
    var href = SITEURL + '/modules/movies/addlink.tpl.php?movie_id=<?php echo $movie->id; ?>';
    $.get(href, function(response) {
      $('.modal').html(response);
      $('.modal').modal({keyboard:true});
      $('button#submit_link').bind('click', function()
        {
        $('.modal-body').html('<p>Loading ...</p>');
        $.ajax({
                    type: 'post',
                    url: SITEURL + "/ajax/controller.php",
                    data: 'action=report_link',
                    dataType : 'html',
                    context: $(this),
                    success: function (msg) {
                        $(".modal-body").html(msg);
                    }
                });
        });
    });
    event.preventDefault();
  });
});
</script>
like image 177
Comforse Avatar answered Dec 04 '22 09:12

Comforse