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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/38729177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 21:46:51  来源:igfitidea点击:

Insert data into mysql database using ajax in php

javascriptphpjquerymysqlajax

提问by Dhruvi Mistry

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..?

我正在尝试在 php 中使用 ajax 插入值,但数据没有插入到数据库中。我从本网站其他问题中回答的问题中获取了此代码。谁能建议我在哪里犯错..?

<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."')");

 ?>

回答by Tsotne Kekelia

<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>

回答by Sanju Yadav

This is html form for insert data

这是插入数据的html表单

<form id="frmrecord" method="post">
    <input type="text" name="txtusermame" />
    <input type="password" name="txtpassword" />
    <input type="submit" value="Insert" />
</form>

Use this code for call insert.php file to insert data

使用此代码调用 insert.php 文件插入数据

jQuery(document).ready(function ($) {

    $("#frmrecord").submit(function (event) {
                event.preventDefault();
                //validation for login form
        $("#progress").html('Inserting <i class="fa fa-spinner fa-spin" aria-hidden="true"></i></span>');

            var formData = new FormData($(this)[0]);
            $.ajax({
                url: 'insert.php',
                type: 'POST',
                data: formData,
                async: true,
                cache: false,
                contentType: false,
                processData: false,
                success: function (returndata) 
                {
                    //show return answer
                    alert(returndata);
                },
                error: function(){
                alert("error in ajax form submission");
                                    }
        });
        return false;
    });
});

After calling file you can receive data in php file Insert.php

调用文件后,您可以在 php 文件 Insert.php 中接收数据

<?php 
  $usernmae=$_POST['txtusername']; 
  $password=$_POST['password']; 
  $sql= mysqli_query($conn,"INSERT INTO insert_tbl(name,pass) 
    VALUES('".$usernmae."','".$password."')");
 ?>

Download Demo

下载演示