首页 > 解决方案 > 该代码在某些方面不起作用。ajax和php代码中的问题

问题描述

我希望有人在我的 ajax 代码中帮助我。问题是(1)当我尝试在操作中添加 if(isset) 时,如果没有这个,它不会将数据插入数据库,它工作正常,使用 if(isset) 这是我的需要。(2) 插入数据后,成功弹出窗口不起作用。

索引.php

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>AJAX PRACISE</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <!-- FOR INSERT DATA INTO DATABASE -->
    <script type="text/javascript">
      $(document).ready(function(){

        $('#submit').click(function(event){
          event.preventDefault();

          $.ajax({

            url: "action.php",
            method: "post",
            data: $('#myform').serialize(),
            success: function(){
                 swal("Good job!", "You clicked the button!", "success");
            }

          });

        });

      });
    </script>
    <!-- FOR INSERT DATA INTO DATABASE -->

  </head>
  <body>

        <form action="action.php" id="myform" method="post">
          <div class="form-group">
            <label for="exampleInputEmail1">Email address</label>
            <input type="email" class="form-control" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">
            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Password</label>
            <input type="password" class="form-control" name="password" id="exampleInputPassword1">
          </div>

          <button type="submit" class="btn btn-primary" name="insbtn" value="Submit" id="submit">Submit</button>
        </form>

  </body>

</html>

动作.php


$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "ajax");

if (isset($_POST['insbtn'])) {
  $email= $_POST['email'];
  $password= $_POST['password'];

  $sql ="INSERT INTO `user` (`ID`, `email`, `password`) VALUES (NULL, '$email', '$password')";

  $query = mysqli_query($con, $sql);
}

 ?>

我知道这两个问题太基本了,但我是 ajax 新手,这就是我被卡住的原因。谢谢... :)

标签: phpmysqlajax

解决方案


此处$('#myform').serialize()不包含提交按钮,因此您必须在后端签入if (isset($_POST['email']) && isset($_POST['password']))

同样在插入后,回显某些内容并在 ajax 响应中进行比较。

注意您的代码对 SQL 注入开放,因此请保护它

$(document).ready(function(){

        $('#submit').click(function(event){
          event.preventDefault();
          
          console.log($('#myform').serialize());
          
          $.ajax({

            url: "action.php",
            method: "post",
            data: $('#myform').serialize(),
            success: function(resp){
                if(resp == 'success'){
                  swal("Good job!", "You clicked the button!", "success");
                } else {
                  alert('cant insert data');
                }
                 
            }

          });

        });

      });
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>AJAX PRACISE</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>

    <!-- FOR INSERT DATA INTO DATABASE -->
 
 </head>
  <body>

        <form action="action.php" id="myform" method="post">
          <div class="form-group">
            <label for="exampleInputEmail1">Email address</label>
            <input type="email" class="form-control" name="email" id="exampleInputEmail1" aria-describedby="emailHelp">
            <small id="emailHelp" class="form-text text-muted">We'll never share your email with anyone else.</small>
          </div>
          <div class="form-group">
            <label for="exampleInputPassword1">Password</label>
            <input type="password" class="form-control" name="password" id="exampleInputPassword1">
          </div>

          <button type="submit" class="btn btn-primary" name="insbtn" value="Submit" id="submit">Submit</button>
        </form>

  </body>

</html>

你的action.php

$con = mysqli_connect("localhost", "root", "");
mysqli_select_db($con, "ajax");

if (isset($_POST['email']) && isset($_POST['password'])) {
  $email= $_POST['email'];
  $password= $_POST['password'];

  $sql ="INSERT INTO `user` (`ID`, `email`, `password`) VALUES (NULL, '$email', '$password')";

  $query = mysqli_query($con, $sql);

 // after insert echo something
 echo "success";
}

 ?>

推荐阅读