Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Ajax shoutbox

  1. #1
    New Coder
    Join Date
    Oct 2009
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts

    Ajax shoutbox

    Howdy!

    Been trying to make an shoutbox, it works perfectly so far but it all goes bad when im trying to add ajax for letting it auto refresh.

    ajax_shout.js :

    Code:
    function ajaxRequest(){
        var activexmodes=["Msxml2.XMLHTTP", "Microsoft.XMLHTTP"] //activeX versions to check for in IE
        if (window.ActiveXObject){ //Test for support for ActiveXObject in IE first (as XMLHttpRequest in IE7 is broken)
            for (var i=0; i<activexmodes.length; i++){
                try{
                    return new ActiveXObject(activexmodes[i])
                }
                catch(e){
                //suppress error
                }
            }
        }
        else if (window.XMLHttpRequest) // if Mozilla, Safari etc
            return new XMLHttpRequest()
        else
            return false
    }
    
    
    function updateShoutbox(){
        var ajax_request= new ajaxRequest();
        ajax_request.onreadystatechange=function(){
            if (ajax_request.readyState==4){
                if (ajax_request.status==200){
                    ajax_request.open("GET", "./shout.php?a=d", true)
                    //ajax_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
                }
                else{
                    alert("An error has occured making the request")
                }
            }
        }
    }
    shout.php:

    Code:
    <script type="text/javascript" src="./themes/default/jquery-1.4.2.js"></script>
    <script type="text/javascript" src="./themes/default/ajax_shout.js"></script>
    
    <form method="post" action="">
        <table>
            <tr>
                <td>
                    <?php
                    $SHOUT_NOTICE = true;
                    $SHOUT_NOTICE_TEXT = "Welcome to my Shoutbox!";
                    if(!empty($_SESSION["SHOUT_NICK"])) {
                        ?>
                    <input type="text" value="<?php echo $_SESSION['SHOUT_NICK'] ?>" size="25" maxlength="15" onclick="this.value='';" name="name">
                        <?php
                    } else {
                        ?>
                    <input type="text" value="Nickname" size="25" maxlength="15" onclick="this.value='';" name="name">
                        <?php
                    }
                    ?>
                </td>
            </tr>
            <tr><td><textarea name="msg" rows="6" maxlength="65" onclick="this.value='';">Message</textarea></td>
            </tr><tr><td><input type="submit" name="post" value="Shout!"></td>
            </tr>
        </table>
    
        <div id="shout-frame">
            <?php
            if($SHOUT_NOTICE) {
                echo '<div id="shout-notice">';
                echo $SHOUT_NOTICE_TEXT;
                echo '</div>';
            }
            ?>
            <div id="shout-body">
    
                <?php
    
                global $cfg;
                global $conn;
                global $engine;
    
                if(EMPTY($_POST)) {
                     ?>
                <script type="text/javascript">
                setInterval('updateShoutbox()', 4000);
                </script>
                     <?php
                }
                if(isset($_GET['a'])) {
                    $query = $conn->query('SELECT * FROM shoutbox ORDER BY id DESC');
                    $rows = mysql_num_rows($query);
                    if($rows < 1) {
                        echo '<div id="shout-notice">There currently are no shouts.</div>';
                    }
                    $i = 0;
                    while($i < $rows) {
                        $shout_message = mysql_result($query, $i, 'message');
                        $shout_date = mysql_result($query, $i, 'date');
                        $shout_author = mysql_result($query, $i, 'author');
    
                        $shout_message = str_replace("[color=orange]", "<font color='orange'>", $shout_message);
                        $shout_message = str_replace("", "<font color='red'>", $shout_message);
                        $shout_message = str_replace("", "<b>", $shout_message);
                        $shout_message = str_replace("", "</b>", $shout_message);
                        $shout_message = str_replace("", "</font>", $shout_message);
                        echo '&nbsp;Written by: '.$shout_author.' at '.$shout_date.'<div id="shout-message">'.$shout_message.'</div><br>';
    
                        $i++;
                    }
                }
                if(isset($_POST['post'])) {
                    $micro = time() >= $_SESSION['LAST_POST'] + 30 ? true : false;
                    if(!$micro) {
                        $lp = time() - $_SESSION['LAST_POST'];
                        $engine->displayError('You can only post a message every 30 seconds. Last post: '.round($lp).' second(s) ago.', 'Anti-Spam ');
                        exit;
    
                    }
                    $name = mysql_escape_string($_POST['name']);
                    $message = mysql_escape_string($_POST['msg']);
    
                    $name_valid = strlen($name) <= 15 && strlen($name) > 2 ? true : false;
                    $message_valid = strlen($message) <= 65 && strlen($message) > 2 ? true : false;
    
                    if(!$name_valid) {
                        $engine->displayError('Nickname cannot exceed 15 and must be atleast 3 characters!', 'Nickname Length');
                        exit;
                    }
                    if(!$message_valid) {
                        $engine->displayError('Message cannot exceed 65 and must be atleast 3 characters.', 'Message Length');
                        exit;
                    }
                    $_SESSION['SHOUT_NICK'] = $name;
                    $date = date ('Y-m-d H:i:s');
    
                    $message = str_replace("<","&lt;", $message);
                    $message = str_replace(">","&gt;", $message);
                    $query = $conn->query("INSERT INTO shoutbox VALUES('$message', '$name' , '$date','')");
                    $_SESSION['LAST_POST'] = microtime(true);
                    $engine->reloadPage("?p=shout");
                }
    
    
    
                ?>
    
            </div>
        </div>
    </form>
    Please help, could anyone edit my script and make it work with auto refreshing. i know that might be alot to ask but in this way i will learn it quicker.

  • #2
    New Coder
    Join Date
    Oct 2009
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Anyone that can help?

  • #3
    New Coder
    Join Date
    Oct 2009
    Posts
    78
    Thanks
    1
    Thanked 2 Times in 2 Posts
    Bump .

  • #4
    New Coder
    Join Date
    Apr 2010
    Posts
    38
    Thanks
    0
    Thanked 3 Times in 3 Posts
    If you will go for jquery ajax.post() $.post() each from jquery.com
    it may help you more.

    Else you may fix the bug by yourself, by giving alert message for each line to find where it fails on the ajax. If you have firebug with firefox, it is well and good.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •