Jump to content

A simple ajax system not working for me


stockton

Recommended Posts

The following very simple scenario is not working for me.

Please help.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
        "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<title>Test PHP Semaphore</title>
<meta name="author" content="Alf Stockton">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="Mon, 22 Jul 2000 11:12:01 GMT">
<script language="JavaScript" type="text/javascript" src="js/Diva.js"></script>
</head>
<body>
<center>
<h2>Test PHP Semaphore</h2>
</center>
<form action="semaphoreTest.php" method="get" name="SemaphoreTest" id="SemaphoreTest" onSubmit='return false;' >
<center>
<input type=hidden name="submit1" id="submit1" value="<?php echo $Ding; ?>" >
<P>
<input type=submit value="Submit" onclick="submit_click()">
</p>
<div id="Boodskap";></div>
<!-- <input type=text readonly name="Boodskap" id="Boodskap" value ='<?php echo $Message ?>'>; -->
</center>
</FORM>
</body>
</html>
<script language=javascript>
function submit_click()
{
alert "submit_click()";
var bError = false;
var strErrorMsg = "";
var Command = "semaphoreTest.php";
progExe(Command);
return false;
}

the semaphoreTest.php looks like:-

<?php
        {
        $intKey = GUUIDtoInt('b6379c52-d96d-418b-83c3-8aedb5e6cf95');
//        echo "Testing semaphore code<br/>";
        if (!$sem_id = sem_get($intKey)) 
            {
            $Message="\nX\n\nCould not get ID for the semaphore.";
            echo $Message;
            exit;
            } 
        else 
            {
            $Message="\nX\n\nGot semaphore ID; acquiring...";
            flush();
            if (!sem_acquire($sem_id)) 
                {
                $Message.=" failed.";
                echo $Message;
                exit;
                } 
            else 
                {
                $Message="\nX\n\nOK got it. Sleeping for 20 seconds, then releasing.";
                flush();
                sleep(20);
                sem_release($sem_id);
                $Message.="Semaphore released. Other scripts should be able to get it now.";i
                echo $Message;
                exit;
                }
            }
        }
?>

and the Diva.js is:-

function getXMLHttpRequest ()
    {
    try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) {};
    try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) {};
    try { return new XMLHttpRequest(); } catch(e) {};
    return null;
    }

// Make the XMLHttpRequest object
    var xhr = getXMLHttpRequest();

function progExe(CommandString)
    {
    xhr.open("GET", CommandString, true);
    xhr.onreadystatechange = ServerResponse;
    xhr.send(null); 
    }

function ServerResponse()
    {
    alert("entered ServerResponse()");
/* The full list of the readyState values is as follows:
    * 0 (uninitialized)
    * 1 (loading)
    * 2 (loaded)
    * 3 (interactive)
    * 4 (complete) 
*/
    if(xhr.readyState == 0)
        {
        document.getElementById("Boodskap").innerHTML = "Uninitialized........";
        }
        else
    if(xhr.readyState == 1)
        {
        document.getElementById("Boodskap").innerHTML = "Loading........";
        }
        else
    if(xhr.readyState == 2)
        {
        document.getElementById("Boodskap").innerHTML = "Loaded........";
        }
        else
    if(xhr.readyState == 3)
        {
        document.getElementById("Boodskap").innerHTML = "Interactive........";
        }
        else
    if(xhr.readyState == 4)
        {
        if (xhr.status == 200)
            {
            var parts = xhr.responseText.split("\n");
            if (parts[1] == "X")
               {
               alert(parts[3]);
               }
            else window.location=parts[3];
            document.getElementById("Boodskap").innerHTML = "";
            }
        }
    }

Link to comment
Share on other sites

function ServerResponse()
    {
    alert("entered ServerResponse()");
/* The full list of the readyState values is as follows:
    * 0 (uninitialized)
    * 1 (loading)
    * 2 (loaded)
    * 3 (interactive)
    * 4 (complete)
*/
    if(xhr.readyState == 0)
        {
        document.getElementById("Boodskap").innerHTML = "Uninitialized........";
        }
        else
    if(xhr.readyState == 1)
        {
        document.getElementById("Boodskap").innerHTML = "Loading........";
        }
        else
    if(xhr.readyState == 2)
        {
        document.getElementById("Boodskap").innerHTML = "Loaded........";
        }
        else
    if(xhr.readyState == 3)
        {
        document.getElementById("Boodskap").innerHTML = "Interactive........";
        }
        else
    if(xhr.readyState == 4)
        {
        if (xhr.status == 200)
            {
            var parts = xhr.responseText.split("\n");
            if (parts[1] == "X")
               {
               alert(parts[3]);
               }
            else window.location=parts[3];
            document.getElementById("Boodskap").innerHTML = "";
            }
        }
    }

 

Question - does the first alert in that function work?

Link to comment
Share on other sites

Instead of assigning onreadystatechange to ServerResponse, what if you dynamically create the function there?

 

xhr.onreadystatechange = function() {
   // put ServerResponse code here
}

 

Would that work? Did you get the alert from submit_click function?

Link to comment
Share on other sites

Try with this:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
        "http://www.w3.org/TR/REC-html40/loose.dtd">
<html>
<title>Test PHP Semaphore</title>
<meta name="author" content="Alf Stockton">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="Mon, 22 Jul 2000 11:12:01 GMT">
<script language="JavaScript" type="text/javascript" src="js/Diva.js"></script>
</head>
<body>
<center>
<h2>Test PHP Semaphore</h2>
</center>
<form action="semaphoreTest.php" method="get" name="SemaphoreTest" id="SemaphoreTest" onSubmit="return submit_click();">
<center>
<input type=hidden name="submit1" id="submit1" value="<?php echo $Ding; ?>" >
<P>
<input type=submit value="Submit">
</p>
<div id="Boodskap";></div>
<!-- <input type=text readonly name="Boodskap" id="Boodskap" value ='<?php echo $Message ?>'>; -->
</center>
</FORM>
</body>
</html>
<script type="text/javascript">
function submit_click()
{
alert("submit_click()");
var bError = false;
var strErrorMsg = "";
var Command = "semaphoreTest.php";
progExe(Command);
return false;
}
</script>

Link to comment
Share on other sites

I have removed the " onSubmit='return false; " from the form definition in the html and now I get the final message but not those messages inbetween.

The form definition now looks like:-

<form action="semaphoreTest.php" method="get" name="SemaphoreTest" id="SemaphoreTest" >

The messages I am also expecting but not getting are:-

"Uninitialized........"

"Loading........"

"Loaded........" and

"Interactive........" from the Diva.js script.

 

BTW You can actually see the programs/scripts in action at http://www.stockton.co.za/semaphoreTest.html

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.