0

I am trying to build a comment system for my website using Ajax, jQuery and PHP. My site has the lot of queries, how can I submit comments every query separate?

Ajax code

 $(document).ready(function()
   {
   $("#comq").click(function() {
       var comment=$("#comment").val();
               var qid=$("#qid").val();
       $.ajax({
        cache:false,
        type:"post",         
         url:"jquery.php",
data:{comments:comment, qid:qid},
        success:function(data)
        {
    $(".cmt").html(data);
        }
     });
   });
  });

when I submit the comments, comments only inserted but query (qid) not inserted in DB (database table)

php code

 if(isset($_POST["comments"])){         
    $comment=$_POST['comments'];
    $qid= $_POST['qid'];
    $reslt_user= mysqli_query($connection,"SELECT * FROM tbl_users,`queries` where id='".$_SESSION['id']."' AND  qid= '".$qid."'");
    $row_lat_lng= mysqli_fetch_array($reslt_user);
       $stmt = mysqli_query($connection,"INSERT INTO comments set uid='".$_SESSION['id']."',comments='".$comment."',reply='".$reply."', qid= '".$qid."' ");

Html code

     <div id="comments" class="cmt" >
    <input class="commentbox"id="comment"name="comments"placeholder="Comment 
               Here" maxlength="50">
    <input type="hidden"id="qid "name="qid">
     <button type="button" id="comq" name="compost" class="butn2" value="submit">
    </button>
                        </div>

How to post comments as per queries (how to insert)?

chris85
  • 23,846
  • 7
  • 34
  • 51

1 Answers1

1

See this line here,

<input type="hidden" id="qid "name="qid">
                        ^^^^
  • Misplaced closing " for id attribute.
  • value attribute is missing from the hidden input element.

So the hidden input element should be like this:

<input type="hidden" id="qid" name="qid" value="SOME VALUE" />

Sidenote: Learn about prepared statement because right now your queries are susceptible to SQL injection attack. Also see how you can prevent SQL injection in PHP.

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
  • No Still same issue And let me know how can i give value whats is the use – Madhu Munna Aug 15 '17 at 19:55
  • @MadhuMunna If you don't give any `value` attribute, then with this `var qid=$("#qid").val();` you would get `qid` as *undefined*. Did you change the closing `"` for *id* attribute as well? Do `alert(qid);` inside your js code and see what it is showing. – Rajdeep Paul Aug 15 '17 at 19:59
  • where can i put the alert in ajax – Madhu Munna Aug 15 '17 at 20:01
  • @MadhuMunna Right after `var qid=$("#qid").val();` put `alert(qid);` to check whether you're getting expected value or not. Also, in place of `SOME VALUE` in the `value` attribute, put your desired value there. – Rajdeep Paul Aug 15 '17 at 20:05
  • getting an alert box (empty) when i was submitted the comment – Madhu Munna Aug 15 '17 at 20:07
  • 1
    Regardless, why do you have that `qid` element at all in the INPUT? The ID should be generated by the database, and pushed back to be attributed to the actual comment when it is shown. (No need to have that qid when submitting a comment, but you might wanna have it when showing the comment to be able to identify it on edits, deletions etc.) I'm guessing your whole approach is a bit wonky, and that you don't really understand what you're trying to do. – junkfoodjunkie Aug 15 '17 at 20:11
  • @MadhuMunna That should not happen, see the [fiddle](https://jsfiddle.net/theyg56o/2/). `value` attribute and the associated value must be missing. Check this first. – Rajdeep Paul Aug 15 '17 at 20:14
  • @RajdeepPaul sorry still not working main problem qid not inserted into db – Madhu Munna Aug 16 '17 at 03:18