<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>PHP and Web Development Blog &#187; AJAX</title>
	<atom:link href="http://www.ebrueggeman.com/blog/tag/ajax/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ebrueggeman.com/blog</link>
	<description>Tips and Tricks for Web Developers</description>
	<lastBuildDate>Tue, 03 Aug 2010 17:21:32 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using a Timer with AJAX</title>
		<link>http://www.ebrueggeman.com/blog/using-a-timer-with-ajax/</link>
		<comments>http://www.ebrueggeman.com/blog/using-a-timer-with-ajax/#comments</comments>
		<pubDate>Thu, 17 Jan 2008 03:46:56 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[timer]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/php/using-a-timer-with-ajax/</guid>
		<description><![CDATA[Background
In the previous article, we examined the PHP AJAX Framework, which is an easy way of combining AJAX with PHP. In order to trigger our AJAX, we relied on user interaction. However, what happens when you want your AJAX call to execute at a certain time interval? The solution is relatively easy and involves the [...]]]></description>
			<content:encoded><![CDATA[<h2>Background</h2>
<p>In the previous article, we examined the <a href="http://www.ebrueggeman.com/blog/php/php-ajax-framework/">PHP AJAX Framework</a>, which is an easy way of combining AJAX with PHP. In order to trigger our AJAX, we relied on user interaction. However, what happens when you want your AJAX call to execute at a certain time interval? The solution is relatively easy and involves the JavaScript function setInterval().</p>
<h2>AJAX Timer Code</h2>
<p>Below is a snippet of JavaScript code that will work with my PHP AJAX Framework to call the sales_calculate hook every 5 seconds:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>script language<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;JavaScript&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #003366; font-weight: bold;">var</span> time <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span>; <span style="color: #006600; font-style: italic;">//time in seconds</span>
<span style="color: #003366; font-weight: bold;">var</span> interval <span style="color: #339933;">=</span> time <span style="color: #339933;">*</span> <span style="color: #CC0000;">1000</span>;
<span style="color: #003366; font-weight: bold;">var</span> timer <span style="color: #339933;">=</span> setInterval<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ajaxHelper('sales_calculate')&quot;</span><span style="color: #339933;">,</span> interval<span style="color: #009900;">&#41;</span>;
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span></pre></div></div>

<p>
The above code will execute our ajaxHelper() function with the sales_calculate hook that will in turn call sales_calculate_setup() and then sales_calculate_ajax() every 5 seconds.</p>
<p>
Note that this code is meant to accompany my PHP Framework, and will not trigger an AJAX call unless properly configured. Please refer to the prior <a href="http://www.ebrueggeman.com/blog/php/php-ajax-framework/">AJAX Framework</a> article for more information.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/using-a-timer-with-ajax/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP AJAX Framework</title>
		<link>http://www.ebrueggeman.com/blog/php-ajax-framework/</link>
		<comments>http://www.ebrueggeman.com/blog/php-ajax-framework/#comments</comments>
		<pubDate>Thu, 10 Jan 2008 02:17:00 +0000</pubDate>
		<dc:creator>Elliott Brueggeman</dc:creator>
				<category><![CDATA[AJAX]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[framework]]></category>

		<guid isPermaLink="false">http://www.ebrueggeman.com/blog/php/php-ajax-framework/</guid>
		<description><![CDATA[Background
One of the most useful new web technologies is AJAX, or Asynchronous JavaScript and XML. In short, AJAX allows you to send and receive server requests without a user having to reload the page. This allows pages to be more dynamic without interrupting the user experience.
AJAX is fairly easy to implement and use effectively, but [...]]]></description>
			<content:encoded><![CDATA[<h3>Background</h3>
<p>One of the most useful new web technologies is AJAX, or Asynchronous JavaScript and XML. In short, AJAX allows you to send and receive server requests without a user having to reload the page. This allows pages to be more dynamic without interrupting the user experience.</p>
<p>AJAX is fairly easy to implement and use effectively, but finding a comprehensive start to finish tutorial for integration with PHP is not quite as easy. To make the task easier, I have put together a simple PHP AJAX framework that is flexible and easy to implement.</p>
<h3>AJAX Workflow</h3>
<p>The inner workings of AJAX are a mystery for some so I will go over the basics of how our framework will work with PHP.</p>
<ol>
<li>Using JavaScript, a call to your AJAX function is triggered by clicking somewhere, entering something, etc.</li>
<li>Your AJAX function creates an XMLHttpRequest object.</li>
<li>When ready, the object requests information from your server in the form of a URL, possibly with arguments such as myscript.php?name=jon&#038;year=2007, etc.</li>
<li>The server executes the PHP script and sends the result by echo[ing] it out.</li>
<li>Instead of printing it to the screen, that information is instead returned to you as a JavaScript variable.</li>
<li>Using JavaScript, that information can now be dynamically inserted in the page by using the innerHTML property or by assigning the result to the value of a form element.</li>
<li>Rinse and Repeat&#8230; The value of AJAX is that you can repeat this process infinitely without having the user have to navigate away from the page!</li>
</ol>
<h3>PHP AJAX Framework</h3>
<p>The PHP AJAX Framework shown below is structured into three parts. The part that stays the same every time is the JavaScript ajaxHelper() function. This is the core of our AJAX functionality. We also need to create two additional JavaScript functions. The code of these functions is different depending on what you want to do. These functions or hooks will be called by the ajaxHelper() function and their names must end in &#8220;_init&#8221; and &#8220;_ajax&#8221; which will be explained below.</p>
<p><b>The ajaxHelper() function</b></p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> ajaxHelper<span style="color: #009900;">&#40;</span>functionName<span style="color: #339933;">,</span> additionalArgs<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> xmlHttp;
  <span style="color: #006600; font-style: italic;">// Firefox, Opera 8.0+, Safari, SeaMonkey</span>
  <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    xmlHttp<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Internet Explorer</span>
    <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
      xmlHttp<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Msxml2.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000066; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        xmlHttp<span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">new</span> ActiveXObject<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Microsoft.XMLHTTP&quot;</span><span style="color: #009900;">&#41;</span>;
      <span style="color: #009900;">&#125;</span>
      <span style="color: #000066; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Sorry, your browser does not support AJAX.&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span>;
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  xmlHttp.<span style="color: #660066;">onreadystatechange</span><span style="color: #339933;">=</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">//The request is complete == state 4</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttp.<span style="color: #660066;">readyState</span><span style="color: #339933;">==</span><span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #003366; font-weight: bold;">var</span> response<span style="color: #339933;">=</span>xmlHttp.<span style="color: #660066;">responseText</span>;
      <span style="color: #006600; font-style: italic;">//Send reponse to _ajax hook of passed function name</span>
      <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>functionName <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;_ajax&quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'(<span style="color: #000099; font-weight: bold;">\'</span>'</span> <span style="color: #339933;">+</span> response <span style="color: #339933;">+</span> <span style="color: #3366CC;">'<span style="color: #000099; font-weight: bold;">\'</span>)'</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">//Get request string from _setup hook of passed function name</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>additionalArgs <span style="color: #339933;">!==</span> undefined <span style="color: #339933;">&amp;&amp;</span> additionalArgs.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> requestString <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>functionName<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_init&quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'('</span> <span style="color: #339933;">+</span> additionalArgs <span style="color: #339933;">+</span> <span style="color: #3366CC;">')'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> requestString <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">eval</span><span style="color: #009900;">&#40;</span>functionName<span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;_init&quot;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'()'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span> 
&nbsp;
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>requestString<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    xmlHttp.<span style="color: #000066;">open</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;GET&quot;</span><span style="color: #339933;">,</span> requestString<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>;
    xmlHttp.<span style="color: #660066;">send</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><b>The _init and _ajax functions</b></p>
<p>As stated above, these are functions are hooks, meaning that they will be called dynamically by the ajaxHelper() function depending on the argument you pass it. For example, on a button click you execute the ajaxHelper(&#8217;chat_update&#8217;) function. The ajaxHelper will call chat_update_init() and then chat_update_ajax() automatically, so you must have them defined. By making them dynamic hooks, you can have many different AJAX calls on the same page but use the same ajaxHelper() function each time. You pass the ajaxHelper() function the first part of the name of your two additional functions (minus the _init and _ajax), which is &#8220;chat_update&#8221; in the above example.</p>
<p>The _init hook function specifies the name of the PHP file and any arguments to be passed to the ajaxHelper() function. An example _init hook is as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> my_first_try_init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #006600; font-style: italic;">//retrieve username from html input box with the id &quot;name_box&quot;</span>
  <span style="color: #003366; font-weight: bold;">var</span> username <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'name_box'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #003366; font-weight: bold;">var</span> script <span style="color: #339933;">=</span> <span style="color: #3366CC;">'myscript.php'</span>;
  <span style="color: #003366; font-weight: bold;">var</span> queryString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;?username=&quot;</span> <span style="color: #339933;">+</span> username;
  <span style="color: #000066; font-weight: bold;">return</span> script <span style="color: #339933;">+</span> queryString;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The _ajax function is passed the results of the PHP script specified in the _init hook as a JavaScript variable. Using JavaScript trickery, we can update the page that the user is viewing. You can use the .innerHTML property and change the contents of a div or other element. Or, you could even change the text of input boxes by changing the .value property. This may sound confusing, but study the below examples for a better understanding.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> my_first_try_ajax<span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> targetDiv <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'firstDiv'</span><span style="color: #009900;">&#41;</span>;
  <span style="color: #003366; font-weight: bold;">var</span> resultHTML<span style="color: #339933;">=</span><span style="color: #3366CC;">'&lt;h1&gt;'</span> <span style="color: #339933;">+</span> results <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&lt;/h1&gt;'</span>;
  targetDiv.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> resultHTML;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The above code replaces the html within a div with the id &#8220;firstDiv&#8221; with whatever has been returned by myscript.php wrapped in h1 tags. You could have an empty div and only insert html into it on AJAX calls. The div would look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;firstDiv&quot;&gt;&lt;/div&gt;</pre></div></div>

<p>While declaring an _ajax hook function is required, adding JavaScript code inside the function is optional. Why? Because often you do not want to change content on the user&#8217;s page, but instead just update values in the database on your call to the script specified in the _init hook. In that case, nothing would need to be updated on the page.</p>
<h3>Your PHP Script</h3>
<p>Creating your PHP script that will be called by the AJAX framework is easy if you know PHP. You can have your PHP script do pretty much anything. You probably want to either return some value or values, update a database, or both. The only thing to note is that to return a value, you need to &#8220;echo&#8221; it out. Values printed to the screen from your PHP callback script are returned to the the JavaScript _ajax() hook function as the results variable, as seen above.</p>
<p>How do you return multiple results? Well the implementation differs depending on what you want to return, but if you want to return an array you simply echo out a comma separated list in your PHP script and then you split the returned JavaScript variable by the commas and loop through the values in your _ajax() function and do whatever. Here is an example of a php script that connects to a database and returns the current sales totals for the year:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//connect to database</span>
<span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="">'localhost'</span><span style="color: #339933;">,</span> <span style="">'username'</span><span style="color: #339933;">,</span> <span style="">'password'</span><span style="color: #009900;">&#41;</span>
  or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Could not connect: '</span> <span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="">'sales_db'</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="">'Could not select database'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #000088;">$currentYear</span> <span style="color: #339933;">=</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="">'Y'</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//execute query</span>
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;SELECT SUM(sale_total) as `total`
  FROM sales_table WHERE year = $currentYear&quot;</span>;
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//retrieve results</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_assoc</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$total</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;total&quot;</span><span style="color: #009900;">&#93;</span>;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">//return via echo to our JavaScript _ajax function</span>
<span style="color: #990000;">echo</span> <span style="color: #000088;">$total</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>Here is another example that returns an array of values:</p>

<div class="wp_syntax"><div class="code"><pre class="php php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #666666; font-style: italic;">//[not shown] do a database call and retrieve an array</span>
<span style="color: #000088;">$valueArray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">23</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">231</span><span style="color: #339933;">,</span> <span style="">'roger 123'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">26</span><span style="color: #339933;">,</span> <span style="">'Las Veags,
  Nevada'</span><span style="color: #339933;">,</span> <span style="">'Washington'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">77</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #666666; font-style: italic;">//replace commas with their html value so they do not</span>
<span style="color: #666666; font-style: italic;">//get in the way of separating out the array</span>
<span style="color: #666666; font-style: italic;">// values with commas via the implode statement</span>
<span style="color: #990000;">echo</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="">', '</span><span style="color: #339933;">,</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="">','</span><span style="color: #339933;">,</span> <span style="">'&amp;#44;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$valueArray</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p><b>Activating AJAX</b></p>
<p>
Finally, the only thing we are missing is how we trigger our AJAX call. There are many ways to do this. The most common method is when a user interacts with an element on the page, triggering the AJAX helper function, but this is not the only way. You can also use a JavaScript timer and call the AJAX at a set interval. This method will be covered in a future article. Let&#8217;s look at some common examples of triggering AJAX through user interaction:
</p>
<p>Button Click</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;button&quot; id=&quot;trig&quot; value=&quot;ClickMe&quot; onClick=&quot;ajaxHelper('my_first_ajax');&quot; &gt;</pre></div></div>

<p>Mouse-Over a Div</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div id=&quot;TriggerDiv&quot; onMouseOver=&quot;ajaxHelper('sales_calculate');&quot; &gt;&lt;/div&gt;</pre></div></div>

<p>Click in a Text Box</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;input type=&quot;text&quot; name=&quot;datefield&quot; onClick=&quot;ajaxHelper('update_date');&quot; &gt;</pre></div></div>

<h3>Conclusion</h3>
<p>
The uses for AJAX are endless, especially coupled with PHP. Explore, experiment, and see what&#8217;s best for your website.
</p>
<div style="background-color: #FFFBCC; border: 1px solid #E6DB55; padding: 4px; margin-top:15px">
<h3>Update &#8211; September 15th, 2009</h3>
</div>
<p>
I added an optional second parameter to the ajaxHelper() function call &#8211; this allows you to pass additional variables to the PHP script via Javascript. Below is an example of passing an array of three values to the php script.</p>
<p>Example Usage</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;a href=&quot;#&quot; onclick=&quot;ajaxHelper('track_click', new Array(1303432, 26));&quot;&gt;Click Here&lt;/a&gt;</pre></div></div>

<p>To utilize these values, add parameters to your _init hook function for each value passed. The below _init hook function takes each of the passed values and then puts them into a url friendly format to be passed to your PHP script.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> track_click_init<span style="color: #009900;">&#40;</span>arg0<span style="color: #339933;">,</span> arg1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #003366; font-weight: bold;">var</span> script <span style="color: #339933;">=</span> <span style="color: #3366CC;">'onclick.php'</span>;
	<span style="color: #003366; font-weight: bold;">var</span> queryString <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;?id=&quot;</span> <span style="color: #339933;">+</span> arg0 <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;&amp;age=&quot;</span> <span style="color: #339933;">+</span> arg1;
&nbsp;
	<span style="color: #000066; font-weight: bold;">return</span> script <span style="color: #339933;">+</span> queryString;
<span style="color: #009900;">&#125;</span></pre></div></div>
</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ebrueggeman.com/blog/php-ajax-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
