php - Get a chat refresh constantly -
i have problem making chat refresh constantly.
this how built it:
i save messages in mysql database connected php, loading messages php , post messages php, have jquery making message when clicked on submit send chatbox.
my problem when me , friend chat, have refresh site everytime writes something. want make chatbox refresh constantly. have tried $('#example').load(document.url + ' #example');
doesn't work, can please me.
[html]
<!doctype html> <html lang="en"> <head> <meta charset="utf-8" /> <title>chatbox</title> <script src="http://code.jquery.com/jquery-1.11.3.min.js" charset="utf-8"></script> <script> $(document).ready(function(){ $("#content").load("ajaxload.php"); $("a.btn").click(function(){ $.post('ajaxpost.php', $('#userarea').serialize(), function(data){ $('#content').append('<p>'+data+'</p>'); }); return false; }); }); </script> <script src="chatbox.js" charset="utf-8"></script> <link href='http://fonts.googleapis.com/css?family=indie+flower' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=open+sans' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="chatbox.css" title="chatbox" type="text/css" media="screen" charset="utf-8"> </head> <body> <h1 id="swegre"> swegre<span>©</span> </h1> <div id="wrapper"> <h1>leave comment!</h1> <div id ="content"> <p style="background-color: #b7b7b7">admin: here's our chatbox</p> </div> <div id="message"> <form id="userarea"> <textarea id="textareamsg" name="messages" rows="2" cols="30" required=""></textarea> <a href="#" class="btn">post</a> </form> </div> </div> </body> </html>
[css not important] [jquery]
$(document).ready(function(){ $( "a.btn" ).click(function() { var textvalue = $('textarea#textareamsg').val(); if(textvalue == "" || textvalue == " " || textvalue == " " || textvalue == " "){ alert('you have write in textarea post message'); return false; }else{ $('#content').append("<p>you: " + textvalue + "</p>"); $('textarea#textareamsg').val(''); } }); });
[ajaxload.php]
<?php //conection: $link = mysqli_connect("localhost","root","123","chat") or die("error " . mysqli_error($link)); //consultation: $query = "select * messages" or die("error in consult.." . mysqli_error($link)); //execute query. $result = mysqli_query($link, $query); //display information: while($row = mysqli_fetch_array($result)) { echo '<p class="other">'; echo $row ["message"] . '</p>'; } ?>
[ajaxpost]
<?php $link = mysqli_connect("localhost","root","123","chat") or die("error " . mysqli_error($link)); //consultation: $query = "select * messages" or die("error in consult.." . mysqli_error($link)); //execute query. $result = mysqli_query($link, $query); //code goes here $message = $_post['messages']; mysqli_query($link,"insert messages (message) values ('$message')"); ?>
use ajax reload chat div.
steps
1. make chat div 2. make ajax call every 10 sec 3. design new page called chat.php 4. update chat data per db.
moral: call ajax every 10 sec
Comments
Post a Comment