1

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

 ?>
Dhruvi Mistry
  • 124
  • 1
  • 3
  • 13
  • Do you see your success `alert()` message? – showdev Aug 02 '16 at 19:46
  • no..I can not..@showdev – Dhruvi Mistry Aug 02 '16 at 19:47
  • 1
    It's possible that your form is submitting via its default action before the AJAX can fire. Try `return false;` at the end of your `click` handler to [prevent the default action](http://stackoverflow.com/questions/1357118/event-preventdefault-vs-return-false). Also, do you see any errors in your browser console or server logs? – showdev Aug 02 '16 at 19:48
  • **html** `alert("sucess");` to `alert(data);` / **insert.php**: where a return? to success, add later $sql is `echo "success";` – KingRider Aug 02 '16 at 19:50
  • @showdev do mean adding return false in $("#submit").click(function() {}); or should I add another js.? ..nop there is no error..that's the main issue.. – Dhruvi Mistry Aug 02 '16 at 19:51
  • @DhruviMistry Yes, inside that `function(){}`. At the end of the function. – showdev Aug 02 '16 at 19:54

2 Answers2

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

This is html form for insert data

<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

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 
  $usernmae=$_POST['txtusername']; 
  $password=$_POST['password']; 
  $sql= mysqli_query($conn,"INSERT INTO insert_tbl(name,pass) 
    VALUES('".$usernmae."','".$password."')");
 ?>

Download Demo

dhilt
  • 15,987
  • 7
  • 58
  • 75