0

oI am having problem fetching comments from MySQL database using jQuery.

I am trying this way, but its not working.

PHP (comments.php)

<?php 

    if (isset($_POST['value1'])) {
        $id = ($_POST['value1']);
    }else{
        $id = '';
    } 
    if (isset($_POST['value2'])) {
        $database = ($_POST['value2']);
    }else{
        $database = '';
    } 
    if (isset($_POST['value3'])) {
        $tablename = ($_POST['value3']);
    }else{
        $tablename='';
    } 

    require_once('rt-connect.php');

    $find_data = "SELECT * FROM $tablename";
    $query = mysqli_query($connection, $find_data);
?> 



  <?php while($row = mysqli_fetch_assoc($query)):?>
  <div class="comment-container">
    <div class="user-info"><?php echo $row['user_name']; ?></div>
    <div class="comment"><p><?php echo $row['comment']; ?></p></div>
  </div>
  <?php endwhile;?>

Jquery(comments.html)

     var id=2;
 var database='comments_db';
 var tablename='comments';

 $.post('comments.php', {value1:id, value2:database, value3:tablename}, function(data)
    {
    $('#comments').load('comments.php .comment-container');
 });

Html(div on comments to load on comments.html)

      <div id="comments"></div><!--end of comments-->

Please see and suggest any possible way to do it.

Thanks

2
  • Which part is not working? Btw: you are exposing your database name and table name in the javascript which is not secure. Commented Oct 11, 2012 at 4:27
  • php is working but when fetching result with jquery not working, also its just a test page, i will remove database name once it gets working. Commented Oct 11, 2012 at 4:29

4 Answers 4

1

Try This one it will help you. This is jquery Ajax post method requesst if you want to show your data is loaded or not just remove the commet.

$.ajax({
type: "POST",
url: url,
data: { value1:id, value2:database, value3:tablename}
}).done(function( data ) {
//alert(data); return false;

$("#comments").html(html);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Got it working this way also, thanks, i appreciate your time and input, as Rajat Singhal has answered first, so i accepted his answer. too bad to accept only one answer.
0

You have $.load() inside success function of $.post(), try this..

$.post('comments.php', {value1:id, value2:database, value3:tablename}, function(data)
   {
   $('#comments').html(data);
});

1 Comment

I have to load comments from .comment-container which is on comments.php on to #comments which is on comments .html
0

In your javascript, you are posting data to a url, accepting response and if the response is successful, you are sending another request to the PHP script, this time without parameters. Your comments box is displaying the result of your second request.

You do not require :

 $('#comments').load('comments.php .comment-container');

in your javascript, since you have already received a response. Instead, use :

  $('#comments').html(data);

which will display the response data in the comments div.

2 Comments

What is the data you are receiving in your div?
Got it working this way, thanks, i appreciate your time and input, as Rajat Singhal has answered first, so i accepted his answer. too bad to accept only one answer.
0

You could try this one,

 var id = 2;
 var database  = 'comments_db';
 var tablename = 'comments';

$.ajax({
    type   :"POST",
    data   :"id="+id+"&database="+database+"&tablename="+tablename,
    url    : comments.php, 
    success: function(msg){
        $("#comments").html(msg);
     }
});

3 Comments

Please don't construct the query string with concatenation, use an object and let $.ajax turn it into form data.
@Barmar - Oh i see, i will try to avoid, Thanks for pointing out this.
Got it working this way also, thanks, i appreciate your time and input, as Rajat Singhal has answered first, so i accepted his answer. too bad to accept only one answer.

Your Answer

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