Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form inside the modal bootstrap

I have a modal that makes the registration of products. I wanted to submit a form inside the modal without leaving the modal.

<!-- Button to trigger modal -->
<a href="#myModal" role="button" class="btn" data-toggle="modal">Launch demo modal</a>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Modal header</h3>
  </div>
  <div class="modal-body">
    <form method="post">
        <button type="submit">Submit</button>
    </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>
</div>
like image 887
Max Avatar asked Mar 07 '13 19:03

Max


People also ask

Can you put a form inside a modal?

One of the most common use cases you see around the web is that of the Login Modal, logging in via a form inside a modal.

What is an example of a modal form?

Modal verbs show possibility, intent, ability, or necessity. Because they're a type of auxiliary verb (helper verb), they're used together with the main verb of the sentence. Common examples include can, should, and must.


2 Answers

In my case I only put make the submit button one the footer body and remove the data-dismiss i.e:


<div class="modal-content">
    <form id="role-form"  method="get">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal">&times;</button>

            <h4 class="modal-title">تعديل صلاحيات المدير - {{$user->username}}</h4>
        </div>
        <div class="modal-body">

            <div class="form-group col-md-12">

                <select id="role" name="role" class="form-control">
                    <option selected disabled>الصلاحية</option>
                    <option value='1'>مدير</option>
                    <option value='2'>مشرف</option>
                </select>
            </div>

            <div class="clearfix"></div>
        </div>
        <div class="modal-footer">

            <button type="submit" class="btn btn-success" >حفظ</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">إلغاء</button>

        </div>
    </form>
</div>
like image 175
Abdelrahman Elzedy Avatar answered Sep 17 '22 00:09

Abdelrahman Elzedy


Firstly, set an id on your <form> tag:

<form id="myForm" method="post">
    ...
</form>

If you were using jQuery (highly recommended), you could do this:

$(function(){
    $('#myForm').on('submit', function(e){
      e.preventDefault();
      $.post('http://www.somewhere.com/path/to/post', 
         $('#myForm').serialize(), 
         function(data, status, xhr){
           // do something here with response;
         });
    });
});
like image 45
PinnyM Avatar answered Sep 19 '22 00:09

PinnyM