3

I opened a question for a different issue that finally led to the one I now need help with. AJAX form Submit to post comments

I got a solution to the js errors I was getting but now the issue is something else.

I'm using a script I found here. Credits and Demo to original script

and it's meant to post comments without refreshing the page with a fadeIn effect. I modified it to fit my project and the issue I have now is that even tho' it is saving the data into the database, it's not loading the comments live. I have to manually refresh the page is I want to see the new content. Since it's not throwing any js errors in the developer console, I can't find what the issue is.

The Comments Section

<div class="comments">
    <div class="top-title">
        <h3>Messages</h3>
    </div>
    <ul class="level-1">
    <div class="comment-block">
<?php while ($row = mysqli_fetch_array($res_post_select))
{
$PageID = $row['PageID'];
$Name = $row['Name'];
$Photo = $row['Photo'];
$PostDate = $row ['PostDate'];
$Comment = nl2br($row['Comment']);
?>
    Posted on <?php echo DATE("F d, Y", strtotime($PostDate)) ?> at <?php echo DATE("g:i a", strtotime($PostDate)) ?>
    <li>
    <span class='comment'>
    <span class='picture'>
    <span><img src='users/images/<?php echo $Photo?>'></span>
    </span>
    <span class='content'><span class='mid-title'><b><?php echo $Name ?></b>, Said:</span><br><?php echo $Comment ?></span>
    <span class='clear'></span>
    </span>
    </li>
<?php } ?>

The JS Credits

<script>
$(document).ready(function(){
  var form = $('form');
  var submit = $('#submit');

  form.on('submit', function(e) {
   //$('#submit').on('click', function(e) {
    // prevent default action
    e.preventDefault();
    // send ajax request
    $.ajax({
      url: 'data/queries/comment-insert.php',
      type: 'POST',
      cache: false,
      data: form.serialize(), //form serialized data
      beforeSend: function(){
        // change submit button value text and disabled it
        submit.val('Posting...').attr('disabled', 'disabled');
      },
      success: function(data){
        // Append with fadeIn see https://stackoverflow.com/a/978731
        var item = $(data).hide().fadeIn(800);
        $('.comment-block').append(item);

        // reset form and button
        form.trigger('reset');
        submit.val('Post Message').removeAttr('disabled');
      },
      error: function(e){
        alert(e);
      }
    });
  });
});
</script>

The comment-insert.php File

<?php
if (isset( $_SERVER['HTTP_X_REQUESTED_WITH'] )):
$ds = DIRECTORY_SEPARATOR;
$base_dir = realpath(dirname(__FILE__)  . $ds . '..') . $ds;
include_once("{$base_dir}..{$ds}include{$ds}dbconfig.php");
include_once("{$base_dir}..{$ds}include{$ds}dbconnection.php");
$conn = dbconnect();

    if(!empty($_POST['comment']))
        {
$Name = mysqli_real_escape_string($conn, $_POST['Name']);
$PageID = mysqli_real_escape_string($conn, $_POST['PageID']);
$ID = mysqli_real_escape_string($conn, $_POST['ID']);
$Comment = mysqli_real_escape_string($conn, $_POST['comment']);

$sql = "INSERT INTO comments
(PageID, ID, Name, PostDate, Comment)
VALUES
('$PageID', '$ID', '$Name', now(), '$Comment')";

    mysqli_query($conn, $sql) or die("Error: ".mysqli_error($conn));

}
?>

<!-- This is the mark up that should fadeIn after the data is inserted into the database.
 You can refer to the demo found in the credited page -->

<li><span class='comment'>
<span class='picture'>
<span><img src='users/images/<?php echo $Photo ?>'></span>
</span>
<span class='content'><span class='title'><b><?php echo $Name ?></b>, Said:</span><br><?php echo $Comment ?></span>
<span class='clear'></span>
</span>
</li>

<?php
mysqli_close($conn);
endif

So, the ajax triggers the query found in comment-insert.php but it does not insert live and is not giving any js errors in the console. I have to manually refresh the page to view the comments which defeats the purpose of the live-comment idea.

Community
  • 1
  • 1
BlueSun3k1
  • 757
  • 5
  • 21
  • 39
  • Can you preview what the AJAX query is returning in your browsers console network tab? – TomDillinger May 06 '16 at 03:26
  • Have you checked that you are at least getting into the `success` callback and getting your HTML in the `data`? – Mikey May 06 '16 at 03:27
  • side notes: no need to captialize the PHP variables. and the `DATE()` should be `date()`. – Raptor May 06 '16 at 03:27
  • @TomDillinger. It previews the text part. the image doesn't preview. The POST response returns this.
  • John Doe, Said:
    This is the option you selected
  • and the HTML returns text portion (no image) without format. – BlueSun3k1 May 06 '16 at 03:38
  • Just to confirm, after you submit a comment, your form is resetting to blank? Your issue might be with your hide/fade-in if your form is resetting as it means your the success function is running. – TomDillinger May 06 '16 at 03:46
  • @Mikey. the error handler in the script doesn't return any errors. I am guessing the callback is successful since it passes to the form reset trigger which is the last event in the script. maybe I am mistaken. any suggestions on how to check if the callback is successful? – BlueSun3k1 May 06 '16 at 03:47
  • 1
    I'd use the element inspector to check if the comments are being appended, but are not displaying. – TomDillinger May 06 '16 at 03:47
  • @TomDillinger. You are right. I used the element inspector and the firebug html inspector and the comments are being appended and not displaying until the page is refreshed. how can I tackle this? – BlueSun3k1 May 06 '16 at 03:57
  • So the problem is with your fadeIn code. Try removing the hide/fadeIn component to make sure it displays and then tinker around with different combinations of hide/Fade/display:none to see which one works in your current browser. I'd recommend trying other browsers also to see if it works in those. – TomDillinger May 06 '16 at 04:01