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

Issue: No property named 'counterUp' has been defined on the 'JQLite' type

I’m attempting to implement the counterUp() function from JQuery into my Angular project, but I keep encountering an error. The error message mentions jqlite instead of jquery. I have attempted to rectify the issue but have not been able to find a su ...

What determines which HTML file is loaded based on the user's browser?

I've been searching online but can't find a definite answer - is it possible to load different HTML based on the type of browser being used? In my specific case, this seems to be the only solution. After trying everything else, it looks like the ...

Thumbnails failing to line up in a continuous row

Having an issue creating a row with 3 thumbnails as they are not aligning in a single row, instead each thumbnail is going into a different row. echo "<table>"; echo "<tr>"; echo "</tr>"; while($r ...

The React application is experiencing difficulty in rendering SVG images exclusively on Windows operating systems

My react app runs smoothly on OSX, but encounters issues on Windows due to SVG files: Module parse failed: Unexpected token (2:0) You may need an appropriate loader to handle this file type. <svg xmlns="http://www.w3.org/2000/svg" viewBox="..."> I ...

How to turn off automatic password suggestions in Chrome and Firefox

Currently, I have integrated a 'change password' feature which includes fields for 'old password', 'new password', and 'retype password'. However, the autocomplete feature is suggesting passwords from other user acco ...

Aligning Content in the Header

Currently, I am attempting to place a logo on my WordPress site right at the top of the header above the menus and in the center. Even though I've positioned it at the top, I'm struggling to align it horizontally in the center. I've experime ...

Deciphering the LocalDate and nested object array in an AJAX response

Seeking assistance, looking for solutions to two problems. Firstly, how can I display LocalDate in an ajax response? And secondly, how do I iterate over a list of Custom objects received in the ajax response? I am passing a List of Custom Objects and Loca ...

There appears to be an issue with the onmousemove function

I am currently troubleshooting an issue with a script that I wrote for my button. Interestingly, the code works flawlessly when I test it on CodePen, but I encountered an error when I attempted to run it in VSCode. Even after trying to recreate the script ...

The complexity of utilizing the map() function in React causes confusion

While delving into React, I stumbled upon this interesting code snippet: {this.state.sections.map(({title, imageUrl, id, size}) => ( <MenuItem key={id} title={title} imageUrl={imageUrl} size={size}/> ))} I'm intrigued by the use of destruc ...

The form-group nested within a form-horizontal is exceeding the boundaries of the preceding form-group

I am currently utilizing Bootstrap 3.3.6 and have a basic form enclosed in a panel: https://i.stack.imgur.com/UOFI6.png Although it is quite simple, the alignment of the "Post" button is not displaying properly. Here is the HTML code for the form: < ...

How to display JSON containing nested objects in AngularJS using the ng-repeat directive

Hey everyone, I have this JSON external file that I need help with: { "success":true, "errors":[ ], "objects":[ { "cod":"8211300", "descricao":"Serviços advocatícios" }, // more objects here... ] } In ...

"Creating multiple circles on an HTML5 canvas using an iPad: A step-by-step guide

My current code is only drawing one circle at a time on an iPad view, but I want to be able to draw multiple circles simultaneously. How can I achieve this? // Setting up touch events for drawing circles var canvas = document.getElementById('pain ...

Ways to verify the application of CSS hyphens using Chrome's developer tools

I cannot seem to get automatic hyphens to work via CSS. I have applied the following CSS: body { -moz-hyphens: auto; -ms-hyphens: auto; -o-hyphens: auto; -webkit-hyphens: auto; hyphens: auto; } Despite adding this CSS, my text is not being hyph ...

NodeJS JSON file write function not updating as expected

I'm working on a task that involves reading an existing JSON file, updating the value within it, and then saving it back. However, I've encountered the following error message: node:internal/modules/cjs/loader:944 throw err; ^ Error: Cannot ...

The error message "There is no defined window.matchMedia prefers-color-scheme window in Next.js"

I am currently working on a project with React.js alongside Next.js and encountered an issue that I need assistance with. Upon loading the page, I need to set a variable that indicates whether the user is using dark mode or not. I attempted the following ...

What is the most effective way to extract data that includes an array within it?

const flightList = [{ number: 343, from: "Singapore", to: "India", upgradeTypes: ["Economy to Premium Economy", "Economy to Business Class"] }, . { number: 363, from: "Chennai", to: "Sing ...

The css cropping issue with sprite image is not being resolved

I'm currently working on creating a sprite image for the bottom links on our media page, but I seem to be having trouble getting the image to crop correctly. Can anyone point out where I may be making a mistake? CSS .footer{ position:absolute; ...

Customize stroke widths for each individual SVG item

I'm facing an issue with applying consistent stroke width to my CSS on a webpage. Here is the code snippet I am using: path { fill: none; stroke-width: 10pt; stroke: red; } Despite this, the rendering appears differently on certain SVGs. You c ...

Two events in an arrow-guided grid including a text box (jQuery)

I have successfully implemented an editable table using jQuery where users can click on a cell to input text. The goal now is to enable the use of ARROW keys for quick navigation within the table. Below is my current code: $( function () { $( "td" ). ...

Instructions for making a crossword-inspired grid using a high quantity of divs or alternative elements

I am looking to create a grid on the screen of a web browser or mobile device, with each grid item containing just one letter. It's similar to a crossword puzzle, but taking up most of the screen. I've tried using divs for this purpose, but it se ...