php ajax 插入显示空白响应但没有错误

php ajax insert shows blank response but no error

我只是通过 ajax 形式将演示数据插入数据库。 ajax 没有显示错误,但在我使用 console.log(response);.

时显示空白响应

Php代码-

<form role="form" id="signup_form">
                            
            <div class="row">

            <div class="col-lg-6 col-md-6">
            <div class="form-group">
              <label class="required">First Name</label>
                 <div class="input-with-icon">
                   <input type="text" class="form-control" placeholder="Your First Name"
                     name="f_name" id="f_name" required>
                  <i class="ti-user"></i>
                      </div>
              </div>
         </div>
         </div>

           <div class="form-group">
            <button type="submit" id="submit" name="submit"
                class="btn btn-md full-width btn-theme-light-2 rounded">Sign Up</button>
                    </div>

 </form>

ajax代码-

 <script>
$(document).ready(function() {
    $("#submit").on("click", function(e) {
        e.preventDefault();

        var f_name = $('#f_name').val();
        console.log(f_name);  // <--- but this shows the typed data from the form



        $.ajax({
            url: "homes/php/user-signup.php",
            method: "POST",
            data: {

                "f_name": f_name

            },
            success: function(response) {
                $('#signup_form')[0].reset();
                $('#signup').modal('hide');
                console.log(response); // <--- this shows blank response



            },
            error: function(response) {
                console.log(response);


            }
        });
    });
});
</script>

mysql--

<?php
session_start();
include_once 'conf.php';

if(isset($_POST['submit'])){
            $f_name=$_POST['f_name'];

              $query = "INSERT INTO `user_info`(`first_name`) VALUES (':f_name')";

            $stmt = $dbcon->prepare($query);
            $stmt->bindParam(':f_name',$f_name);
            $stmt->execute();

  
 
    }
 ?>

将此 $_POST['submit'] 更改为 $_POST['f_name']。它会起作用。你没有 post submit ajax 请求。

<?php
session_start();
include_once 'conf.php';

    if(isset($_POST['f_name'])){
            $f_name=$_POST['f_name'];

              $query = "INSERT INTO `user_info`(`first_name`) VALUES (:f_name)";

            $stmt = $dbcon->prepare($query);
            $stmt->bindParam(':f_name',$f_name);
            

            if($stmt->execute()){   
                echo "Signup Successfull";
            }else{
               echo "Insert failed. DB error";
            }
 
    }
 ?>

您在响应中回显脚本块,这在 AJAX 上下文中没有意义。

此外,您 没有 回显除纯成功案例之外的任何内容,并且您正在检查 $_POST 中的错误变量 - 您的 AJAX代码只发送“f_name”到服务器,没有别的。

您的 SQL 查询中也有拼写错误 - :f_name 不应包含在 SQL 字符串中的引号中。

这应该会给您更多反馈:

<?php
session_start();
include_once 'conf.php';

if(isset($_POST['f_name']))
{
    $query = "INSERT INTO `user_info`(`first_name`) VALUES (:f_name)";
    $stmt = $dbcon->prepare($query);
    $stmt->bindParam(':f_name', $_POST['f_name']);
            
    if($stmt->execute())
    {   
       echo "Signup Successful";
    }
    else 
    {
      echo "Error - query did not execute correctly. See error logs for details";
      //N.B. You should put some code here to log the real error to a file
    }
}
else
{
  echo "Missing parameters, please try again";
}
?>

如果之后仍然没有输出任何东西,请进行更多调试 - 检查浏览器的控制台和网络工具以查看请求是否成功。