【问题标题】:Ajax-Request: Passing Dynamic Content to modalAjax-Request:将动态内容传递给模态
【发布时间】:2015-11-19 18:53:22
【问题描述】:

我以前从未使用过 jquery,并努力为我的情况找到解决方案。

在 cockpit.php 页面上,我使用表单从 mysql 数据库中获取一些内容。同时,我可以在 cockpit.php 页面上的 div 中显示此内容。

计划是以模式显示内容。在那里,用户有 10 秒的时间来确认它(在这种情况下,它应该存储到数据库中)。

问题:我尝试了数小时和数天以将内容放入模态。没有机会......关于如何解决这个问题的任何想法?顺便说一句:目前我在倒计时归零后重新加载窗口。在这里,通过 jquery 接近模态也是一个想法。

所以我真的很感激一些提示。谢谢。

最终解决方案: 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();

             }
    });
});

Modal for click-function的相关HTML部分:

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

确认.php

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

所以功能正常...

【问题讨论】:

  • 当您将数据传递给模态时,您能告诉我吗?是在显示模式时想要显示来自 html 的数据,还是当响应从服务器返回并且您想要显示响应时出现问题?
  • 我可以传递数据(来自服务器的响应)并可以在 div 中显示它......这很好。但是当我尝试将这些数据输入模态时,我没有得到响应。至少我在模态中看不到它 - 我当然能够看到模态(没有响应)。重新加载页面中断了一些尝试(因此模式甚至没有打开)
  • 好的,据我了解,服务器响应应该已经按预期存在。我注意到您的代码中有一个潜在的小问题,请尝试在 AJAX 的行尾添加 .show,如下所示: $('#pricing').find('.form_result').html(response).show
  • 还是一样...我想我必须找到一个解决方案才能正确调用模态并同时通过 ajax 获取数据。

标签: javascript php jquery html ajax


【解决方案1】:

我为你做了一个完整的例子,我在我的网站上使用它。这是一个带有链接和模式的 html 文件,所需的 JQuery 和一个简单的 php 代码来模拟服务器响应。它向您展示了如何将数据传递给模态,如何显示模态并显示服务器响应。

只需将文件复制到同一目录并测试它,它对我有用。

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');

             }
       });    
  });

});

函数.php

<?php

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

希望对你有帮助,请随时问我

根据您的评论进行更新

我创建了另一个版本,它将从 html 页面上的表单中获取数据,将其传递给 php,然后在模式窗口上显示来自 php 的结果。它使用不同的 javascript,因为现在我们将表单数据“发布”到 php。以下是文件:

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;  
  });

});

函数.php

<?php

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

【讨论】:

  • 太棒了,这无疑是朝着正确方向迈出的一大步。 javascript 的第二部分非常适合此目的。
  • 只有一个问题:我想获取表单数据(即来自 index html 的表单),将它们传递给 php 并在模式中打开结果。然后第二步是确认这个数据(它是一个报价)并将其保存到数据库中。第二步将使用您的脚本。但是将数据输入到表单中仍然是一个问题。
  • 您的脚本运行良好,但无法解决获取数据的问题。我试过 'var myform = document.getElementById("formid"); var fd = new FormData(myform);'然后是 $.ajax。如何调用并显示成功的变量:?
  • 我在原始答案中添加了修改后的解决方案,请看一下,它与您在之前的评论中描述的一样
  • @mgw 我的回答有帮助吗?我把工作代码放在那里,所以我很好奇我们是否一起取得了一些成就:)
猜你喜欢
  • 2012-11-08
  • 1970-01-01
  • 1970-01-01
  • 2018-09-01
  • 2014-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-20
相关资源
最近更新 更多