简体   繁体   中英

Call action from modal bootstrap

My HTML code is like this :

<form action="form2_action.php" method="post">
    <table>
        <tr>
            <td>First Name </td>
            <td>:</td>
            <td><input type="text" id="first_name" name="first_name" required></td>
        </tr>
        <tr>
            <td>Last Name</td>
            <td>:</td>
            <td><input type="text" id="last_name" name="last_name" required></td>
        </tr>
        <tr>
            <td>Age</td>
            <td>:</td>
            <td><input type="text" id="age" name="age" required></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
            <td><input type="submit" value="Submit" id="submit"></td>
        </tr>
    </table>
</form> 


<div class="modal fade" id="confirm-save" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">

            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"><b>Are you sure to book this tour?</b></h4>
            </div>

            <div class="modal-body">
                <table style="width:100%">
                    <tr>
                        <td style="width:30%">First Name</td>
                        <td height top="40" style="width:70%" id="first_name_modal"></td>
                    </tr>
                    <tr>
                        <td>Last Name</td>
                        <td height="40" id="last_name_modal"></td>
                    </tr>
                    <tr>
                        <td>Age</td>
                        <td height="40" id="age_modal"></td>
                    </tr>                 
                </table>        

                <p class="debug-url"></p>
            </div>

            <div class="modal-footer">
                <button type="submit" class="btn btn-primary">Save changes</button>&nbsp;
                <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>  
            </div>
        </div>
    </div>
</div>

My Javascript code is like this :

$('#submit').on('click',function(e){

        var first_name_modal = document.getElementById("first_name").value;
        var last_name_modal = document.getElementById("last_name").value;
        var age_modal = document.getElementById("age").value;

        if(first_name_modal!="" && last_name_modal!="" && age_modal!=""){
            $("#confirm-save").modal("show");
            $('#first_name_modal').text(first_name_modal);
            $('#last_name_modal').text(last_name_modal);
            $('#age_modal').text(age_modal);
          return false;
        }
    });

Demo is like this : http://jsfiddle.net/oscar11/xxx03c6z/

How to call action from modal bootstrap?

So, when clicking on the button "save changes" in the modal bootstrap, How to keep the system to call the action form2_action.php ?

Thank you very much

  • Give your form an id for referencing in the javaScript code.

    <form id="form1">

  • Then define a function as below:

     function formSubmit(){ $('#form1').submit(); } 
  • Call formSubmit on click of Save button.

    <button type="button" onClick="formSubmit();" class="btn btn-primary">

  1. Form Submit

    $("#modal-submit-btn").click(function(){ $('modal-dialog-id').modal('hide'); $('#form-id').submit(); });

check my script it is working fine.. just put the form tag in bootstrap mode and take values from input to bootstap model. here is the fiddle, you can check: https://jsfiddle.net/kriz1439/v5wpwcv9/

<table>
    <tr>
        <td>First Name </td>
        <td>:</td>
        <td><input type="text" id="first_name" name="first_name" required>          </td>
    </tr>
     <tr>
        <td>Last Name</td>
        <td>:</td>
        <td><input type="text" id="last_name" name="last_name" required></td>
    </tr>
    <tr>
        <td>Age</td>
        <td>:</td>
        <td><input type="text" id="age" name="age" required></td>
    </tr>
    <tr>
        <td></td>
        <td></td>
        <td><button type="button" id="submit" class="btn btn-info btn-lg" data-toggle="modal" data-target="#confirm-save">Submit</button></td>
    </tr>
</table>



       <div class="modal fade" id="confirm-save" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
     <div class="modal-dialog">
    <div class="modal-content">
    <form action ="action.php">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title" id="myModalLabel"><b>Are you sure to book this tour?</b></h4>
        </div>

        <div class="modal-body">
            <table style="width:100%">
                <tr>
                    <td style="width:30%">First Name</td>

                 <td height top="40" style="width:70%"  >  <input type="text" id ="first_name_modal"></td> 
                </tr>
                <tr>
                    <td>Last Name</td>

                     <td height="40" > <input type="text" id ="last_name_modal"> </td>
                </tr>
                <tr>
                    <td>Age</td>

                     <td height="40">  <input type="text" id ="age_modal"> </td>
                </tr>                 
            </table>        

            <p class="debug-url"></p>
        </div>

        <div class="modal-footer">
           <input type="submit" /> &nbsp;
            <button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>  
        </div>
     </div>
     </div>
     </div>
  </form>

   </body>
      <script>
          $('#submit').on('click',function(e){

   document.getElementById("first_name_modal").value = document.getElementById("first_name").value;
    document.getElementById("last_name_modal").value= document.getElementById("last_name").value;
     document.getElementById("age_modal").value = document.getElementById("age").value;

    if(first_name_modal!="" && last_name_modal!="" && age_modal!=""){
        $("#confirm-save").modal("show");
        $('#first_name_modal').text(first_name_modal);
        $('#last_name_modal').text(last_name_modal);
        $('#age_modal').text(age_modal);
           return false;
              }
               });
     </script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM