简体   繁体   English

模态表单不使用jQuery提交

[英]modal form not submit using jquery

I was looking for a way to keep my modal form open after submit and I manage to do that but now my problem is the submit form is not submiting the value. 我一直在寻找一种方法,使提交后的模式表单保持打开状态,但我设法做到了,但是现在我的问题是提交表单没有提交值。 Is there somthing wrong with my jQuery? jQuery有什么问题吗?

Here is my code: 这是我的代码:

<form class="needs-validation" id="contact-form" action="index13.php" method="post" novalidate>
    <div class="row" style="margin-left: 300px;">
        <label>Date:</label>  
        <span id="date1" name="date"></span>
    </div>
    <div class="row" style="margin-left: 300px;">
        <label>Time:</label> 
        <span id="clock1" name="time"></span>
    </div>  
    <div class="md-form form-sm mb-5" style="margin-top: 05px;">
        <i class="fa fa-user prefix"></i>
        <input type="text" id="username2" name="username2" class="form-control form-control-sm" required>
        <label for="modalUsername">Username</label>
        <div class="invalid-feedback" style="margin-left: 30px">
            Enter username.
        </div>
    </div>
    <div class="md-form form-sm mb-4">
        <i class="fa fa-lock prefix"></i>
        <input type="password" id="password1" name="password1" class="form-control form-control-sm" required>
        <label for="modalPassowrd">Password</label>
        <div class="invalid-feedback" style="margin-left: 30px">
            Enter password.
        </div>
    </div>
    <div class="text-center mb-3">
        <button type="submit" id="submit1" name="submit" class="btn btn-info">Log out <i class="fa fa-sign-out ml-1"></i></button>
    </div>

    <!--Footer-->
    <div class="modal-footer">
        <div class="options text-center text-md-right mt-1">
            <p class="font-small blue-text d-flex justify-content-end" data-toggle="modal" data-target="#modalPush">
                Forgot <a href="#" class="blue-text ml-1" style="margin-right: 310px;">Password?</a>
            </p>
        </div>
    </div>
</form>

php inside index13: PHP里面的i​​ndex13:

<?php
    include_once ('connection.php');

    if (isset($_POST['submit'])) {
        $username2 = $_POST['username2'];
        $password1 = $_POST['password1'];
        $time = date("H:i:s");
        $sql = "select * from visitor_att where uname = '$username2' and pass = '$password1'";
        $result = mysqli_query($conn,$sql);
        $count = mysqli_num_rows($result);
        if ($count == 0) {
            echo "No Results";
        } else {
            while ($row = mysqli_fetch_array($result)) {
                $username2 = $row['uname'];
                $password1 = $row['pass'];
                $fname=$row['fname'];
                $lname=$row['lname'];   
                $InsertSql = "Update visitor_att set timeout = '$time' where uname = '$username2' and pass = '$password1'";
                $res = mysqli_query($conn, $InsertSql); 
            }
        }
    }
?>

Here is the script is use to prevent my modal form from refreshing after submit: 这是用于防止提交后的模态表单刷新的脚本:

    <script>    
$(function() {


 var frm = $("#contact-form");

    frm.submit(function (e) {

        e.preventDefault();

        $.ajax({
            type: frm.attr("method"),
            url: frm.attr("action"),
            data: frm.serialize(),
            success: function (data) {
                console.log('Submission was successful.');
                console.log(data);
            },
            error: function (data) {
                console.log('An error occurred.');
                console.log(data);
            },
           });
        });
    });
</script>

Hope you can help me, thanks. 希望你能帮助我,谢谢。

Updated my jQuery code, still not submiting. 更新了我的jQuery代码,但仍未提交。 i try to run it on console, it says submission was successful but no data 我尝试在控制台上运行它,说提交成功,但是没有数据

The Problem is with server side code ie PHP code. 问题在于服务器端代码,即PHP代码。

On server side PHP code you have written a condition ie if the request contains $_POST['submit'] key then insert the data in the database otherwise do nothing. 在服务器端PHP代码上,您编写了一个条件,即,如果请求中包含$_POST['submit']键,则将数据插入数据库中,否则不执行任何操作。

Here is the JSFiddle where i tried this code https://jsfiddle.net/g2b4ujzp/1/ . 这是JSFiddle,我在这里尝试过此代码https://jsfiddle.net/g2b4ujzp/1/ Go to the fiddle open developer tools of the browser go to network tab, click the log out button. 转到打开浏览器的小工具,转到“网络”选项卡,单击“注销”按钮。 There you will see that there is no value with the key submit in the request. 在那里,您将看到请求中的密钥submit没有任何价值。

You are serializing the form and sending the data but the form.serialize() method does not serializes the button elements. 您正在序列化表单并发送数据,但是form.serialize()方法不会序列化按钮元素。 hence the submit named input doesn't serialize causing the condition isset($_POST['submit']) false on server side. 因此, submit命名的输入不会序列化,从而导致服务器端条件isset($_POST['submit']) false。

I would suggest you to write an else code block for the condition isset($_POST['submit']) to debug by printing some error. 我建议您为条件isset($_POST['submit'])写一个else代码块,以通过打印一些错误进行调试。 And on client add a hidden input element named submit with some value. 然后在客户端上添加一个名为submit的隐藏输入元素,并带有一些值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM