C'est dans la balise de script

$(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',
            });

        });

    });

C'est mon 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);


    }

Aucune entrée dans la base de données, ajax non appelé.

0
prerak rathore 13 avril 2018 à 14:50

5 réponses

Meilleure réponse

Essaye ça

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


    }
0
Luca Kiebel 13 avril 2018 à 12:00
<?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>
0
Mahendran 13 avril 2018 à 12:35

Il manque une balise #

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

Et ajouter

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

Après la variable data

0
Rahul Shrivastava 13 avril 2018 à 12:08

Mettre à jour votre code jQuery de telle manière

$(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',
            });

        });

    });
0
Malik Muhammad Bilal Awan 13 avril 2018 à 12:38

Essaye ça,

<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);
            }
        });

    });

});    

Page Comment_load.php

<?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
user8680667user8680667 13 avril 2018 à 12:21