Results 1 to 6 of 6
  1. #1
    Join Date
    Apr 2005
    Location
    North Carolina, USA
    Posts
    4,535
    Tokens
    0

    Latest Awards:

    Default Simple JS Help I think [Nets :P HELP!]

    Ok,

    I have an ajax code to read a .php file, and I have my alert PHP file.

    This is for an alert system. But instead of a refreshing iframe, I want ajax to do it.

    I have it to work, but when there isn't an alert (or the one your session is set to) it shows a blank alert.

    Very confusing in my wording it.

    Page > serverTime.php sets session to the id of the alert > shows alert
    Page > serverTime.php reads session, if no new alert echo ""; > shows blank alert

    I don't want it to show any alert if there is already a session

    +REP for all help!

  2. #2
    Join Date
    Aug 2004
    Location
    bristol
    Posts
    3,799
    Tokens
    0

    Latest Awards:

    Default

    Code:
    if(HttpRequest.responseText != '') {
      alert(HttpRequest.responseText);
    }
    ?

    You may have named your HttpRequest object something else.
    kinda quit.

  3. #3
    Join Date
    Apr 2005
    Location
    North Carolina, USA
    Posts
    4,535
    Tokens
    0

    Latest Awards:

    Default

    PHP Code:
    <?
    session_start
    ();
    $id "401";
    $message "Caleb: test";
    if(
    $_SESSION[session_alert] != "$id") {
    echo 
    "$message";
    $_SESSION[session_alert] = $id;
    exit;
    }
    ?>
    Thats my servertime.php

    Code:
    <?php
         session_start();
        include("config.php");
    ?>
    <script type="text/javascript">
    var xmlHttp 
    
    function GetXmlHttpObject()
    { 
        var objXMLHttp = null 
        if (window.XMLHttpRequest)
        { 
            objXMLHttp = new XMLHttpRequest() 
            }
                else if (window.ActiveXObject)
            { 
            objXMLHttp = new ActiveXObject("Microsoft.XMLHTTP")
        } 
        return objXMLHttp 
    } 
    
    function GetShouts()
    { 
        xmlHttp = GetXmlHttpObject() 
        if (xmlHttp == null)
        {
            alert ("Browser does not support HTTP Request")
            return
        }
        var url="serverTime.php"
        xmlHttp.open("GET", url, true)
        xmlHttp.onreadystatechange = function ()
        {
            if (xmlHttp.readyState == 4)
                { 
                    if (xmlHttp.status == 200)
                        {
    if(xmlHttp.responseText != '') {
      alert(xmlHttp.responseText);
    }
                            ; 
                        
                        }
                }
        };
        xmlHttp.send(null); 
    }
    GetShouts(); 
    setInterval("GetShouts()", 1000);
    </script>
    <body onload="javascript:GetShouts();">
    </body>
    thats my ajax.php, somethings wrong..
    Last edited by Dentafrice1; 18-04-2007 at 11:58 PM.

  4. #4
    Join Date
    Aug 2004
    Location
    bristol
    Posts
    3,799
    Tokens
    0

    Latest Awards:

    Default

    You might have some whitespace in your serverTime.php page.

    Code:
    if(xmlHttp.responseText.replace(/^\s+|\s+$/g, '') != '') {
      alert(xmlHttp.responseText);
    }
    kinda quit.

  5. #5
    Join Date
    Aug 2004
    Location
    UK
    Posts
    11,283
    Tokens
    2,031

    Latest Awards:

    Default

    You could add a null return?

    PHP Code:
    <?
    session_start
    ();
    $id "401";
    $message "Caleb: test";
    if(
    $_SESSION[session_alert] != "$id") {
    echo 
    "$message";
    $_SESSION[session_alert] = $id;
    exit;
    }else{
    echo 
    "!no!";
    exit;
    }
    ?>
    So basicly instead of returning nothing it returns !no! when there is nothing, which is easyer to pick up on.

    Then in your js just change it to

    if(xmlHttp.responseText != '!no!') {
    alert(xmlHttp.responseText);
    }

    o.0

  6. #6
    Join Date
    Apr 2005
    Location
    North Carolina, USA
    Posts
    4,535
    Tokens
    0

    Latest Awards:

    Default

    Thanks carl and nets

    +REP

Posting Permissions

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