Skip to content
Advertisement

How to preview a form data in a boostrap modal [closed]

My problem is I have a form let say where you input your name and surname and a button that triggers a bootstrap modal. On pressing this button, I want to display the data inputted in the previous form in my modal. Within the modal, the user can either close or submit the data that will be saved in a database.

Advertisement

Answer

You can Leverage Bootstrap Modal Event.

If you post your code, then I could better answer it

$('#myModal').on('shown.bs.modal', function () {

  //1- Get the value from form

  var surname = $('#surname').val();
  var surname = $('#surname').val();

  //2- Set the value in Modal

})

Edited


    <?php
   session_start();
   $_SESSION["userId"];
   ?>
<!DOCTYPE html>
<html>
   <head>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
      <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
      <link rel="stylesheet" href="../css/global.css">
      <link rel="stylesheet" href="../css/button$img.css">
      <title>Calculate Health Score</title>
      <link rel='icon' href='../img/favicon.ico' type='image/x-icon'/>
   </head>
   <body>
      <div class="container">
      <div class="col-md-12">
      <nav class="navbar navbar-expand-lg navbar-light bg-light">
         <a href="../index.php" class="navbar-brand">Mosant&eacute;</a>
         <button class="navbar-toggler" data-toggle="collapse" data-target="#navigation">
         <span class="navbar-toggler-icon"></span>
         </button>
         <div class="collapse navbar-collapse" id="navigation">
            <ul class="navbar-nav">
               <li class="nav-item "><a href="profile.php" class="nav-link">Profile</a></li>
               <li class="nav-item "><a href="../include/logout.inc.php" class="nav-link">Log Out</a></li>
            </ul>
         </div>
      </nav>
      <div class="d-flex justify-content-center align-items-center login-container">
         <form id="myform" class="login-form text-center" action="../include/calcule_sante.php" method="post">
            <h1 class="mb-5 font-weight-light text-uppercase">Calculate Health Score</h1>
            <div class="form-group">
               <label for="bPressure">Please insert your blood pressure</label>
               <input type="number" name="bPressure" min = "0"  id="bPressure" class="form-control rounded-pill form-control-lg" required>
            </div>
            <div class="form-group">
               <label for="LDL">Please insert your LDL g/l</label>
               <input type="number" name="LDL" step ="0.01" min = "0" id="LDL" class="form-control rounded-pill form-control-lg" required/>
            </div>
            <div class="form-group">
               <label for="HDL">Please insert your HDL g/l</label>
               <input type="number" name="HDL" step ="0.01" min = "0" id="HDL" class="form-control rounded-pill form-control-lg" required/>
            </div>
            <div class="form-group">
               <label for="HbA1c">Please insert your HbA1c g/l</label>
               <input type="number" name="HbA1c" step ="0.01" min = "0" id="HbA1c" class="form-control rounded-pill form-control-lg" required/>
            </div>
            <div class="form-group">
               <label for="waist">Please insert your waist size cm</label>
               <input type="number" name="waist" step = "0.01" min = "0" id="waist"class="form-control rounded-pill form-control-lg" required/>
            </div>
            <div class="form-group">
               <label>Do you smoke?</label>
            </div>
            <div class="btn-group btn-group-toggle" data-toggle="buttons">
               <label class="btn btn-primary ">Yes
               <input type="radio" name="smoke" value="Yes" required/>
               </label>
               <label class="btn btn-primary active">
               <input type="radio" name="smoke" value="No" required/>No
               </label>
            </div>
            <button type="button" class="btn mt-5 mb-5 rounded-pill btn-lg btn-custom btn-block text-uppercase" data-toggle="modal" data-target="#myModal">Submit</button>
         </form>
         <!-- Modal -->
         <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog" role="document">
               <div class="modal-content">
                  <div class="modal-header">
                     <h5 class="modal-title" id="exampleModalLabel">Modal title</h5>
                     <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                     <span aria-hidden="true">&times;</span>
                     </button>
                  </div>
                  <div class="modal-body">
                     <div id="formPreview">
                     </div>
                  </div>
                  <div class="modal-footer">
                     <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                     <button id="formSubmit" type="button" class="btn btn-primary">Submit</button>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
      <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
      <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
      <script>
         $('#myModal').on('shown.bs.modal', function () {

             //1- Get the value from form
             var table = '<table>'
             $("#myform input").each(function(){
                    console.log($(this).attr('name'), $(this).val())
                 table += '<tr><td>'+$(this).attr('name')+'</td><td>'+$(this).val()+'</td></tr>'
             })

             table += '</table>'    

             //2- Set the value in Modal
             $('#formPreview').html(table)

           })

           $('#formSubmit').on('click', function (){
                $("#myform").submit();
           })
      </script>
   </body>
</html>
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement