Dranreb
Dranreb

Reputation: 313

AJAX data returned is not correct

I have this posts.php where it uses a unique id to view different contents. For example: posts.php?id=1 will display all data on my table which has an id of 1, id = 2 for 2 and so on...

This id is held on a php var $post_id on each page and in order to send this php variable on my jquery/javascript, I used:

<div id = "dummy_disp" style = "display: none;">
  <?php echo htmlspecialchars($post_id); ?>
</div>

<div id = "real_commentsno"></div>

( I know, not the best way to do it but it works)

It connects to my js:

var commentid = document.getElementById("dummy_disp");
      var commentidreal = commentid.textContent;

      $.ajax ({
        type:"GET",
        url:"php/post_comments.php",
        data:'comid='+commentidreal,
        success: function(data){
          $("#real_commentsno").html(data);
        }
      });

I have this post_comments.php where it has:

$cc_g_postid = htmlentities($_GET['comid']);
$cc_postid = mysqli_real_escape_string($con,$cc_g_postid);


 $cc_sql  = "SELECT * FROM comments WHERE postuniqueid = '$cc_postid' ";
 $cc_result = mysqli_query($con,$cc_sql);

 $cc_count = mysqli_num_rows($cc_result);

  echo $cc_count;

When I try to view the post_comments.php alongside with the attached url var: ?comid=1 the page displays 2 which is correct since post id=1 on my table has only 2 comments. But when I go to my posts.php the ajax displays 0, not 2. I tried looking at the console, there were no errors.

Is there anything I missed or misdo? Also, post_comments.php is on located on mypage/php/post_comments.php whereas posts.php is located on mypage/posts.php ( i dunno if this is necessary info but maybe the url is jamming it or something? )

Upvotes: 1

Views: 55

Answers (1)

Barmar
Barmar

Reputation: 780724

The problem is that the DIV has newlines and spaces around the ID number. You need to remove all this whitespace.

  var commentidreal = commentid.textContent.trim();

Upvotes: 1

Related Questions