html 如何将PHP表单放入模态

nqwrtyyt  于 2022-11-27  发布在  PHP
关注(0)|答案(3)|浏览(167)

我有一个模态表单的HTML代码(使用Bootstrap)

<div class="modal fade" id="loginModal" tabindex="-1" role="dialog" aria-labelledby="edit-user-modal-label" aria-hidden="true">
    <div class="modal-dialog">

      <!-- Modal content-->
      <div class="modal-content">

          <form name="login_form" action="test.php" method="post" role="form">
            <div class="modal-header">
              <button type="button" class="close" data-dismiss="modal">&times;</button>
              <h4 class="modal-title">Login</h4>
            </div>
            <div class="modal-body">
                <div class="form-group">                  
                      <label for="email">Email:</label>
                      <input type="email" class="form-control" id="email" placeholder="Enter email">                  
                </div>

                <div class="form-group">
                      <label for="pwd">Password:</label>
                      <input type="password" class="form-control" id="pwd" placeholder="Enter password">
                </div>
            </div>

            <div class="modal-footer">
                <input id="submit" name="submit" type="submit" value="Ok" class="btn btn-primary">
            </div>

          </form>

      </div>

    </div>
</div>

问题是,当我点击“ok”按钮时,什么都没有发生。这是“test.php”文件(我只是用来看看它是否工作)

<html>
    logged
</html>

我是新的引导程序,所以我不太清楚为什么它不能作为一个普通的HTML+CSS页面工作。谢谢你的帮助!

编辑

我找到了这个 AJAX 代码,试图把它改编成我的代码,但仍然没有工作。

$(document).ready(function () {
            $("input#submit").click(function(){
                $.ajax({
                    type: "POST",
                    url: "test.php", // 
                    data: $('form.login_form').serialize(),
                    success: function(msg){
                        $("#form-content").modal('hide');   
                    },
                    error: function(){
                        alert("failure");
                    }
                });
            });
        });

现在,我只想在按下按钮后转到另一个PHP页面(我还没有编写登录验证代码)。

w1e3prcc

w1e3prcc1#

我知道发生了什么,只要加上这个

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0-alpha1/jquery.js"></script>
<script>
    $(document).ready(function(){

    $('#submit').click(function(){

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

</script>
mv1qrgav

mv1qrgav2#

最明显的问题是,当您执行 AJAX 调用时,您实际上并没有阻止表单提交。您并不想提交页面,而是等待ajax请求结束,然后在success回调中处理响应。由于您使用的是jQuery,最简单的方法是从事件回调中返回false

$(document).ready(function () {
            $("input#submit").click(function(){
                $.ajax({
                    type: "POST",
                    url: "test.php", // 
                    data: $('form.login_form').serialize(),
                    success: function(msg){
                        $("#form-content").modal('hide');   
                    },
                    error: function(){
                        alert("failure");
                    }
                });
                return false;//stop form submission
            });
        });

jQuery的return false相当于vanilla-JS的return false

eventObj.preventDefault();//do not handle event in a normal fashion
eventObj.stopPropagation();//do not propagate event to other handlers

话虽如此,您的选择器和事件绑定还可以做得更好,我将为表单(<form id='modalformid'>)设置一个ID,并绑定 submit 事件(它可以通过用户按enter来触发),然后绑定处理程序,如下所示:

$('#modalformid').on('submit', function()
{
    var frm = $(this);//reference to the form that is submitted
    $.ajax({
        type: "POST",
        url: "test.php",
        data: frm.serialize(),//serialize correct form
        success: function(msg) {
            //the response from the server is in msg here!
            $("#form-content").modal('hide');   
        },
        error: function(){
            alert("failure");
        }
    });//your ajax call here
    return false;
});

我还会检查控制台,以确保在调用$(document).ready()之前 * 包括所有JS依赖项(即jQuery),并仔细检查名称冲突(在控制台console.log($)console.log($ === jQuery)中)。
最后,$('form.login_form')选择器是不可靠的:根据定义,ID是唯一的。类不是。理论上,使用$('form.login_form')可以匹配多个表单。经验法则:类对于CSS规则很有用,而对于JS事件处理(例如委托)则不太有用,如果你想处理单个表单/元素:请使用ID。

cedebl8k

cedebl8k3#

我同意Elias的观点。在 AJAX 调用的情况下,你不想提交表单,因为页面会重新加载,你永远不会得到响应。
在JS中,您绑定了一个click事件,这很好,但是您的inputtype="submit",这将导致一个问题。
第一件事,把它拿掉。

<input id="submit" name="submit" value="Ok" class="btn btn-primary">

其次,在JQuery中,您引用了一个无效的表单选择器。

$('form.login_form').serialize()

没有login_form类。由于 AJAX 为您处理请求,我建议使用id代替并最小化属性。

<form id="login_form" role="form">

根据您提供的HTML,我必须假设您想要隐藏#loginModal而不是#form-content

$(document).ready(function () {
    $("input#submit").click(function(){
        $.ajax({
            type: "POST",
            url: "test.php",
            data: $('form#login_form').serialize(),
            success: function(msg){
                $("#loginModal").modal('hide');
                // show validation error if applicable   
            },
            error: function(){
                alert("failure");
            }
        });
    });
});

相关问题