doukui4786 2016-08-02 19:42
浏览 74
已采纳

使用php中的ajax将数据插入mysql数据库

I am trying to insert value using ajax in php, but data is not inserted in database. I have taken this code from the questions answered in other question from this site. Can anyone suggest where am I making mistake..?

<script>
  $("#submit").click(function() {
                var name= $("#name").val();
                var password= $("#password").val();

                $.ajax({
                    type: "POST",
                    url: "insert.php",
                    data: "name=" + name+ "&password=" + password,
                    success: function(data) {
                       alert("sucess");
                    }
                });


            });

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>

<?php
    //------insert.php------
     $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "dbname";

    // Create connection
    $conn = new mysqli($servername, $username, $password,$dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 


          $name=$_POST['name'];
            $pass=$_POST['password'];
             $sql= mysqli_query($conn,"INSERT INTO insert_tbl(name,pass) VALUES('".$name."','".$pass."')");

 ?>
  • 写回答

2条回答 默认 最新

  • duanke0555 2016-08-02 19:53
    关注
    <script>
      $("#FORM_ID").submit(function() {
                    var name= $("#name").val();
                    var password= $("#password").val();
    
                    $.ajax({
                        type: "POST",
                        url: "insert.php",
                        data: "name=" + name+ "&password=" + password,
                        success: function(data) {
                           alert("sucess");
                        }
                    });
    
    
                });
    </script>
    

    and also either load

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.0/jquery.min.js"></script>
    

    before your script tag or use

     <script>
    $(document).ready(function(){
          $("#FORM_ID").submit(function() {
                        var name= $("#name").val();
                        var password= $("#password").val();
    
                        $.ajax({
                            type: "POST",
                            url: "insert.php",
                            data: "name=" + name+ "&password=" + password,
                            success: function(data) {
                               alert("sucess");
                            }
                        });
    
    
                    });
            });
        </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目
  • ¥15 利用pthon计算薄膜结构的光导纳