Hi I have problem to get data when insert with ajax. I have ajax.js file and here is this code:

$('#text-content').keypress(function(e){ if(e.which == 13) { if($('input#enter-click').prop('checked', true)) { $('#live-send').click(); e.preventDefault(); } } }); $('#live-send').click(function(){ var message = $.trim( $('#text-content').val() ); if($('#text-content').val()!="") { $.ajax({ type: 'POST', dataType: "json", url: './includes/conversation.php', data: { message: message }, cache: false, success: function(data) { $('.commentArea').load( 'Messages.php.'); $('.bubbledRight').val(""); } }); // setInterval(function(){ // $('.commentArea').load( 'Messages.php.'); // },1000); return false; } else { //notify the user they need to enter data alert("Please write messages!"); } }); $('.max-height').scrollTop($('.max-height')[0].scrollHeight); //show messages on bottom In index page I have <div class="commentArea"></div>

for show conversation this is query for select:

<?php $conversation_ID = 1; $sql = "SELECT * FROM conversations_messages WHERE conversation_ID='{$conversation_ID}' ORDER BY time ASC"; $query = $connection->prepare($sql); $query->execute(); while($r = $query->fetch(PDO::FETCH_OBJ)) { $data = ""; // startting div tag if ( $user_ID == $r->sender_ID ) { $data .= '<div class="bubbledRight">'; } else { $data .= '<div class="bubbledLeft">'; } // image tag (later to add) $data .= '<img class="img-responsive" alt="" src="images/profileimage.jpg">'; // message content $data .= $r->message; // time sent (latter to add) $data .= '<span class="label label-default">21.Aug</span>'; // ending div tag $data .= '</div>'; // display message echo $data; } ?>

I insert data in database corectly but if want to see in index I must refresh page. How to fix this problem? Thanks!

You could either do another AJAX query to return all of the conversations and use the result to update the comment area (could be slow depending on the amount of data) or you could use javascript to inject the new comment into the page, at the correct location, after the post has been saved.

Member Avatar

diafol

DId you intentionally leave out any spacing and indenting, or did the editor strip them?