Ajax only posts sometimes

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Ajax only posts sometimes

Post by shiznatix »

Ok so I have a live chat thing that I made and it seams like it should work find but I am having a few problems. The biggest problem is that it only posts some of the time, but does not seam to throw the exception when it fails to post anything. The second is this section of code:

Code: Select all

onkeydown="if (event.keyCode == 13) add_content();"
that seams to work since in the function add_content() it empties the text box called 'message' and this happens. The problem is that nothing gets posted. It also seams to be a little fickle when it comes to posting the message even if I click the 'send' button. Could someone please take a look at this and see if my ajax is messed up or could be improved somehow?

The source of the chat page:

Code: Select all

<script type="text/javascript">
                try
                {
                    if (window.XMLHttpRequest)
                    {
                        Ajax = new XMLHttpRequest();
                    }
                    else
                    {
                        Ajax = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                }
                catch(e)
                {
                    alert(e);
                };

               function update_content()
               {
                    try
                    {
                        Ajax.open("GET", "chat.php?_view=chat_content", true);
                        Ajax.send(null);
                  
                        Ajax.onreadystatechange = function()
                        {
                            if (Ajax.readyState == 4 && Ajax.status == 200)
                            {
                                if ("end" == Ajax.responseText)
                                {
                                    window.location="chat.php?_view=ended";
                                }
                                else if ("need rep" == Ajax.responseText)
                                {
                                    document.getElementById("content").innerHTML = "Waiting for a representative...";
                                }
                                else if ("timeout" == Ajax.responseText)
                                {
                                    window.location="chat.php?_view=timeout";
                                }
                                else
                                {
                                    document.getElementById("content").innerHTML = Ajax.responseText;
                                }
                            }
                        };
                    }
                    catch(e)
                    {
                        //alert(e);
                    }
                 
                    setTimeout("update_content()",5000);
                }

                function add_content()
                {
                    try
                    {
                        Ajax.open("POST", "chat.php?_action=add", true);
                        var POST = "message=" + document.getElementById("message").value;
                        Ajax.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
                        Ajax.setRequestHeader("Content-Length", POST.length);
                        Ajax.send(POST);

                        document.getElementById("message").value = "";

                        Ajax.onreadystatechange = function()
                        {
                            if (Ajax.readyState == 4 && Ajax.status == 200)
                            {
                                if ("need rep" == Ajax.responseText)
                                {
                                    document.getElementById("content").innerHTML = "You must wait for a representative to post";
                                }
                            }
                        };
                    }
                    catch(e)
                    {
                        alert(e);
                    }
                }
                update_content();

            </script>
        
                    <link rel="stylesheet" href="css/css.css" type="text/css" />

                </head>
                <body>
        <div style="text-align: center;">
    <p>
                <table class="small-table" style="width: 500px;">
    <tr>
        <td>
            <div id="content">
            </div>

        </td>
    </tr>
    <tr>
        <td style="text-align: left;">
            <input type="text" name="message" id="message" style="width: 100%;" onkeydown="if (event.keyCode == 13) add_content();"/>
        </td>
        <td>
            <input type="submit" value="SEND" onclick="add_content();" class="submit-button"/>
            <form action="chat.php" method="get" style="display: inline;">

                <input type="hidden" name="_action" value="end_chat"/>
                <input type="submit" value="END CHAT" class="submit-button"/>
            </form>
        </td>
    </tr>
</table>    </p>
</div>
                    
                </body>
Post Reply