繁体   English   中英

使用ajax用php mysqli更新模态形式的数据

[英]using ajax to update data on modal form with php mysqli

我设法防止使用ajax jQuery提交值后不关闭模态表单而无需刷新页面,

但是现在我的问题是当我填写模态表单并单击提交以更新数据库中的数据时,我的数据库没有更新。

我认为我的Ajax代码有问题。

希望您能够帮助我。 谢谢。

这是我的代码:

 <form class="needs-validation " id="contact-form" action="index13.php" method="post" novalidate >
</form>

这是我的index13.php代码:

<?php

 include_once ('connection.php');

if(isset($_POST['submit1']))
{

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

        }

         }



  }
?>

这是我的ajax jQuery代码:

<script>    
$(function() {


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

    frm.submit(function (e) {

        e.preventDefault();

        $.ajax({
            type: frm.attr("post"),
            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>

当我在控制台上运行它时,表明提交成功。 但没有数据插入数据库中。

看起来您需要在表单中添加id属性,因为您的ajax调用已绑定到表单...

<form id="contact-form" class="needs-validation" action="index13.php" method="post" novalidate >

暂无
暂无

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

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