Trigger a function from a Bootstrap modal

Here is a preview of my HTML code :

<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>

When it comes to Javascript, here is the script I've used :

$('#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;
        }
    });

For a live demo, click on this link : http://jsfiddle.net/oscar11/xxx03c6z/

I am looking for guidance on how to trigger an action from a bootstrap modal. Specifically, how can I ensure that when clicking "save changes" in the modal, the system calls the action form2_action.php ?

Your help is greatly appreciated.

Answer №1

  • Ensure your form has a unique id to use in the JavaScript code.

    <form id="myForm">

  • Create a function like this:

    function submitForm(){
       $('#myForm').submit();
    }
    
  • Trigger the submitForm function when Save button is clicked.

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

Answer №2

  • Form Submission

    When the button with id "modal-submit-btn" is clicked, the modal dialog with id "modal-dialog-id" will be hidden and the form with id "form-id" will be submitted.

Answer №3

Review my code snippet, everything seems to be functioning correctly. Simply place the form tag in bootstrap mode and extract values from input to a bootstrap model. For reference, you can view the fiddle here: https://jsfiddle.net/user123/g8w4i3j7/

<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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Dealing with a 404 error when using the JQuery AJAX PUT method in ASP.NET MVC

My issue arises when attempting to update my data in ASP.NET MVC using jQuery AJAX with the PUT method. I am encountering a 404 error, indicating that my controller cannot be found. However, everything works fine when I use the GET or POST methods. What ...

Adding middleware to the res.render() function can be extremely helpful when dealing with a large number

Recently, I put together a webpage for local use and it involves many routes. Specifically, I have 31 .ejs files and 3 .html files all neatly stored in the "views" folder. //app.js - using node and express app.get('/page1', function(req, res ...

A combination of fixed width column and dynamic content area in Bootstrap design

Is there a way to achieve the combination of fixed and fluid width columns using Twitter Bootstrap alone, similar to this example? The HTML and CSS in the example make it seem simple. But can this be done solely with Bootstrap? ...

Having trouble with basic authorization for Facebook API using JavaScript?

I am having an issue with my source code. When I run it, I receive an alert that says "No Response" and then the Facebook page displays an error message stating "An error occurred with MYAPP. Please try again later" <div id="fb-root"></div> & ...

Save information on users' Google accounts

Utilizing this resource, I successfully implemented a login feature on my website. The implementation includes functions such as onSignIn, signOut, and auth2.isSignedIn.get() to manage user authentication. Now, I am looking for a way to extract specific d ...

Can you explain the contrast between img.height and img.style.height?

I'm currently in the process of resizing a series of images by iterating through an array and adjusting their sizes. if(items[0].height > 700 || items[0].width > 700){ items[0].style.height = "700px"; items[0].style.width = "700px"; } As ...

Retrieve weight measurements from a serial port by using Node JS

I'm a novice in node.js and I'm trying to retrieve weight data from a serial port. I have nodejs(v14.19.0) and npm(6.14.16) installed, and I'm attempting to obtain weight data using localhost:8080/get_weight. However, the script isn't f ...

Can Authorization be Combined with Filtering in a Node.js RESTful API?

In my current setup, I have a web application communicating with a RESTful API to interact with the database. The frontend of the web app uses Angular HTTP to post/get/put data to the backend, which then manages authentication, interacts with the API, and ...

Is there a way to remove the old React component when there are two instances of it still active while passing variables?

Recently, I've encountered some unusual behavior with my React component. As a newcomer to React, I have a page where users can configure toast notifications that are displayed. For this functionality, I'm utilizing the react-hot-toast package. U ...

Encountered an error 'Unexpected token ;' while attempting to include a PHP variable in a jQuery AJAX call

Trying to execute a PHP script that updates a deleted field in the database when you drag a specific text element to a droppable area. Currently, here is the droppable area code: <div class="dropbin" id="dropbin" > <span class="fa fa-trash-o ...

Frontend experiencing issues with Laravel Echo Listener functionality

I have recently developed a new event: <?php namespace App\Events; use Illuminate\Broadcasting\Channel; use Illuminate\Broadcasting\InteractsWithSockets; use Illuminate\Broadcasting\PresenceChannel; use Illuminate&bs ...

AngularJS: Display the last four characters of a string and substitute the rest with 'X'

I am attempting to change the characters with X and make it look something like this XXXXXT123 This is what I have tried: var sno = 'TEST123'; alert(sno.slice(0,3).replaceWith('X')); However, I encountered an error in the console ...

material-ui DropDown with an image displayed for the selected value

Can someone help me figure out how to display an image in my material-ui dropdown menu? I'm currently using version 0.19.1 and have written the following code: <DropDownMenu autoWidth style={{ width: 500, marginBottom: 30 }} underlin ...

How can I include a nested object in an ng-repeat loop in Angular?

I'm new to using Angular so please excuse my basic question. Here is the structure of my model for posts and comments: [ { "title": "awesome post", "desc": "asdf", "comment_set": [ { "id": 2, ...

When you click on Highcharts, a bigger graph will be displayed

I am working on a feature that allows users to zoom in on small charts on my website. This involves revealing an overlay div and displaying a larger chart with the same data as the clicked chart inside it. function DrawSmallChart() { chart_data = [1, 2, 3 ...

Utilize alternating colors from the Material UI palette to enhance the appearance of

Can someone help me understand how to utilize the different color variants in Material UI? For instance, the theme primary color has various options like 100, 200, 300, 400, 500, and so on. How can I access these colors from within my component? I attempt ...

The dropdown menu repeatedly opens the initial menu only

My script fetches data from a database to populate a table with one row for each member. Each row contains a dropdown list with the same class and ID. Although I attempted to open and close the dropdowns using specific codes, I am facing an issue where onl ...

Unable to declare a string enum in TypeScript because string is not compatible

enum Animal { animal1 = 'animal1', animal2 = 'animal2', animal3 = 'animal3', animal4 = 'animal4', animal5 = 'animal5' } const species: Animal = 'animal' + num Why does typescr ...

Addressing the issue of empty ngRepeat loops

Utilizing ngRepeat to generate table rows: <tr ng-repeat="User in ReportModel.report" on-finish-render> <td><span>{{User.name}}</span></td> </tr> An on-finish-render directive triggers an event upon completion of t ...

React client side componentDidMount function encountering issues (server side rendering)

Greetings to the Stackoverflow community Apologies in advance if my explanation is not clear enough When a user directly types a URL, the server makes a request, fetches the corresponding data, and renders it on the screen flawlessly. However, when a us ...