This page has:
A value which is read from the server via AJAX once per second and updated on the page
A button which sends a toggle on/off action to the server via AJAX.
3 buttons which send a single button press action to the server via AJAX.
The index.php file
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Control</title>
<script>
var toggle1 = 0;
//************************************************
//************************************************
//********** BACKGROUND AJAX GET VALUES **********
//************************************************
//************************************************
function ajax_get_values_timer_tick()
{
//Setup to make an HTTP POST Ajax request
AjaxRequest1Parameters = "request_type=get_ajax_value1"; //<<<<<SET PARAMETER TO POST (id1=val1&id2=val2 for multiple parameters)
AjaxRequest1 = new ajaxRequest();
AjaxRequest1.open("POST", "ajax.php", true); //<<<<<SET THE FILE TO POST THE REQUEST TO
AjaxRequest1.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
AjaxRequest1.setRequestHeader("Content-length", AjaxRequest1Parameters.length);
AjaxRequest1.setRequestHeader("Connection", "close");
AjaxRequest1.onreadystatechange = function()
{
//-----------------------------------------------
//----- RESPONSE RECEIVED FROM AJAX REQUEST -----
//-----------------------------------------------
if ((this.readyState == 4) && (this.status == 200)) //4=Completed Ajax request, 200=Call succeeded
{
if (this.responseText != null) //Check we got some response data (this.responseText or this.responseXML)
{
//----- PROCESS THE RESPONSE -----
document.getElementById('ajax_value1').innerHTML = this.responseText
}
}
}
//SEND THE AJAX REQUEST
AjaxRequest1.send(AjaxRequest1Parameters); //Use (null) if there are no parameters to send
}
setInterval('ajax_get_values_timer_tick()', 1000 ); //Time in mS
//************************************************
//************************************************
//********** CREATE AJAX REQUEST OBJECT **********
//************************************************
//************************************************
function ajaxRequest()
{
//Cross browser - deal with IE varients
try
{
//Non IE Browser
var request = new XMLHttpRequest();
}
catch(e1)
{
try
{
//IE6?
request = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e2)
{
try
{
//IE5?
request = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e3)
{
//There is no Ajax support in this browser
request = false;
}
}
}
return request;
}
//************************************
//************************************
//********** BUTTON CLICKED **********
//************************************
//************************************
function ajax_button_clicked (button_id)
{
var parameters_to_send;
//----- PROCESS THE BUTTON PRESS -----
switch (button_id)
{
case 'button_toggle1':
if (toggle1)
{
toggle1 = 0;
document.getElementById('button_toggle1').innerHTML = "Toggle 1 Off";
parameters_to_send = "action_id=1";
}
else
{
toggle1 = 1;
document.getElementById('button_toggle1').innerHTML = "Toggle 1 On";
parameters_to_send = "action_id=2";
}
break;
case 'button_up':
parameters_to_send = "action_id=3";
break;
case 'button_stop':
parameters_to_send = "action_id=4";
break;
case 'button_down':
parameters_to_send = "action_id=5";
break;
} //switch (button_id)
//----- SEND THE AJAX ACTION -----
AjaxRequest1Parameters = parameters_to_send; //"action=do_something"; //<<<<<SET PARAMETER TO POST (id1=val1&id2=val2 for multiple parameters)
AjaxRequest1 = new ajaxRequest();
AjaxRequest1.open("POST", "ajax.php", true); //<<<<<SET THE FILE TO POST THE REQUEST TO
AjaxRequest1.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
AjaxRequest1.setRequestHeader("Content-length", AjaxRequest1Parameters.length);
AjaxRequest1.setRequestHeader("Connection", "close");
AjaxRequest1.send(AjaxRequest1Parameters); //Use (null) if there are no parameters to send
}
</script>
</head>
<body>
<p>Current Value: <span id='ajax_value1'>Retreiving...</span></p>
<button id='button_toggle1' onclick="ajax_button_clicked('button_toggle1')">Toggle 1 Off</button><br />
<button id='button_up' onclick="ajax_button_clicked('button_up')">Up</button><br />
<button id='button_stop' onclick="ajax_button_clicked('button_stop')">Stop</button><br />
<button id='button_down' onclick="ajax_button_clicked('button_down')">Down</button><br />
</body>
</html>
The ajax.php file
In this example the file reads and writes to shared memory locations on the server, but you can change it to do whattever you want
<?php
header('Content-Type: text/xml'); //Include these headers at the very start - they are essential to stop some browsers caching the ajax response
header("Cache-Control: no-cache, must-revalidate");
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); //A date in the past
//--------------------------------
//--------------------------------
//----- ACCESS SHARED MEMORY -----
//--------------------------------
//--------------------------------
//----- SHARED MEMORY CONFIGURATION -----
$SEMAPHORE_KEY = 293623706; //Semaphore unique key
$SHARED_MEMORY_KEY = 662213404; //Shared memory unique key
//Create the semaphore
$semaphore_id = sem_get($SEMAPHORE_KEY, 1); //Creates, or gets if already present, a semaphore
//Acquire the semaphore
sem_acquire($semaphore_id); //If not available this will stall until the semaphore is released by the other process
//We have exclusive access to the shared memory (the other process is unable to aquire the semaphore until we release it)
//Setup access to the shared memory
$shared_memory_id = shmop_open($SHARED_MEMORY_KEY, "w", 0, 0); //Shared memory key, flags, permissions, size (permissions & size are 0 to open an existing memory segment)
//flags: "a" open an existing shared memory segment for read only, "w" read and write to a shared memory segment
if (empty($shared_memory_id))
{
echo "Failed to open shared memory.<br />"; //<<<< THIS WILL HAPPEN IF THE C APPLICATION HASN'T CREATED THE SHARED MEMORY OR IF IT HAS BEEN SHUTDOWN AND DELETED THE SHARED MEMORY
}
else
{
//--------------------------------------------
//----- READ AND WRITE THE SHARED MEMORY -----
//--------------------------------------------
//---------------------------------------
//----- READ FROM THE SHARED MEMORY -----
//---------------------------------------
$shared_memory_string = shmop_read($shared_memory_id, 0, 32); //Shared memory ID, Start Index, Number of bytes to read
if($shared_memory_string == FALSE)
{
echo "Failed to read shared memory";
sem_release($semaphore_id);
exit;
}
//CONVERT TO AN ARRAY OF BYTE VALUES
$shared_memory_array = array_slice(unpack('C*', "\0".$shared_memory_string), 1);
$index = 0;
//1:0
$ajax_value1 = (int)($shared_memory_array[$index++]) << 8;
$ajax_value1 |= $shared_memory_array[$index++];
//31:2
//Unused
if (isset($_POST['request_type']))
{
$request_type = $_POST['request_type'];
if ($request_type == "get_ajax_value1")
{
//------------------------------------------
//------------------------------------------
//----- AJAX REQUEST - GET ajax_value1 -----
//------------------------------------------
//------------------------------------------
echo($ajax_value1);
}
} //if (isset($_POST['request_type']))
if (isset($_POST['action_id']))
{
//--------------------------------
//--------------------------------
//----- AJAX ACTION RECEIVED -----
//--------------------------------
//--------------------------------
$action_id = $_POST['action_id'];
$action_id = (int)$action_id;
if (($action_id < 0) || ($action_id > 255))
$action_id = 0;
//--------------------------------------
//----- WRITE TO THE SHARED MEMORY -----
//--------------------------------------
//The array to write
$shared_memory_array = array($action_id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0); //16 bytes
//Convert the array of byte values to a byte string
$shared_memory_string = call_user_func_array(pack, array_merge(array("C*"), $shared_memory_array));
shmop_write($shared_memory_id, $shared_memory_string, 32); //Shared memory id, string to write, Index to start writing from
//Note that a trailing null 0x00 byte is not written, just the byte values
}
//Detach from the shared memory
shmop_close($shared_memory_id);
}
//Release the semaphore
sem_release($semaphore_id); //Must be called after sem_acquire() so that another process can acquire the semaphore
?>
Feel free to comment if you can add help to this page or point out issues and solutions you have found. I do not provide support on this site, if you need help with a problem head over to stack overflow.