{"id":1108,"date":"2013-10-29T19:54:47","date_gmt":"2013-10-29T19:54:47","guid":{"rendered":"https:\/\/ibex.tech\/javascript\/?p=1108"},"modified":"2022-02-17T07:14:46","modified_gmt":"2022-02-17T07:14:46","slug":"basic-ajax-working-example","status":"publish","type":"post","link":"https:\/\/ibex.tech\/javascript\/ajax-javascript\/basic-ajax-working-example","title":{"rendered":"Basic AJAX Working Example"},"content":{"rendered":"<p>\nThis page has:\n<\/p>\n<p style=\"margin-left: 40px;\">\nA&nbsp;value which is read from the server via AJAX once per&nbsp;second and updated on the page<br \/>\nA button which sends a toggle on\/off action to the server via AJAX.<br \/>\n3 buttons which send a single button press action to the server via AJAX.\n<\/p>\n<p>\n&nbsp;\n<\/p>\n<h4>\nThe index.php file<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n&lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN&quot; &quot;http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd&quot;&gt;\r\n&lt;html xmlns=&quot;http:\/\/www.w3.org\/1999\/xhtml&quot;&gt;\r\n&lt;head&gt;\r\n&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text\/html; charset=utf-8&quot; \/&gt;\r\n&lt;title&gt;Control&lt;\/title&gt;\r\n\r\n&lt;script&gt;\r\nvar toggle1 = 0;\r\n\r\n\/\/************************************************\r\n\/\/************************************************\r\n\/\/********** BACKGROUND AJAX GET VALUES **********\r\n\/\/************************************************\r\n\/\/************************************************\r\nfunction ajax_get_values_timer_tick()\r\n{\r\n\t\/\/Setup to make an HTTP POST Ajax request\r\n\tAjaxRequest1Parameters = &quot;request_type=get_ajax_value1&quot;;\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;SET PARAMETER TO POST  (id1=val1&amp;id2=val2 for multiple parameters)\r\n\tAjaxRequest1 = new ajaxRequest();\r\n\tAjaxRequest1.open(&quot;POST&quot;, &quot;ajax.php&quot;, true);\t\t\t\t\t\t\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;SET THE FILE TO POST THE REQUEST TO\r\n\tAjaxRequest1.setRequestHeader(&quot;Content-type&quot;, &quot;application\/x-www-form-urlencoded; charset=UTF-8&quot;);\r\n\tAjaxRequest1.setRequestHeader(&quot;Content-length&quot;, AjaxRequest1Parameters.length);\r\n\tAjaxRequest1.setRequestHeader(&quot;Connection&quot;, &quot;close&quot;);\r\n\tAjaxRequest1.onreadystatechange = function()\r\n\t{\r\n\t\t\/\/-----------------------------------------------\r\n\t\t\/\/----- RESPONSE RECEIVED FROM AJAX REQUEST -----\r\n\t\t\/\/-----------------------------------------------\r\n\t\tif ((this.readyState == 4) &amp;&amp; (this.status == 200))\t\t\t\t\/\/4=Completed Ajax request, 200=Call succeeded\r\n\t\t{\r\n\t\t\tif (this.responseText != null)\t\/\/Check we got some response data (this.responseText or this.responseXML)\r\n\t\t\t{\r\n\t\t\t\t\/\/----- PROCESS THE RESPONSE -----\r\n\t\t\t\tdocument.getElementById(&#39;ajax_value1&#39;).innerHTML = this.responseText\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\t\r\n\t\/\/SEND THE AJAX REQUEST\r\n\tAjaxRequest1.send(AjaxRequest1Parameters);\t\t\t\/\/Use (null) if there are no parameters to send\r\n\r\n}\r\nsetInterval(&#39;ajax_get_values_timer_tick()&#39;, 1000 );\t\t \/\/Time in mS\r\n\r\n\r\n\r\n\/\/************************************************\r\n\/\/************************************************\r\n\/\/********** CREATE AJAX REQUEST OBJECT **********\r\n\/\/************************************************\r\n\/\/************************************************\r\nfunction ajaxRequest()\r\n{\r\n\t\/\/Cross browser - deal with IE varients\r\n\ttry\r\n\t{\r\n\t\t\/\/Non IE Browser\r\n\t\tvar request = new XMLHttpRequest();\r\n\t}\r\n\tcatch(e1)\r\n\t{\r\n\t\ttry\r\n\t\t{\r\n\t\t\t\/\/IE6?\r\n\t\t\trequest = new ActiveXObject(&quot;Msxml2.XMLHTTP&quot;);\r\n\t\t}\r\n\t\tcatch(e2)\r\n\t\t{\r\n\t\t\ttry\r\n\t\t\t{\r\n\t\t\t\t\/\/IE5?\r\n\t\t\t\trequest = new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);\r\n\t\t\t}\r\n\t\t\tcatch(e3)\r\n\t\t\t{\r\n\t\t\t\t\/\/There is no Ajax support in this browser\r\n\t\t\t\trequest = false;\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\treturn request;\r\n}\r\n\r\n\r\n\r\n\/\/************************************\r\n\/\/************************************\r\n\/\/********** BUTTON CLICKED **********\r\n\/\/************************************\r\n\/\/************************************\r\nfunction ajax_button_clicked (button_id)\r\n{\r\n\tvar parameters_to_send;\r\n\t\/\/----- PROCESS THE BUTTON PRESS -----\r\n\tswitch (button_id)\r\n\t{\r\n\tcase &#39;button_toggle1&#39;:\r\n\t\tif (toggle1)\r\n\t\t{\r\n\t\t\ttoggle1 = 0;\r\n\t\t\tdocument.getElementById(&#39;button_toggle1&#39;).innerHTML = &quot;Toggle 1 Off&quot;;\r\n\t\t\t\r\n\t\t\tparameters_to_send = &quot;action_id=1&quot;;\r\n\t\t}\r\n\t\telse\r\n\t\t{\r\n\t\t\ttoggle1 = 1;\r\n\t\t\tdocument.getElementById(&#39;button_toggle1&#39;).innerHTML = &quot;Toggle 1 On&quot;;\r\n\t\t\t\r\n\t\t\tparameters_to_send = &quot;action_id=2&quot;;\r\n\t\t}\r\n\t\tbreak;\r\n\t\t\r\n\tcase &#39;button_up&#39;:\r\n\t\tparameters_to_send = &quot;action_id=3&quot;;\r\n\t\tbreak;\r\n\t\t\r\n\tcase &#39;button_stop&#39;:\r\n\t\tparameters_to_send = &quot;action_id=4&quot;;\r\n\t\tbreak;\r\n\t\t\r\n\tcase &#39;button_down&#39;:\r\n\t\tparameters_to_send = &quot;action_id=5&quot;;\r\n\t\tbreak;\r\n\r\n\t} \/\/switch (button_id)\r\n\t\r\n\t\/\/----- SEND THE AJAX ACTION -----\r\n\tAjaxRequest1Parameters = parameters_to_send;\t\t\/\/&quot;action=do_something&quot;;\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;SET PARAMETER TO POST (id1=val1&amp;id2=val2 for multiple parameters)\r\n\tAjaxRequest1 = new ajaxRequest();\r\n\tAjaxRequest1.open(&quot;POST&quot;, &quot;ajax.php&quot;, true);\t\t\t\t\t\t\t\t\t\t\/\/&lt;&lt;&lt;&lt;&lt;SET THE FILE TO POST THE REQUEST TO\r\n\tAjaxRequest1.setRequestHeader(&quot;Content-type&quot;, &quot;application\/x-www-form-urlencoded; charset=UTF-8&quot;);\r\n\tAjaxRequest1.setRequestHeader(&quot;Content-length&quot;, AjaxRequest1Parameters.length);\r\n\tAjaxRequest1.setRequestHeader(&quot;Connection&quot;, &quot;close&quot;);\r\n\r\n\tAjaxRequest1.send(AjaxRequest1Parameters);\t\t\t\/\/Use (null) if there are no parameters to send\r\n}\r\n\r\n&lt;\/script&gt;\r\n\r\n\r\n&lt;\/head&gt;\r\n\r\n&lt;body&gt;\r\n\r\n&lt;p&gt;Current Value: &lt;span id=&#39;ajax_value1&#39;&gt;Retreiving...&lt;\/span&gt;&lt;\/p&gt;\r\n\r\n&lt;button id=&#39;button_toggle1&#39; onclick=&quot;ajax_button_clicked(&#39;button_toggle1&#39;)&quot;&gt;Toggle 1 Off&lt;\/button&gt;&lt;br \/&gt;\r\n&lt;button id=&#39;button_up&#39; onclick=&quot;ajax_button_clicked(&#39;button_up&#39;)&quot;&gt;Up&lt;\/button&gt;&lt;br \/&gt;\r\n&lt;button id=&#39;button_stop&#39; onclick=&quot;ajax_button_clicked(&#39;button_stop&#39;)&quot;&gt;Stop&lt;\/button&gt;&lt;br \/&gt;\r\n&lt;button id=&#39;button_down&#39; onclick=&quot;ajax_button_clicked(&#39;button_down&#39;)&quot;&gt;Down&lt;\/button&gt;&lt;br \/&gt;\r\n\r\n\r\n&lt;\/body&gt;\r\n&lt;\/html&gt;\r\n<\/code><\/pre>\n<h4>\nThe ajax.php file<br \/>\n<\/h4>\n<p>\nIn this example the file reads and writes to shared memory locations on the server, but you can change it to do whattever you want\n<\/p>\n<pre>\r\n<code>\r\n&lt;?php\r\nheader(&#39;Content-Type: text\/xml&#39;);\t\t\t\t\t\t\t\t\t\t\t\/\/Include these headers at the very start - they are essential to stop some browsers caching the ajax response\r\nheader(&quot;Cache-Control: no-cache, must-revalidate&quot;);\r\nheader(&quot;Expires: Mon, 26 Jul 1997 05:00:00 GMT&quot;);\t\t\t\/\/A date in the past\r\n\r\n\r\n\/\/--------------------------------\r\n\/\/--------------------------------\r\n\/\/----- ACCESS SHARED MEMORY -----\r\n\/\/--------------------------------\r\n\/\/--------------------------------\r\n\r\n\/\/----- SHARED MEMORY CONFIGURATION -----\r\n$SEMAPHORE_KEY = 293623706;   \t\t\t\/\/Semaphore unique key\r\n$SHARED_MEMORY_KEY = 662213404;   \t\/\/Shared memory unique key\r\n\r\n\/\/Create the semaphore\r\n$semaphore_id = sem_get($SEMAPHORE_KEY, 1);\t\t\/\/Creates, or gets if already present, a semaphore\r\n\r\n\/\/Acquire the semaphore\r\nsem_acquire($semaphore_id);\t\t\t\t\t\t\/\/If not available this will stall until the semaphore is released by the other process\r\n\r\n\/\/We have exclusive access to the shared memory (the other process is unable to aquire the semaphore until we release it)\r\n\r\n\/\/Setup access to the shared memory\r\n$shared_memory_id = shmop_open($SHARED_MEMORY_KEY, &quot;w&quot;, 0, 0);\t\/\/Shared memory key, flags, permissions, size (permissions &amp; size are 0 to open an existing memory segment)\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/flags: &quot;a&quot; open an existing shared memory segment for read only, &quot;w&quot; read and write to a shared memory segment\r\nif (empty($shared_memory_id))\r\n{\r\n\techo &quot;Failed to open shared memory.&lt;br \/&gt;&quot;;\t\t\t\/\/&lt;&lt;&lt;&lt; THIS WILL HAPPEN IF THE C APPLICATION HASN&#39;T CREATED THE SHARED MEMORY OR IF IT HAS BEEN SHUTDOWN AND DELETED THE SHARED MEMORY\r\n}\r\nelse\r\n{\r\n\t\/\/--------------------------------------------\r\n\t\/\/----- READ AND WRITE THE SHARED MEMORY -----\r\n\t\/\/--------------------------------------------\r\n\r\n\t\/\/---------------------------------------\r\n\t\/\/----- READ FROM THE SHARED MEMORY -----\r\n\t\/\/---------------------------------------\r\n\t$shared_memory_string = shmop_read($shared_memory_id, 0, 32);\t\t\t\t\/\/Shared memory ID, Start Index, Number of bytes to read\r\n\tif($shared_memory_string == FALSE) \r\n\t{\r\n\t\t\techo &quot;Failed to read shared memory&quot;;\r\n\t\t\tsem_release($semaphore_id);\r\n\t\t\texit;\r\n\t}\r\n\t\/\/CONVERT TO AN ARRAY OF BYTE VALUES\r\n\t$shared_memory_array = array_slice(unpack(&#39;C*&#39;, &quot;\\0&quot;.$shared_memory_string), 1);\r\n\r\n\r\n\t$index = 0;\r\n\r\n\t\/\/1:0\r\n\t$ajax_value1 = (int)($shared_memory_array[$index++]) &lt;&lt; 8;\r\n\t$ajax_value1 |= $shared_memory_array[$index++];\r\n\r\n\t\/\/31:2\r\n\t\/\/Unused\r\n\r\n\tif (isset($_POST['request_type']))\r\n\t{\r\n\t\t$request_type = $_POST['request_type'];\r\n\t\tif ($request_type == &quot;get_ajax_value1&quot;)\r\n\t\t{\r\n\t\t\t\/\/------------------------------------------\r\n\t\t\t\/\/------------------------------------------\r\n\t\t\t\/\/----- AJAX REQUEST - GET ajax_value1 -----\r\n\t\t\t\/\/------------------------------------------\r\n\t\t\t\/\/------------------------------------------\r\n\t\t\techo($ajax_value1);\r\n\t\t}\r\n\t\t\r\n\t} \/\/if (isset($_POST['request_type']))\r\n\r\n\r\n\tif (isset($_POST['action_id']))\r\n\t{\r\n\t\t\/\/--------------------------------\r\n\t\t\/\/--------------------------------\r\n\t\t\/\/----- AJAX ACTION RECEIVED -----\r\n\t\t\/\/--------------------------------\r\n\t\t\/\/--------------------------------\r\n\t\t$action_id = $_POST['action_id'];\r\n\t\t$action_id = (int)$action_id;\r\n\t\tif (($action_id &lt; 0) || ($action_id &gt; 255))\r\n\t\t\t$action_id = 0;\r\n\t\t\r\n\t\t\/\/--------------------------------------\r\n\t\t\/\/----- WRITE TO THE SHARED MEMORY -----\r\n\t\t\/\/--------------------------------------\r\n\t\t\/\/The array to write\r\n\t\t$shared_memory_array = array($action_id, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);\t\t\/\/16 bytes\r\n\t\t\r\n\t\t\/\/Convert the array of byte values to a byte string\r\n\t\t$shared_memory_string = call_user_func_array(pack, array_merge(array(&quot;C*&quot;), $shared_memory_array));\r\n\t\tshmop_write($shared_memory_id, $shared_memory_string, 32);\t\t\t\/\/Shared memory id, string to write, Index to start writing from\r\n\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\/\/Note that a trailing null 0x00 byte is not written, just the byte values\r\n\t}\r\n\r\n\t\/\/Detach from the shared memory\r\n\tshmop_close($shared_memory_id);\r\n}\r\n\r\n\/\/Release the semaphore\r\nsem_release($semaphore_id);\t\t\t\t\/\/Must be called after sem_acquire() so that another process can acquire the semaphore\r\n\r\n?&gt;\r\n\r\n\r\n<\/code><\/pre>\n<p>\n&nbsp;\n<\/p>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This page has: A&nbsp;value which is read from the server via AJAX once per&nbsp;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. &nbsp; The index.php file &lt;!DOCTYPE html PUBLIC &quot;-\/\/W3C\/\/DTD XHTML [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[112,100],"tags":[],"class_list":["post-1108","post","type-post","status-publish","format-standard","hentry","category-examples-javascript","category-ajax-javascript"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/1108","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/comments?post=1108"}],"version-history":[{"count":2,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/1108\/revisions"}],"predecessor-version":[{"id":1112,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/posts\/1108\/revisions\/1112"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/media?parent=1108"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/categories?post=1108"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/javascript\/wp-json\/wp\/v2\/tags?post=1108"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}