Home Top Projects Tutorials Articles Submit Project
 
  • .NET Frameworks
  • Java Frameworks
  • PHP Frameworks
  • Ruby Frameworks
  • Other Frameworks
  • Cool AJAX sites
  • Ajax Resources
  • Ajax Tools
  • JavaScript frameworks
 
     
    • Ajax RSS reader
    • Ajax for tables
    • Using ASP.NET MVC and Ajax to check names availability
    • JavaScript Method Overloading
    • Saving Session Across Page Loads Without Cookies, On The Client Side
    • Use jQuery to simplify Ajax development
    • AJAX with jQuery and EasyWeb4J
    • How To Create Your Own Ajax Contact Form
    • AJAX Tutorial for Begginers
    • Chrome Extension Process Model Design Doc
    Home » Tutorials » PHP-AJAX Chat/Shoutbox Example

    PHP-AJAX Chat/Shoutbox Example

    AJAX and PHP combined together in a simple chat message server.  This uses XMLHTTPRequest to call itself with the appropriate GET variables to trigger a read or write to a server-side file named chit-chat.dat

     
    <?php
    $filename = 'chit-chat.dat';
     
    if(isset($_GET['msg'])){
    $msg = sanitize($_GET['msg']);
    $conv = file_get_contents($filename);
    file_put_contents($filename,$conv.$msg);
    exit;
    }
    if(isset($_GET['conv'])){
    $conv = file_get_contents($filename);
    echo $conv;
    exit;
    }
    function sanitize($var){
    //sanitize here
    return $var;
    }
    ?>
     

    <script type="text/javascript">
     
    function createXMLHttpRequest() {
    if (typeof XMLHttpRequest != "undefined") {
    return new XMLHttpRequest();
    } else if (typeof ActiveXObject != "undefined") {
    return new ActiveXObject("Microsoft.XMLHTTP");
    } else {
    throw new Error("XMLHttpRequest not supported");
    }
    }
     
    function sendMsg(msg){
    request = createXMLHttpRequest();
    request.open("GET", "index.php?msg="+msg, true);
    request.onreadystatechange = function() {
    //
    }
    request.send(null);
    }
    function rcvMsg(){
    request = createXMLHttpRequest();
    request.open("GET", "index.php?conv=1", true);
    request.onreadystatechange = function() {
    if (request.readyState == 4 && request.status == 200) {
    var node = document.createTextNode(request.responseText);
    document.getElementById("output").appendChild(node);
    }
    }
    request.send(null);
    }
    rcvMsg();</script>
     
    <div id="output">
     
    </div>
    <input type="text" id="msg_form"><input type="submit" value="send" onClick="sendMsg(document.getElementById('msg_form').value);rcvMsg();">

    source: hawkenterprises

    PHP-AJAX+Chat%2FShoutbox+Example addthis

    Copyrights Reserved AjaxProjects.com 2006-2013, Powered by Enozom - Mobile Development Company -Privacy Policy