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

7
  • 2
    You are open to SQL injections, parameterize your query. Check the return of your query call for errors. Commented Aug 15, 2017 at 19:42
  • Where do you define $reply? Commented Aug 15, 2017 at 19:44
  • @chris85 Please Give me more clarification on this Commented Aug 15, 2017 at 19:44
  • Read more about Prepared Statements in the manual Commented Aug 15, 2017 at 19:44
  • 2
    You haven't SET a value on #qid it has name and id, but no value, hence it will be empty/undefined. Commented Aug 15, 2017 at 19:47

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

No Still same issue And let me know how can i give value whats is the use
@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.
where can i put the alert in ajax
@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.
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.