<?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>Chris Cook .me &#187; chriscook.me</title>
	<atom:link href="http://chriscook.me/tag/chriscook-me/feed/" rel="self" type="application/rss+xml" />
	<link>http://chriscook.me</link>
	<description>Homepage</description>
	<lastBuildDate>Sat, 23 Oct 2010 22:53:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>PHP/AJAX: Call PHP function by clicking a link</title>
		<link>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/</link>
		<comments>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/#comments</comments>
		<pubDate>Sat, 15 Aug 2009 22:14:24 +0000</pubDate>
		<dc:creator>Chris</dc:creator>
				<category><![CDATA[Web Development]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[chris cook]]></category>
		<category><![CDATA[chriscook.me]]></category>
		<category><![CDATA[execute php function in ajax request]]></category>
		<category><![CDATA[link]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://chriscook.me/?p=46</guid>
		<description><![CDATA[This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission. 1) Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;. {code type=html} /* * ajax_click.js [...]]]></description>
			<content:encoded><![CDATA[<p>This tutorial demonstrates how to execute an external PHP function by clicking a simple link within HTML.  The method uses AJAX so that the page doesn&#8217;t refresh and doesn&#8217;t require a form submission.</p>
<p><strong>1)</strong> Paste the following code into a .js file.  For demonstration purposes, we have named it &#8220;ajax_click.js&#8221;.</p>
<p>{code type=html}</p>
<p>/*<br />
* ajax_click.js<br />
*  chriscook.me<br />
*/</p>
<p>function loadurl(dest) {</p>
<p>try {<br />
// Moz supports XMLHttpRequest. IE uses ActiveX.<br />
// browser detction is bad. object detection works for any browser<br />
xmlhttp = window.XMLHttpRequest?new XMLHttpRequest(): new ActiveXObject(&#8220;Microsoft.XMLHTTP&#8221;);<br />
} catch (e) {<br />
// browser doesn&#8217;t support ajax. handle however you want<br />
}</p>
<p>// the xmlhttp object triggers an event everytime the status changes<br />
// triggered() function handles the events<br />
xmlhttp.onreadystatechange = triggered;</p>
<p>// open takes in the HTTP method and url.<br />
xmlhttp.open(&#8220;GET&#8221;, dest);</p>
<p>// send the request. if this is a POST request we would have<br />
// sent post variables: send(&#8220;name=aleem gender=male)<br />
// Moz is fine with just send(); but<br />
// IE expects a value here, hence we do send(null);<br />
xmlhttp.send(&#8220;null&#8221;);<br />
}</p>
<p>function triggered() {<br />
if ((xmlhttp.readyState == 4) (xmlhttp.status == 200)) {</p>
<p>document.getElementById(&#8220;ajaxlink&#8221;).innerHTML = xmlhttp.responseText;<br />
}<br />
}<br />
{/code}</p>
<p><strong>2)</strong> Next, add the following code in the  section of your HTML file.</p>
<p>{code type=html}&lt;script src=&#8221;ajax_link.js&#8221; type=&#8221;text/javascript&#8221;&gt;&lt;/script&gt;{/code}</p>
<p><strong>3)</strong> The following code should be placed in the HTML body of a PHP file.</p>
<p>{code type=html}</p>
<p>&lt;div id=&#8221;ajaxlink&#8221; onclick=&#8221;loadurl(&#8216;ajax_function.php&#8217;)&#8221;&gt;Click Here&lt;/div&gt;</p>
<p>{/code}</p>
<p>Replace &#8216;ajax_function.php&#8217; with the correct file you want to execute.  For instance, if you want your users to &#8220;click here&#8221; in order to send themselves a copy of their monthly invoice, you&#8217;d code the mailer function in &#8216;ajax_function.php&#8217;.</p>
<p><strong>4) </strong>That&#8217;s it.  Ensure that you keep the &lt;div&gt; id  as &#8220;ajaxlink&#8221;.  Once you click the link, it will disappear (until the page is refreshed).</p>
<p><strong>Feedback:</strong> I&#8217;d love to hear how you&#8217;ve used this tool.  Please feel free to post a comment on my blog.</p>
<p><strong>Disclaimer: </strong> This tutorial is provided to demonstrate how to perform the function.  Please ensure that you review the code and add security measures before using this in a production environment.</p>
]]></content:encoded>
			<wfw:commentRss>http://chriscook.me/web-development/phpajax-execute-php-function-by-clicking-a-link/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

