简体   繁体   中英

Ajax-Request: Passing Dynamic Content to modal

I never used jquery before and tried hard to find a solution for my case.

On the cockpit.php page I use a form to get some content from a mysql database. Meanwhile I am able to show this content in a div on the cockpit.php page.

The plan is to show the content in a modal. There the user has 10 seconds to confirm it (in this case it should be stored to the database).

The problem: I tried it for hours and days to get the content into the modal. No chance... Any idea on how to solve this? Btw: At the moment I reload the window after the countdown reaches zero. Here it would also be an idea to just close to modal via jquery.

So I would really appreciate some hints. Thx.

Final Solution: modal.js

$(function(){

  $("#pricing").submit(function() {
      $.ajax({
             type: "POST",
             url: $(this).attr('action'),
             data: $(this).serialize(),
             dataType: 'json',
             success: function(data)
             {                 
             $('#myModal').find('#a').text(data.a);
             $('#myModal').find('#b').text(data.b);     
             $('#myModal').find('#c').text(data.c);
             $('#myModal').find('#d').text(data.d);
             $('#myModal').find('#e').text(data.e);
             $('#myModal').find('#f').text(data.f);
             $('#a2').val($(this).data('a'));
             $('#myModal').modal('show');
             }
           });
      return false;  
  });

});


   $("#confirm").click(function(){
    var data = {
    a: $('#a').text(),
    b: $('#b').text(),
    c: $('#c').text()
};    
    $.ajax({
        url: 'confirm.php',
        type: "POST",
        data: data,
        dataType: 'json',
        success: function(confirm)  {                 
             window.location.reload();

             }
    });
});

relevant HTML-part of the Modal for click-function:

 <div class="alert hidden" role="alert" id="modalAlert"></div>
                <input type="hidden" id="confirmmodal" name="confirmmodal" value="" /> 
                    </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-ar btn-default" data-dismiss="modal">Cancel</button>
            <button type="button" class="btn btn-ar btn-primary" id="confirm">Confirm</button>
          </div>

confirm.php

<?php
    $a = $_POST['a'];
    // do what you want
    $confirm = array('message' => $a);
    echo json_encode($confirm);

So the functionality works fine...

I made a full example for you, I use it on my website. This is a html file with a link and a modal, the required JQuery and a simple php code to simulate the server response. It shows you, how you can pass data to the modal, how to show the modal and display the server response.

Just copy the files into the same directory and test it, it works for me.

index.html

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="/script.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    </head>
    <body>

        <div class="container">

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal" aria-hidden="true">
          <div class="modal-dialog ">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalTitle"></h4>
              </div>
              <div class="modal-body">
                <div class="alert hidden" role="alert" id="modalAlert"></div>
                <input type="hidden" id="myModalID" name="myModalID" value="" />
                <p>Modal example.</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-danger" id="myModalButton">Submit</button>
              </div>
            </div>
          </div>
        </div>

            <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
                <a class="openModalLink" href="/" data-id="1234" data-name="Stackoverflow answer">
                  <span> Click me to open modal</span>
                </a>
            </div>          
        </div>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

    </body>
</html>

script.js

$(function(){

  $(".openModalLink").click(function(e) {
      e.preventDefault();       
      $("#myModalTitle").html('Hello, my name is '+$(this).data('name')+'!');
      $("#myModalID").val($(this).data('id'));

      $('#myModal').modal('show');

  });

  $("#myModalButton").click(function(){
      $.ajax({
             url: '/function.php',
             data: {
               id: $("#myModalID").val()
             },
             dataType: 'json',
             success: function(data)
             {                 

              $('#myModal').find('#modalAlert').addClass('alert-success');
              $('#myModal').find('#modalAlert').html(data.message).show; 
              $('#myModal').find('#modalAlert').removeClass('hidden');

             }
       });    
  });

});

function.php

<?php

    echo json_encode(array('message' => 'You submitted this id: ' . $_GET['id']));

hope this helps, feel free to ask me

UPDATE BASED ON YOUR COMMENT

I created another version that will take data from the form on the html page, pass it to php and then display the results from php on the modal window. It uses a different javascript, because now we are "post"-ing the form data to php. Here are the files:

index.html

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="/script.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">
    </head>
    <body>

        <div class="container">

        <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal" aria-hidden="true">
          <div class="modal-dialog ">
            <div class="modal-content">
              <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalTitle"></h4>
              </div>
              <div class="modal-body">
                <div class="alert hidden" role="alert" id="modalAlert"></div>
                <input type="hidden" id="myModalID" name="myModalID" value="" />
                <p>Modal example.</p>
              </div>
              <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                <button type="button" class="btn btn-danger" id="myModalButton">Submit</button>
              </div>
            </div>
          </div>
        </div>

            <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="row">
              <div class="col-md-8 col-md-offset-0">
               <form class="form-inline" id="myForm" action="/function.php" method="post">
                 <div class="form-group">
                   <label for="myInput">Input data</label>
                   <input type="text" class="form-control" id="myInput" name="myInput" placeholder="Enter data here">
                 </div>
                 <button type="submit" class="btn btn-primary">Save</button>
               </form>
              </div>
            </div>          
            </div>          
        </div>

        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>

    </body>
</html>

script.js

$(function(){

  $("#myForm").submit(function(event) {

      $.ajax({
             type: "POST",
             url: $(this).attr('action'),
             data: $(this).serialize(),
             dataType: 'json',
             success: function(data)
             {                 

                //display data...
                $('#myModal').find('#modalAlert').addClass('alert-success');
                $('#myModal').find('#modalAlert').html(data.message).show; 
                $('#myModal').find('#modalAlert').removeClass('hidden');

                $('#myModal').modal('show');
             }
           });

      return false;  
  });

});

function.php

<?php

echo json_encode(array('message' => 'You submitted this data: ' . $_POST['myInput']));

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