-2

I am creating a page where clicking on the post button it will prepend below the input and store input data to database but i'm encountered with above problem! Below I m sharing my code

<script
  src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
  <script>
  function get(){
      var input = $("#cmt").val();
      $.post('postToPage.php',{comment:input},function(output){
        $("#post-data").prepend(output+"<br><hr>")});

  }


      </script>
<div class="container"  style="margin-top:20px;">
    <div class="form-group">
      <form name="frm" >

      <input type="text" class="form-control" name="cmt" id="cmt" placeholder='Why So Empty!! Please Post Something'>
      <input class="btn btn-primary" type="button"  onclick="get();" value="Post">

      </form>
    </div>

    <br>
    <br>
<!--Here the post data will be visible-->
    <div id="post-data">

    </div>
</div>

the postToPage.php file

<?php 
require_once('db.php');
$cmt = $_POST['comment'];
$query = "INSERT INTO post (id,body) VALUES ('','$cmt')";
mysqli_query($conn,$query);
echo $cmt;
?>

the database config file db.php

<?php

$conn = mysqli_connect("localhost","root","","signup_data");

?>

Please reveal the errors..Thank you

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
Aditya Mishra
  • 19
  • 1
  • 7

1 Answers1

1

try below code

 <script>
  function get(){

      var input = $("#cmt").val();
      $.ajax({
                        type: 'post',
                        url: 'postToPage.php',
                        data: {
                            comment: input,

                        },
                        success: function (response) {
                             document.getElementById("post-data").innerHTML = response;
                        }
                    });


  }


      </script>
sam
  • 167
  • 8
  • check your sequence of js files included it should be based on versions of jquery – sam Jun 21 '18 at 11:35
  • Sorry guyzz i found my problem its because i have added jquery.slim.js at footer.php thats why its not working i just replaced it with jquery.min.js and its working now....thankss everyone for helping me on this – Aditya Mishra Jun 21 '18 at 11:36