php 如何将表单提交到 Bootstrap Modal(将 POST 方法发送到 Modal)Laravel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29066537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:13:33  来源:igfitidea点击:

How to Submit Form into Bootstrap Modal (send POST method into Modal) Laravel

phptwitter-bootstrapbootstrap-modal

提问by ErcanE

I have been trying for 2 days but still no luck!

我已经尝试了 2 天,但仍然没有运气!

I want to

我想要

  • Submit Form from index.php to result.php
  • Show result.php inside Modal while index.php is open! (without closing index.php)
  • 从 index.php 提交表单到 result.php
  • 在 index.php 打开时在 Modal 中显示 result.php!(不关闭 index.php)

here is example code!

这是示例代码!

index.php

索引.php

<form id="myform" method="post" action="result.php" target="_blank">
<input type="text" name="userId" id="userId"/>
<input id="button" type="submit"/>
</form>

result.php

结果.php

    <div id="resultModal" class="modal fade" tabindex="-1">
      <div class="modal-dialog">
        <div class="modal-content">
             <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">
                 <i class="fa fa-times-circle"></i>ESC</button>
                 <h4 class="modal-title">Show Result </h4>
            </div>
            <div class="modal-body">



            </div>

        </div>
    </div>
   </div>

In Modal body

在模态体中

<?php  $selectedId = $_POST['userId']; 
    echo  $selectedId; 
?>

And JQuery

和 JQuery

<script type="text/javascript">

    $('#myForm').on('submit', function(ev) {
        var userId = $('#userId').find("input").val();
        $.ajax({
            type: 'POST',
            url : $(this).attr('action'),
            data: userId,
            success: function () {
              // alert('form was submitted');
            }
          });
});
</script>

回答by Aleksandar Miladinovic

Well it has taken me some time but I think I found an answer to your question, or at least this solution can give you a good clue on how to continue with what you are doing.

好吧,我花了一些时间,但我想我找到了您问题的答案,或者至少这个解决方案可以为您提供关于如何继续您正在做的事情的好线索。

First index.php: Here you need to have your form with an input field and one button, which we will call modal, and submit form (using Ajax for post)

第一个 index.php:在这里你需要有一个带有输入字段和一个按钮的表单,我们称之为模态,并提交表单(使用 Ajax 进行发布)

<form id="form" method="post">
    <div id="userDiv"><label>UserId</label>
         <input type="text" name="userId" id="userId" placeholder="UserId"/> <br></div>
    <button type="button" id="btn" class="btn btn-info" data-toggle="modal" data-target="#myModal">Send Data</button>
</form>

Then you need a modal where you will put content from remote page. In modal-body you add one more div tag with id="bingo" to locate him easy :) like this:

然后您需要一个模式,您可以在其中放置来自远程页面的内容。在 modal-body 中,你再添加一个带有 id="bingo" 的 div 标签来轻松定位他:) 像这样:

<div class="modal fade" id="myModal" 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-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">MyModal</h4>
      </div>
      <div class="modal-body">
        <div id="bingo"></div>

      </div>
    </div>
   </div>
</div>

This page also needs to have a script tag which will do the job. Importantit must be placed afterthe script tag where you load the jquery file.

这个页面还需要一个脚本标签来完成这项工作。重要的是它必须放在您加载 jquery 文件的脚本标记之后

<script>
    $(document).ready(function(){
        $("#btn").click(function(){
            var vUserId = $("#userId").val();
         if(vUserId=='')
         {
             alert("Please enter UserId");
         }
         else{
            $.post("result.php", //Required URL of the page on server
               { // Data Sending With Request To Server
                  user:vUserId,
               },
         function(response,status){ // Required Callback Function
             $("#bingo").html(response);//"response" receives - whatever written in echo of above PHP script.
             $("#form")[0].reset();
          });
        }
     });
   });
</script>

And last but not the least result.php:

最后但并非最不重要的 result.php:

<?php
   if($_POST["user"])
   {
        $user = $_POST["user"];
       // Here, you can also perform some database query operations with above values.
       echo "Your user id is: ". $user;
  }
?>

P.S. I hope I didn't mess somewhere with ids, variables or similar because I tried to adjust the solution to your example. I hope this is what you need, or at least this will be a clue to accomplish your task. Still think that this could be done on one page but it was interesting for me to try to find a way to make this work... GL!

PS 我希望我没有在某处弄乱 id、变量或类似的东西,因为我试图根据你的例子调整解决方案。我希望这是您所需要的,或者至少这将是完成任务的线索。仍然认为这可以在一页上完成,但尝试找到一种方法来完成这项工作对我来说很有趣...... GL!