简体   繁体   English

数据未通过jQuery和Ajax方法插入数据库

[英]Data is not inserting into database by jQuery and ajax method

I have to insert data through jQuery and ajax method. 我必须通过jQuery和ajax方法插入数据。 But data is not inserting in database. 但是数据没有插入数据库中。 Pleas help me is this method is wrong? 请帮我这个方法是错误的吗?

Here is my code, 这是我的代码,

form.html form.html

<!DOCTYPE html> <html>
    <head>
        <meta charset="UTF-8">
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script> <!--        <script src="jquery-3.1.0.js" type="text/javascript"></script>-->
        <script type="text/javascript">
            function submitData()
            {
                var name = document.getElementById('name');
                var age = document.getElementById('age');
                var contact = document.getElementById('contact')

                $.ajax({
                    type: 'POST',
                    url: 'data_ins.php',
                    data:{
                     uname:name,
                     uage:age,
                     ucontact:contact
                    },
                    success: function(response){
                        $('#success_para').html("Data inserted");
                    }
                });

                return false;
            }
        </script>
    </head>
    <body>
        <form method="POST" onsubmit="return submitData();">

            name : <input type="text" name="name" id="name"> <br>
            age : <input type="text" name="age" id="age"> <br>
            contact : <input type="text" name="contact" id="contact"> <br>
            <input type="submit" name="submit" >

        </form>

        <p id="success_para"></p>
    </body> </html>

and, data_ins.php 并且,data_ins.php

<?php
       $conn = mysqli_connect("localhost","root","","test_db");
       $name = $_POST['uname'];    $age = $_POST['uage'];    $contact = $_POST['ucontact'];
       if(!$conn)    {
       echo"<script>alert('Database Connection Failed or invalid connection');</script>";    }
       else    {
       $sql = "insert into records (name, age, contact) values ('$name', '$age', '$contact')";    mysqli_query($conn, $sql);
    mysqli_error($conn);    }
    ?>

Please let me know what's wrong in my code. 请让我知道我的代码有什么问题。 Thanks, in advance. 提前致谢。

var name = document.getElementById('name').value;
var age = document.getElementById('age').value;

Your "submit" button causes page reloading, so you'll not be able to see the result of a query in your onSuccess. 您的“提交”按钮会导致页面重新加载,因此您将无法在onSuccess中看到查询结果。

<input type="button" name="submit" onclick="submitData();" >

Have you checked your response? 您检查了您的回复吗? You can also view it in browser's inspector. 您也可以在浏览器的检查器中查看它。

                success: function(response){
                    $('#success_para').html(response);
                }

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

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