0

this is in script tag

$(document).ready(function(){
        $("#sub").click(function(){
            var name =  $("#name").val();
            var email =  $("#email").val();
            var comment =  $("comment").val();
            //alert(email);

            $.ajax({
                async:true,
                type:"POST",
                url: "comment_load.php",
                data:{
                    'done':1,
                    'name':name,
                    'email':email,
                    'comment':comment
                },
                enctype: 'multipart/form-data',
            });

        });

    });

this is my comment_load.php

include 'conn.php';

    if(isset($_POST['submit'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        $query = "insert into comment(name,email,comment) values('$name','$email','$comment')";
        $result = mysqli_query($conn,$query);


    }

no entry in database ,ajax not called.

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 3
    You are not sending `submit` in the payload to your php script – Martin Adámek Apr 13 '18 at 11:52
  • can you `console.log` in the ajax post to see if it goes through/which part breaks? – snack_overflow Apr 13 '18 at 11:53
  • 2
    Your code is vulnerable to [SQL injection attacks](https://en.wikipedia.org/wiki/SQL_injection). You should use prepared statements with bound parameters, via either the [mysqli](https://secure.php.net/manual/en/mysqli.prepare.php) or [PDO](https://secure.php.net/manual/en/pdo.prepared-statements.php) driver. [This post](https://stackoverflow.com/q/60174/6634591) has some good examples. – Luca Kiebel Apr 13 '18 at 11:56
  • Dont know your code, but isnt there a '#' missing in front of comment in the .click part? – Celebrombore Apr 13 '18 at 12:00
  • Also check the network tab in the inspector if your ajax request is there and what is the response. – xpy Apr 13 '18 at 12:05
  • thanks all for your suggestions .......i used $_POST['submit'] but correct way is $_POST['done']...got the solution – prerak rathore Apr 13 '18 at 12:35

5 Answers5

0

try this

include 'conn.php';

    if(isset($_POST['done'])){
        $name = $_POST['name'];
        $email = $_POST['email'];
        $comment = $_POST['comment'];
        $query = "insert into comment(name,email,comment) values('$name','$email','$comment')";
        $result = mysqli_query($conn,$query);


    }
Luca Kiebel
  • 9,790
  • 7
  • 29
  • 44
Rp9
  • 1,955
  • 2
  • 23
  • 31
0

there is a missing # tag

var comment =  $("comment").val();

and add

success: function(data){
    $("target Id").html(data);
}

after data variable

Rahul
  • 1,617
  • 1
  • 9
  • 18
0
<?php
/*
$conn = mysqli_connect("localhost","root","") or die (mysqli_error());
mysqli_select_db($conn,"comment") or die (mysqli_error());
*/
//ajax_insert.php file
include('dbconnection.php');

$query = "insert into comment(name,email,comment) values('".$_POST["name"]."','".$_POST["email"]."','".$_POST["comment"]."')";
if ($conn->query($query) === TRUE) {
echo 1;
} 
else {
echo 0;
}
?>   
 <html>
    <head>
    <title>insert data in database using Ajax</title>
    <script>
    $(document).ready(function(){
        $("#submit").click(function(){
            var name =  $("#name").val();
            var email =  $("#email").val();
            var comment =  $("#comment").val();
            $.ajax({
                type: "POST",
                dataType: 'JSON',
                url: "ajax_insert.php",
                data: "name="+name+"&email="+email+"&comment="+comment,
                success: function(data){
                    if(data==1)
                    {   alert('Success');}
                else{
                    alert('Failure');
                    }
                }
            });
        });
    });     
    </script>
    </head>
    <body>

    <div id="main">
    <h1>Insert data into database using Ajax</h1>
    <div id="login">
    <h2> Form</h2>
    <hr/>
    <form action="" method="post" name="ajaxForm">
    <label>Name :</label>
    <input type="text" name="name" id="name" required="required" /><br /><br />
    <label>Email :</label>
    <input type="email" name="email" id="email" required="required" /><br/><br />
    <label>Comment :</label>
    <input type="text" name="comment" id="city" required="required" /><br/><br />
    <input type="button" value="Submit " name="submit"/><br />
    </form>
    </div>
    </div>
    </body>
    </html>
Mahendran
  • 16
  • 2
0

Try This,

<script type="text/javascript" 
 src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"> 
</script>
<input type="text" id="name" placeholder="Name"/>
<input type="email"  id="email" placeholder="Email"/>
<textarea id="comment"></textarea>
<input type="button" name="submit" id="sub" value="submit">
<script type="text/javascript">
  $(document).ready(function(){
    $("#sub").click(function(){
        var name =  $("#name").val();
        var email =  $("#email").val();
        var comment =  $("#comment").val();

        $.ajax({
            async:true,
            type:"POST",
            url: "comment_load.php",
            data:{
                'done':1,
                'name':name,
                'email':email,
                'comment':comment
            },
            enctype: 'multipart/form-data',
            success: function(resp){
                console.log(resp);
            }
        });

    });

});    

Comment_load.php page

<?php
//include 'conn.php';
if(isset($_POST['done'])){
$name = $_POST['name'];
$email = $_POST['email'];
$comment = $_POST['comment'];
echo json_encode($name);
//$query = "insert into comment(name,email,comment) 
values('$name','$email','$comment')";
//$result = mysqli_query($conn,$query);
}
0

update your jQuery code in such a way

$(document).ready(function(){
        $("#sub").click(function(){
            var name =  $("#name").val();
            var email =  $("#email").val();
            var comment =  $("comment").val();
            var submit=  $("#sub").val();
            //alert(email);

            $.ajax({
                async:true,
                type:"POST",
                url: "comment_load.php",
                data:{
                    'done':1,
                    'name':name,
                    'email':email,
                    'comment':comment,
                    'submit' : submit
                },
                enctype: 'multipart/form-data',
            });

        });

    });