php - Unable to auto-refresh page element using AJAX -
i playing around trying make chatbox, find myself unable make auto-reload messages. using database messages there insufficient permission php write files on server. here code chatbox:
<input name="usermsg" id="usermsg" type="text"/> <button id="submitmsg" type="button" onclick="onsubmit()">send</button> </div> <script type="text/javascript" src="src/assets/js/jquery.js"></script> <script> function onsubmit(){ //console.log('ghffgj'); if($("#usermsg").val()) { //alert($("#usermsg").val()); var clientmsg = $("#usermsg").val(); $.ajax({ type: "post", url: "src/add_reply.php", data: {text: clientmsg}, }); $("#usermsg").attr("value", ""); return false; } } setinterval (loadlog, 3000); function loadlog(){ $.ajax({ url: "src/chatlog.php", cache: false, success: function(data){ $("#chatbox").html(data); }, }); } </script>
and code chatlog.php:
<?php include('src/database_connect.php'); $select_query = "select author, send_date, message chatbox"; $chat_obj = $conn->query($select_query); while($row = $chat_obj->fetch_assoc()){ echo "<div><span style='font-size: 10px'>(".$row['send_date'].")</span><b>".$row['author']."</b>: ".$row['message']; } $conn->close(); ?>
adding new messages in database works, can't seem able return text display on chatlog.php chatbox page
edit: forgot mention, browser console displays this: http://domain/chatbox/src/chatlog.php?_=1467644248226 500 internal server error 29ms
found solution, displayed below: index.php (only chatbox code):
<input name="usermsg" id="usermsg" type="text"/> <button id="submitmsg" type="button" onclick="onsubmit()">send</button> </div> <script type="text/javascript" src="src/assets/js/jquery.js"></script> <script> function onsubmit(){ console.log($("#usermsg").val()); if($("#usermsg").val()) { //alert($("#usermsg").val()); var clientmsg = $("#usermsg").val(); $.ajax({ type: "post", url: "src/add_reply.php", data: {text: clientmsg}, }); $("#usermsg").val(""); return false; } } function loadlog(){ $.ajax({ url: "src/chatlog.php", cache: false, success: function(data){ $("#chatbox").html(data); }, })/*.done(function(msg) { alert(msg); })*/; } $( document ).ready(function() { loadlog(); setinterval (loadlog, 2000); }); </script>
chatbox.php :
<?php include('database_connect.php'); $select_query = "select author, send_date, message chatbox"; $chat_obj = $conn->query($select_query); //$chat_list = array(); $content = ""; while($row = $chat_obj->fetch_assoc()){ //$chat_list[] = $row; $content .= "<div><span style='font-size: 10px'>(" . $row['send_date'].")</span><b>". $row['author']."</b>: ".$row['message']."</div>"; } $conn->close(); die($content); ?>
Comments
Post a Comment