<?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>JaffnaCampus.com &#187; PHP</title>
	<atom:link href="http://jaffnacampus.com/category/php/feed/" rel="self" type="application/rss+xml" />
	<link>http://jaffnacampus.com</link>
	<description>Gateway to your IT potential</description>
	<lastBuildDate>Tue, 27 Sep 2011 13:57:48 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>What is PHP?</title>
		<link>http://jaffnacampus.com/php/what-is-php/</link>
		<comments>http://jaffnacampus.com/php/what-is-php/#comments</comments>
		<pubDate>Sun, 28 Feb 2010 01:39:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[about php]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=380</guid>
		<description><![CDATA[Hypertext Preprocessor (the name is a recursive acronym) is a widely used, general-purpose scripting language that was originally designed for [...]]]></description>
			<content:encoded><![CDATA[<p>Hypertext Preprocessor (the name is a recursive acronym) is a widely used, general-purpose scripting language that was originally designed for web development to produce dynamic web pages. For this purpose, PHP code is embedded into the HTML source document and interpreted by a web server with a PHP processor module, which generates the web page  document. As a general-purpose programming language, PHP code is processed by an interpreter application in command-line mode performing desired operating system operations and producing program output on its standard output channel. It may also function as a graphical application. PHP is available as a processor for most modern web servers and as standalone interpreter on most operating systems and computing platforms.</p>
<p>PHP was originally created by Rasmus Lerdorf in 1995 and has been in continuous development ever since. The main implementation of PHP is now produced by The PHP Group and serves as the de facto standard for PHP as there is no formal specification. PHP is free software released under the PHP License, which is incompatible with the GNU General Public License (GPL) because restrictions exist regarding the use of the term PHP.</p>
<p>PHP originally stood for personal home page. It began in 1994 as a set of Common Gateway Interface (CGI) binaries  written in the C programming language by the Danish/Greenlandic  programmer Rasmus Lerdorf. Lerdorf initially created these Personal Home Page Tools to replace a small set of Perl  scripts he had been using to maintain his personal homepage. The tools were used to perform tasks such as displaying his résumé and recording how much traffic  his page was receiving. He combined these binaries with his Form Interpreter to create PHP/FI, which had more functionality. PHP/FI included a larger implementation for the C programming language and could communicate with databases, enabling the building of simple, dynamic web applications. Lerdorf released PHP publicly on June 8, 1995 to accelerate bug  location and improve the code. This release was named PHP version 2 and already had the basic functionality that PHP has today. This included Perl-like variables, form handling, and the ability to embed HTML. The syntax was similar to Perl but was more limited, simpler, and less consistent.</p>
<p>Zeev Suraski and Andi Gutmans, two Israeli developers at the Technion IIT, rewrote the parser in 1997 and formed the base of PHP 3, changing the language&#8217;s name to the recursive initialism PHP: Hypertext Preprocessor. The development team officially released PHP/FI 2 in November 1997 after months of beta testing. Afterwards, public testing of PHP 3 began, and the official launch came in June 1998. Suraski and Gutmans then started a new rewrite of PHP&#8217;s core, producing the Zend Engine in 1999. They also founded Zend Technologies in Ramat Gan, Israel.</p>
<p>On May 22, 2000, PHP 4, powered by the Zend Engine 1.0, was released. As of August, 2008 this branch is up to version 4.4.9. PHP 4 is no longer under development nor will any security updates be released. On July 13, 2004, PHP 5 was released, powered by the new Zend Engine II. PHP 5 included new features such as improved support for object-oriented programming, the PHP Data Objects extension (which defines a lightweight and consistent interface for accessing databases), and numerous performance enhancements. In 2008, PHP 5 became the only stable version under development. Late static binding has been missing from PHP and has been added in version 5.3. PHP 6 is under development alongside PHP 5. Major changes include the removal of register_globals, magic quotes, and safe mode. The reason for the removals was that register_globals had given way to security holes, and magic quotes had an unpredictable nature, and was best avoided. Instead, to escape characters, magic quotes may be substituted with the addslashes() function, or more appropriately an escape mechanism specific to the database vendor itself like mysql_real_escape_string() for MySQL. Functions that will be removed in PHP 6 have been deprecated in PHP 5.3 and will produce a warning if used.</p>
<p>Many high-profile open-source projects ceased to support PHP 4 in new code as of February 5, 2008, because of the GoPHP5 initiative, provided by a consortium of PHP developers promoting the transition from PHP 4 to PHP 5.</p>
<p>PHP currently does not have native support for Unicode or multibyte strings; Unicode support will be included in PHP 6 and will allow strings as well as class, method and function names to contain non-ASCII characters.</p>
<p>PHP interpreters are available on both 32-bit and 64-bit operating systems, but on Microsoft Windows the only official distribution is a 32-bit implementation, requiring Windows 32-bit compatibility mode while using Internet Information Services (IIS) on a 64-bit Windows platform. As of PHP 5.3.0, experimental 64-bit versions are available for MS Windows.</p>
<p>source: Wikipedia</p>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/what-is-php/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; AJAX Poll</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-poll/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-poll/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:57:42 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - AJAX Poll]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=376</guid>
		<description><![CDATA[AJAX Poll
This example will demonstrate a poll where a web page can   get results without reloading.

Do you like [...]]]></description>
			<content:encoded><![CDATA[<h2>AJAX Poll</h2>
<p>This example will demonstrate a poll where a web page can   get results without reloading.</p>
<div id="poll">
<h3>Do you like PHP and AJAX so far?</h3>
<form>
    Yes:</p>
<input name="vote" value="0" onclick="getVote(this.value)" type="radio" />
    <br />
    No:</p>
<input name="vote" value="1" onclick="getVote(this.value)" type="radio" />
  </form>
</div>
<hr />
<h2>Example Explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, an HTML   form, and a div element:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot; src=&quot;poll.js&quot;&gt;&lt;/script&gt;<br />
        &lt;/head&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;div id=&quot;poll&quot;&gt;<br />
        &lt;h3&gt;Do you like PHP and AJAX so far?&lt;/h3&gt;<br />
        &lt;form&gt;<br />
        Yes:<br />
        &lt;input type=&quot;radio&quot; name=&quot;vote&quot;  value=&quot;0&quot; onclick=&quot;getVote(this.value)&quot; /&gt;<br />
        &lt;br /&gt;No:<br />
        &lt;input type=&quot;radio&quot; name=&quot;vote&quot;  value=&quot;1&quot; onclick=&quot;getVote(this.value)&quot; /&gt;<br />
        &lt;/form&gt;<br />
        &lt;/div&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
<p>The HTML form works like this:</p>
<ol>
<li>An event is triggered when the user selects the &quot;yes&quot; or &quot;no&quot;   option</li>
<li>When the event is triggered, the function getVote() is executed</li>
<li>The data   	returned from the getVote() function will replace the form, in the   &lt;div&gt; tag</li>
</ol>
<hr />
<h2>Example Explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code stored in the file &quot;poll.js&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp;</p>
<p>        function getVote(int)<br />
        {<br />
        xmlhttp=GetXmlHttpObject();<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Browser does not support HTTP Request&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;poll_vote.php&quot;;<br />
        url=url+&quot;?vote=&quot;+int;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        document.getElementById(&quot;poll&quot;).innerHTML=xmlhttp.responseText;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        var objXMLHttp=null;<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        objXMLHttp=new XMLHttpRequest();<br />
        }<br />
        else if (window.ActiveXObject)<br />
        {<br />
        objXMLHttp=new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return objXMLHttp;<br />
        } </td>
</tr>
</tbody>
</table>
<p>The stateChanged() and GetXmlHttpObject functions are the same as in   the PHP AJAX   Suggest chapter.</p>
<p><strong>The getVote() Function</strong></p>
<p>This function executes when &quot;yes&quot; or &quot;no&quot; is selected in the HTML   form.</p>
<ol>
<li>Calls the GetXmlHttpObject() function to create an XMLHTTP object</li>
<li>Defines the URL (filename) to send to the server</li>
<li>Adds a parameter (vote) to the URL with the content of the input   field </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Each time the readyState property changes, the stateChanged()   function   	will be executed</li>
<li>Opens the XMLHTTP object with the given url.</li>
<li>Sends an HTTP request to the server</li>
</ol>
<hr />
<h2>The PHP Page</h2>
<p>The server page called by the JavaScript code is a simple PHP file   called &quot;poll_vote.php&quot;.</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $vote = $_REQUEST['vote'];</p>
<p>        //get content of textfile<br />
        $filename = &quot;poll_result.txt&quot;;<br />
        $content = file($filename);</p>
<p>        //put content in array<br />
        $array = explode(&quot;||&quot;, $content[0]);<br />
        $yes = $array[0];<br />
        $no = $array[1];</p>
<p>        if ($vote == 0)<br />
        {<br />
        $yes = $yes + 1;<br />
        }<br />
        if ($vote == 1)<br />
        {<br />
        $no = $no + 1;<br />
        }</p>
<p>        //insert votes to txt file<br />
        $insertvote = $yes.&quot;||&quot;.$no;<br />
        $fp = fopen($filename,&quot;w&quot;);<br />
        fputs($fp,$insertvote);<br />
        fclose($fp);<br />
        ?&gt;</p>
<p>        &lt;h2&gt;Result:&lt;/h2&gt;<br />
        &lt;table&gt;<br />
        &lt;tr&gt;<br />
        &lt;td&gt;Yes:&lt;/td&gt;<br />
        &lt;td&gt;<br />
        &lt;img src=&quot;poll.gif&quot;<br />
        width=&#8217;&lt;?php echo(100*round($yes/($no+$yes),2)); ?&gt;&#8217;<br />
        height=&#8217;20&#8242;&gt;<br />
        &lt;?php echo(100*round($yes/($no+$yes),2)); ?&gt;%<br />
        &lt;/td&gt;<br />
        &lt;/tr&gt;<br />
        &lt;tr&gt;<br />
        &lt;td&gt;No:&lt;/td&gt;<br />
        &lt;td&gt;<br />
        &lt;img src=&quot;poll.gif&quot;<br />
        width=&#8217;&lt;?php echo(100*round($no/($no+$yes),2)); ?&gt;&#8217;<br />
        height=&#8217;20&#8242;&gt;<br />
        &lt;?php echo(100*round($no/($no+$yes),2)); ?&gt;%<br />
        &lt;/td&gt;<br />
        &lt;/tr&gt;<br />
        &lt;/table&gt; </td>
</tr>
</tbody>
</table>
<p>The selected value is sent from the JavaScript and the following   happens:</p>
<ol>
<li>Get the content of the &quot;poll_result.txt&quot; file</li>
<li>Put the content of the file in variables and add one to the   selected   	variable</li>
<li>Write the result to the &quot;poll_result.txt&quot; file</li>
<li>Output a graphical representation of the poll result</li>
</ol>
<hr />
<h2>The Text File</h2>
<p>The text file (poll_result.txt) is where we store the data from the   poll. </p>
<p>It is stored like this:</p>
<table id="table1" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> 0||0 </td>
</tr>
</tbody>
</table>
<p>The first number represents the &quot;Yes&quot; votes, the second number   represents the   &quot;No&quot; votes.</p>
<p><strong>Note:</strong> Remember to allow your web server to edit the text file.   Do <strong> NOT</strong> give everyone access, just the web server (PHP).</p>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-poll/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; AJAX RSS Reader</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-rss-reader/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-rss-reader/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:56:07 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - AJAX RSS Reader]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=374</guid>
		<description><![CDATA[An RSS Reader is used to read RSS Feeds.

AJAX RSS Reader
In this example we will demonstrate an RSS reader, where [...]]]></description>
			<content:encoded><![CDATA[<p>An RSS Reader is used to read RSS Feeds.</p>
<hr />
<h2>AJAX RSS Reader</h2>
<p>In this example we will demonstrate an RSS reader, where the content   from the RSS is loaded into a webpage without refreshing.</p>
<form>
  Select an RSS-feed:</p>
<select name="select" onchange="showRSS(this.value)">
    <option value="Google">Google News</option><br />
    <option value="MSNBC">MSNBC News</option><br />
  </select>
</form>
<p></p>
<div id="rssOutput"><strong>RSS-feed will be listed here&#8230;</strong></div>
<hr />
<h2>Example Explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, an HTML   form, and a div element:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot; src=&quot;getrss.js&quot;&gt;&lt;/script&gt;<br />
        &lt;/head&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;form&gt;<br />
        Select an RSS-feed:<br />
        &lt;select onchange=&quot;showRSS(this.value)&quot;&gt;<br />
        &lt;option value=&quot;Google&quot;&gt;Google News&lt;/option&gt;<br />
        &lt;option value=&quot;MSNBC&quot;&gt;MSNBC News&lt;/option&gt;<br />
        &lt;/select&gt;<br />
        &lt;/form&gt;</p>
<p>        &lt;p&gt;&lt;div id=&quot;rssOutput&quot;&gt;<br />
        &lt;b&gt;RSS-feed will be listed here&#8230;&lt;/b&gt;&lt;/div&gt;&lt;/p&gt;<br />
        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
<p>The HTML form works like this:</p>
<ol>
<li>An event is triggered when a user selects an option in the   	drop-down box</li>
<li>When the event is triggered, the function showRSS() is executed</li>
<li>The &lt;div id=&quot;rssOutput&quot;&gt; is a placeholder for   	the data returned from the showRSS() function</li>
</ol>
<hr />
<h2>Example Explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code stored in the file &quot;getrss.js&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp;</p>
<p>        function showRSS(str)<br />
        {<br />
        xmlhttp=GetXmlHttpObject();<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Your browser does not support XML HTTP Request&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;getrss.php&quot;;<br />
        url=url+&quot;?q=&quot;+str;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        document.getElementById(&quot;rssOutput&quot;).innerHTML=xmlhttp.responseText;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return null;<br />
        } </td>
</tr>
</tbody>
</table>
<p>The stateChanged() and GetXmlHttpObject functions are the same as in   the PHP AJAX   Suggest chapter.</p>
<p><strong>The showRSS() Function</strong></p>
<p>Every time an option is selected in the input field, this function   executes   the following:</p>
<ol>
<li>Calls the GetXmlHttpObject() function to create an XMLHTTP object</li>
<li>Defines the URL (filename) to send to the server</li>
<li>Adds a parameter (q) to the URL with the selected option from the   drop-down   	list </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Each time the readyState property changes, the stateChanged()   function   	will be executed</li>
<li>Opens the XMLHTTP object with the given URL</li>
<li>Sends an HTTP request to the server</li>
</ol>
<hr />
<h2>Example Explained &#8211; The PHP page</h2>
<p>The PHP page called by the JavaScript code is called &quot;getrss.php&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        //get the q parameter from URL<br />
        $q=$_GET[&quot;q&quot;];</p>
<p>        //find out which feed was selected<br />
        if($q==&quot;Google&quot;)<br />
        {<br />
        $xml=(&quot;http://news.google.com/news?ned=us&amp;topic=h&amp;output=rss&quot;);<br />
        }<br />
        elseif($q==&quot;MSNBC&quot;)<br />
        {<br />
        $xml=(&quot;http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml&quot;);<br />
        }</p>
<p>        $xmlDoc = new DOMDocument();<br />
        $xmlDoc-&gt;load($xml);</p>
<p>        //get elements from &quot;&lt;channel&gt;&quot;<br />
        $channel=$xmlDoc-&gt;getElementsByTagName(&#8216;channel&#8217;)-&gt;item(0);<br />
        $channel_title = $channel-&gt;getElementsByTagName(&#8216;title&#8217;)<br />
        -&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;<br />
        $channel_link = $channel-&gt;getElementsByTagName(&#8216;link&#8217;)<br />
        -&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;<br />
        $channel_desc = $channel-&gt;getElementsByTagName(&#8216;description&#8217;)<br />
        -&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;</p>
<p>        //output elements from &quot;&lt;channel&gt;&quot;<br />
        echo(&quot;&lt;p&gt;&lt;a href=&#8217;&quot; . $channel_link<br />
        . &quot;&#8217;&gt;&quot; . $channel_title . &quot;&lt;/a&gt;&quot;);<br />
        echo(&quot;&lt;br /&gt;&quot;);<br />
        echo($channel_desc . &quot;&lt;/p&gt;&quot;);</p>
<p>        //get and output &quot;&lt;item&gt;&quot; elements<br />
        $x=$xmlDoc-&gt;getElementsByTagName(&#8216;item&#8217;);<br />
        for ($i=0; $i&lt;=2; $i++)<br />
        {<br />
        $item_title=$x-&gt;item($i)-&gt;getElementsByTagName(&#8216;title&#8217;)<br />
        -&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;<br />
        $item_link=$x-&gt;item($i)-&gt;getElementsByTagName(&#8216;link&#8217;)<br />
        -&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;<br />
        $item_desc=$x-&gt;item($i)-&gt;getElementsByTagName(&#8216;description&#8217;)<br />
        -&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue;</p>
<p>        echo (&quot;&lt;p&gt;&lt;a href=&#8217;&quot; . $item_link<br />
        . &quot;&#8217;&gt;&quot; . $item_title . &quot;&lt;/a&gt;&quot;);<br />
        echo (&quot;&lt;br /&gt;&quot;);<br />
        echo ($item_desc . &quot;&lt;/p&gt;&quot;);<br />
        }<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>When an option is sent from the JavaScript, the following   happens:</p>
<ol>
<li>PHP finds out which RSS feed was selected</li>
<li>An XML DOM object is created for the selected RSS feed</li>
<li>The elements from the RSS channel are found and outputted</li>
<li>Loops through the first three elements and output result</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-rss-reader/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; AJAX Live Search</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-live-search/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-live-search/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:54:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - AJAX Live Search]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=372</guid>
		<description><![CDATA[AJAX can be used for a more user-friendly and   interactive   search.

AJAX Live Search
In this example we [...]]]></description>
			<content:encoded><![CDATA[<p>AJAX can be used for a more user-friendly and   interactive   search.</p>
<hr />
<h2>AJAX Live Search</h2>
<p>In this example we will demonstrate a live search, where you   get search results while you type.</p>
<p>Live search has many benefits compared to traditional searching:</p>
<ul>
<li>Results are shown as you type</li>
<li>Results narrow as you continue typing</li>
<li>If results become too narrow, remove characters to see a broader   result</li>
</ul>
<p>Search for a W3Schools page in the input field below:</p>
<form>
<input id="txt1" size="30" onkeyup="showResult(this.value)" type="text" />
<div id="livesearch"></div>
</form>
<p>In the example above, the results are found in an XML document (links.xml).   To make this   example small and simple, only eight results are available.</p>
<hr />
<h2>Example Explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, some style   definitions, an HTML form, and a div element:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot; src=&quot;livesearch.js&quot;&gt;&lt;/script&gt;<br />
        &lt;style type=&quot;text/css&quot;&gt;<br />
        #livesearch<br />
        {<br />
        margin:0px;<br />
        width:194px;<br />
        }<br />
        #txt1<br />
        {<br />
        margin:0px;<br />
        }<br />
        &lt;/style&gt;<br />
        &lt;/head&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;form&gt;<br />
        &lt;input type=&quot;text&quot; id=&quot;txt1&quot; size=&quot;30&quot;  onkeyup=&quot;showResult(this.value)&quot; /&gt;<br />
        &lt;div id=&quot;livesearch&quot;&gt;&lt;/div&gt;<br />
        &lt;/form&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
<p>The HTML form works like this:</p>
<ol>
<li>An event is triggered when the user presses, and releases a key in   the   	input field</li>
<li>When the event is triggered, the function showResult() is executed</li>
<li>The &lt;div id=&quot;livesearch&quot;&gt; is a placeholder for   	the data returned from the showResult() function</li>
</ol>
<hr />
<h2>Example Explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code stored in the file &quot;livesearch.js&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp;</p>
<p>        function showResult(str)<br />
        {<br />
        if (str.length==0)<br />
        {<br />
        document.getElementById(&quot;livesearch&quot;).innerHTML=&quot;&quot;;<br />
        document.getElementById(&quot;livesearch&quot;).style.border=&quot;0px&quot;;<br />
        return;<br />
        }<br />
        xmlhttp=GetXmlHttpObject()<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Your browser does not support XML HTTP Request&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;livesearch.php&quot;;<br />
        url=url+&quot;?q=&quot;+str;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged ;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        document.getElementById(&quot;livesearch&quot;).innerHTML=xmlhttp.responseText;<br />
        document.getElementById(&quot;livesearch&quot;).style.border=&quot;1px solid   #A5ACB2&quot;;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return null;<br />
        }</td>
</tr>
</tbody>
</table>
<p>The GetXmlHttpObject() function is the same as in the PHP AJAX   Suggest chapter.</p>
<p><strong>The showResult() Function</strong></p>
<p>This function executes every time a character is entered in the input   field. If there is no input in the text field (str.length == 0), the   function sets   the return field to empty and removes the border around it. However, if   there is any input in the text field, the function   executes the following:</p>
<ol>
<li>Calls the GetXmlHttpObject() function to create an XMLHTTP object</li>
<li>Defines the URL (filename) to send to the server</li>
<li>Adds a parameter (q) to the URL with the content of the input field </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Each time the readyState property changes, the stateChanged()   function   	will be executed</li>
<li>Opens the XMLHTTP object with the given URL</li>
<li>Sends an HTTP request to the server</li>
</ol>
<p><strong>The stateChanged() Function</strong></p>
<p>This function executes every time the state of the XMLHTTP   object changes. When the state changes to 4 (&quot;complete&quot;), the content of   the txtHint   placeholder is filled with the response text, and a border is set around   the   field.</p>
<hr />
<h2>Example Explained &#8211; The PHP page</h2>
<p>The PHP page called by the JavaScript code is called   &quot;livesearch.php&quot;.</p>
<p>The code searches an XML file for titles matching the search string   and returns the result as HTML:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $xmlDoc = new DOMDocument();<br />
        $xmlDoc-&gt;load(&quot;links.xml&quot;);</p>
<p>        $x=$xmlDoc-&gt;getElementsByTagName(&#8216;link&#8217;);</p>
<p>        //get the q parameter from URL<br />
        $q=$_GET[&quot;q&quot;];</p>
<p>        //lookup all links from the xml file if length of q&gt;0<br />
        if (strlen($q) &gt; 0)<br />
        {<br />
        $hint=&quot;&quot;;<br />
        for($i=0; $i&lt;($x-&gt;length); $i++)<br />
        {<br />
        $y=$x-&gt;item($i)-&gt;getElementsByTagName(&#8216;title&#8217;);<br />
        $z=$x-&gt;item($i)-&gt;getElementsByTagName(&#8216;url&#8217;);<br />
        if ($y-&gt;item(0)-&gt;nodeType==1)<br />
        {<br />
        //find a link matching the search text<br />
        if   (stristr($y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue,$q))<br />
        {<br />
        if ($hint==&quot;&quot;)<br />
        {<br />
        $hint=&quot;&lt;a href=&#8217;&quot; . <br />
        $z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . <br />
        &quot;&#8217; target=&#8217;_blank&#8217;&gt;&quot; . <br />
        $y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue .   &quot;&lt;/a&gt;&quot;;<br />
        }<br />
        else<br />
        {<br />
        $hint=$hint . &quot;&lt;br /&gt;&lt;a href=&#8217;&quot; . <br />
        $z-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue . <br />
        &quot;&#8217; target=&#8217;_blank&#8217;&gt;&quot; . <br />
        $y-&gt;item(0)-&gt;childNodes-&gt;item(0)-&gt;nodeValue .   &quot;&lt;/a&gt;&quot;;<br />
        }<br />
        }<br />
        }<br />
        }<br />
        }</p>
<p>        // Set output to &quot;no suggestion&quot; if no hint were found<br />
        // or to the correct values<br />
        if ($hint == &quot;&quot;)<br />
        {<br />
        $response=&quot;no suggestion&quot;;<br />
        }<br />
        else<br />
        {<br />
        $response=$hint;<br />
        }</p>
<p>        //output the response<br />
        echo $response;<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>If there is any text sent from the JavaScript (strlen($q) &gt; 0),   the following   happens:</p>
<ol>
<li>PHP creates an XML DOM object of the &quot;links.xml&quot; file</li>
<li>Loops through all  &lt;title&gt; elements to find titles that match   the   	text sent from the JavaScript</li>
<li>Sets the correct link and title in the   	&quot;$response&quot; variable. If more than one match is found, all matches are   added   	to the variable</li>
<li>If no matches are found, the $response variable is set to &quot;no   suggestion&quot;</li>
<li>Output the $respone variable to the &quot;livesearch&quot; placeholder</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-live-search/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; responseXML</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-responsexml/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-responsexml/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:53:04 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - responseXML]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/php/php-and-ajax/php-example-responsexml/</guid>
		<description><![CDATA[responseText returns the HTTP response as a string.
responseXML returns the response as XML.

AJAX ResponseXML example
The ResponseXML property returns an XML [...]]]></description>
			<content:encoded><![CDATA[<p>responseText returns the HTTP response as a string.</p>
<p>responseXML returns the response as XML.</p>
<hr />
<h2>AJAX ResponseXML example</h2>
<p>The ResponseXML property returns an XML document object, which can be   examined and parsed using   the DOM.</p>
<p>The following example will demonstrate how a web page can fetch   information from a database with AJAX technology. The selected data from   the   database will this time be converted to an XML document, and then we   will use the DOM to extract the values to be displayed.</p>
<p>This example might look equal to the &quot;PHP AJAX and MySQL&quot; example in   the   previous   chapter. However, there is a big difference: this time we get the data   from   the PHP page as XML, with the responseXML function.</p>
<p>Receiving the response as an XML document allows us to update this   page several places, instead   of just receiving an HTML output, and displaying it.</p>
<p>In this example we will update several &lt;span&gt; elements with the   information   we receive from the database.</p>
<form>
  Select a User:</p>
<select name="users" onchange="showUser(this.value)">
    <option value="1">Peter Griffin</option><br />
    <option value="2">Lois Griffin</option><br />
    <option value="3">Joseph Swanson</option><br />
    <option value="4">Glenn Quagmire</option><br />
  </select>
</form>
<h2><span id="firstname"> </span> <span id="lastname"> </span></h2>
<p><span id="job"> </span></p>
<div> <span id="age_text"> </span> <span id="age"> </span> <span id="hometown_text"> </span> <span id="hometown"> </span></div>
<hr />
<h2>Example explained &#8211; The MySQL Database</h2>
<p>The database table we use in this example looks like this:</p>
<table id="table1" border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">id</th>
<th align="left">FirstName</th>
<th align="left">LastName</th>
<th align="left">Age</th>
<th align="left">Hometown</th>
<th align="left">Job</th>
</tr>
<tr>
<td>1</td>
<td>Peter</td>
<td>Griffin</td>
<td>41</td>
<td>Quahog</td>
<td>Brewery</td>
</tr>
<tr>
<td>2</td>
<td>Lois</td>
<td>Griffin</td>
<td>40</td>
<td>Newport</td>
<td>Piano Teacher</td>
</tr>
<tr>
<td>3</td>
<td>Joseph</td>
<td>Swanson</td>
<td>39</td>
<td>Quahog</td>
<td>Police Officer</td>
</tr>
<tr>
<td>4</td>
<td>Glenn</td>
<td>Quagmire</td>
<td>41</td>
<td>Quahog</td>
<td>Pilot</td>
</tr>
</tbody>
</table>
<hr />
<h2>Example explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, an HTML   form, and   several &lt;span&gt; elements:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot;   src=&quot;responsexml.js&quot;&gt;&lt;/script&gt;<br />
        &lt;/head&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;form&gt;<br />
        Select a User:<br />
        &lt;select name=&quot;users&quot; onchange=&quot;showUser(this.value)&quot;&gt;<br />
        &lt;option value=&quot;1&quot;&gt;Peter Griffin&lt;/option&gt;<br />
        &lt;option value=&quot;2&quot;&gt;Lois Griffin&lt;/option&gt;<br />
        &lt;option value=&quot;3&quot;&gt;Glenn Quagmire&lt;/option&gt;<br />
        &lt;option value=&quot;4&quot;&gt;Joseph Swanson&lt;/option&gt;<br />
        &lt;/select&gt;<br />
        &lt;/form&gt;</p>
<p>        &lt;h2&gt;&lt;span id=&quot;firstname&quot;&gt;&lt;/span&gt;&amp;nbsp;&lt;span   id=&quot;lastname&quot;&gt;&lt;/span&gt;&lt;/h2&gt;<br />
        &lt;span id=&quot;job&quot;&gt;&lt;/span&gt;<br />
        &lt;div style=&quot;text-align: right&quot;&gt;<br />
        &lt;span id=&quot;age_text&quot;&gt;&lt;/span&gt;<br />
        &lt;span id=&quot;age&quot;&gt;&lt;/span&gt;<br />
        &lt;span id=&quot;hometown_text&quot;&gt;&lt;/span&gt;<br />
        &lt;span id=&quot;hometown&quot;&gt;&lt;/span&gt;<br />
        &lt;/div&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
<ul>
<li>The HTML form contains a drop-down box called   &quot;users&quot;, with id and names from the database table, as options</li>
<li>The &lt;span&gt; elements are placeholders for the values we will   receive</li>
<li>When a user is selected, a function called &quot;showUser()&quot; is executed   (triggered by the &quot;onchange&quot; event)</li>
</ul>
<p>In other   words: Each time a user changes the value in the drop-down box, the   function showUser() is called, and outputs the result in the   &lt;span&gt; elements.</p>
<hr />
<h2>Example explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code stored in the file &quot;responsexml.js&quot;:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp;</p>
<p>        function showUser(str)<br />
        {<br />
        xmlhttp=GetXmlHttpObject();<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Browser does not support HTTP Request&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;responsexml.php&quot;;<br />
        url=url+&quot;?q=&quot;+str;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        xmlDoc=xmlhttp.responseXML;<br />
        document.getElementById(&quot;firstname&quot;).innerHTML=<br />
        xmlDoc.getElementsByTagName(&quot;firstname&quot;)[0].childNodes[0].nodeValue;<br />
        document.getElementById(&quot;lastname&quot;).innerHTML=<br />
        xmlDoc.getElementsByTagName(&quot;lastname&quot;)[0].childNodes[0].nodeValue;<br />
        document.getElementById(&quot;job&quot;).innerHTML=<br />
        xmlDoc.getElementsByTagName(&quot;job&quot;)[0].childNodes[0].nodeValue;<br />
        document.getElementById(&quot;age_text&quot;).innerHTML=&quot;Age: &quot;;<br />
        document.getElementById(&quot;age&quot;).innerHTML=<br />
        xmlDoc.getElementsByTagName(&quot;age&quot;)[0].childNodes[0].nodeValue;<br />
        document.getElementById(&quot;hometown_text&quot;).innerHTML=&quot;&lt;br/&gt;From:   &quot;;<br />
        document.getElementById(&quot;hometown&quot;).innerHTML=<br />
        xmlDoc.getElementsByTagName(&quot;hometown&quot;)[0].childNodes[0].nodeValue;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return null;<br />
        }</td>
</tr>
</tbody>
</table>
<p>The showUser() and GetXmlHttpObject functions are the same as in the PHP AJAX   and MySQL chapter, you can go to   there for an explanation of those.</p>
<p><strong>The stateChanged() Function</strong></p>
<p>When an option in the drop-down box is selected, the function   executes the following:</p>
<ol>
<li>Sets xmlDoc variable as an XML document, using the responseXML   function</li>
<li>Retrieves data from the XML document, and place it in the correct   &lt;span&gt;   	element</li>
</ol>
<hr />
<h2>Example explained &#8211; The PHP Page</h2>
<p>The PHP page called by the JavaScript, is called   &quot;responsexml.php&quot;.</p>
<p>The PHP script runs an SQL query against a MySQL database, and   returns the result   an XML document:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $q=$_GET[&quot;q&quot;];</p>
<p>        $con = mysql_connect(&#8216;localhost&#8217;, &#8216;peter&#8217;, &#8216;abc123&#8242;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;ajax_demo&quot;, $con);</p>
<p>        $sql=&quot;SELECT * FROM user WHERE id = &quot;.$q.&quot;&quot;;</p>
<p>        $result = mysql_query($sql);<br />
        header(&#8216;Content-type: text/xml&#8217;);<br />
        echo &#8216;&lt;?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?&gt;<br />
        &lt;person&gt;&#8217;;<br />
        while($row = mysql_fetch_array($result))<br />
        {<br />
        echo &quot;&lt;firstname&gt;&quot; . $row['FirstName'] . &quot;&lt;/firstname&gt;&quot;;<br />
        echo &quot;&lt;lastname&gt;&quot; . $row['LastName'] . &quot;&lt;/lastname&gt;&quot;;<br />
        echo &quot;&lt;age&gt;&quot; . $row['Age'] . &quot;&lt;/age&gt;&quot;;<br />
        echo &quot;&lt;hometown&gt;&quot; . $row['Hometown'] . &quot;&lt;/hometown&gt;&quot;;<br />
        echo &quot;&lt;job&gt;&quot; . $row['Job'] . &quot;&lt;/job&gt;&quot;;<br />
        }<br />
        echo &quot;&lt;/person&gt;&quot;;</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>When the query is sent from the JavaScript to the PHP page, the   following   happens:</p>
<ol>
<li>Set the $q variable to the data sent in the q parameter</li>
<li>Open a connection to a MySQL server</li>
<li>The &quot;user&quot; with the specified id is found</li>
<li>The data is outputted as an XML document</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-responsexml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; AJAX and MySQL</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-mysql/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-mysql/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:51:35 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - AJAX and MySQL]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=369</guid>
		<description><![CDATA[AJAX can be used for interactive communication with a   database.

AJAX database example
The following example will demonstrate how a [...]]]></description>
			<content:encoded><![CDATA[<p>AJAX can be used for interactive communication with a   database.</p>
<hr />
<h2>AJAX database example</h2>
<p>The following example will demonstrate how a web page can fetch   information from a database with AJAX technology.</p>
<form>
  Select a person:</p>
<select name="users" onchange="showUser(this.value)">
    <option value="1">Peter Griffin</option><br />
    <option value="2">Lois Griffin</option><br />
    <option value="3">Joseph Swanson</option><br />
    <option value="4">Glenn Quagmire</option><br />
  </select>
</form>
<p></p>
<div id="txtHint"><strong>Person info will be listed here.</strong></div>
<hr />
<h2>Example explained &#8211; The MySQL Database</h2>
<p>The database table we use in this example looks like this:</p>
<table border="1" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<th align="left">id</th>
<th align="left">FirstName</th>
<th align="left">LastName</th>
<th align="left">Age</th>
<th align="left">Hometown</th>
<th align="left">Job</th>
</tr>
<tr>
<td>1</td>
<td>Peter</td>
<td>Griffin</td>
<td>41</td>
<td>Quahog</td>
<td>Brewery</td>
</tr>
<tr>
<td>2</td>
<td>Lois</td>
<td>Griffin</td>
<td>40</td>
<td>Newport</td>
<td>Piano Teacher</td>
</tr>
<tr>
<td>3</td>
<td>Joseph</td>
<td>Swanson</td>
<td>39</td>
<td>Quahog</td>
<td>Police Officer</td>
</tr>
<tr>
<td>4</td>
<td>Glenn</td>
<td>Quagmire</td>
<td>41</td>
<td>Quahog</td>
<td>Pilot</td>
</tr>
</tbody>
</table>
<hr />
<h2>Example explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, an HTML   form, and a   div element:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot; src=&quot;selectuser.js&quot;&gt;&lt;/script&gt;<br />
        &lt;/head&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;form&gt;<br />
        Select a User:<br />
        &lt;select name=&quot;users&quot; onchange=&quot;showUser(this.value)&quot;&gt;<br />
        &lt;option value=&quot;1&quot;&gt;Peter Griffin&lt;/option&gt;<br />
        &lt;option value=&quot;2&quot;&gt;Lois Griffin&lt;/option&gt;<br />
        &lt;option value=&quot;3&quot;&gt;Glenn Quagmire&lt;/option&gt;<br />
        &lt;option value=&quot;4&quot;&gt;Joseph Swanson&lt;/option&gt;<br />
        &lt;/select&gt;<br />
        &lt;/form&gt;<br />
        &lt;br /&gt;<br />
        &lt;div id=&quot;txtHint&quot;&gt;&lt;b&gt;Person info will be listed   here.&lt;/b&gt;&lt;/div&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt; </td>
</tr>
</tbody>
</table>
<p> As you can see it is just a simple HTML form with a drop down box called   &quot;customers&quot;.</p>
<p>The &lt;div&gt; below the form will be   used as a placeholder for info retrieved from the web server.</p>
<p>When the user selects data, a function called &quot;showUser()&quot; is   executed. The   execution of the function is triggered by the &quot;onchange&quot; event. In other   words:   Each time the user change the value in the drop down box, the function   showUser() is called.</p>
<hr />
<h2>Example explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code stored in the file &quot;selectuser.js&quot;:</p>
<table id="table1" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp;</p>
<p>        function showUser(str)<br />
        {<br />
        xmlhttp=GetXmlHttpObject();<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Browser does not support HTTP Request&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;getuser.php&quot;;<br />
        url=url+&quot;?q=&quot;+str;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        document.getElementById(&quot;txtHint&quot;).innerHTML=xmlhttp.responseText;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return null;<br />
        }</td>
</tr>
</tbody>
</table>
<p>The stateChanged() and GetXmlHttpObject functions are the same as in   the PHP AJAX   Suggest chapter, you can go   to there for an explanation of those.</p>
<p><strong>The showUser() Function</strong></p>
<p>When a person in the drop-down box is selected, the showUser()   function   executes the following:</p>
<ol>
<li>Calls the GetXmlHttpObject() function to create an XMLHTTP object</li>
<li>Defines an URL (filename) to send to the server</li>
<li>Adds a parameter (q) to the URL with the content of the drop-down   box </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Each time the readyState property changes, the stateChanged()   function   	will be executed</li>
<li>Opens the XMLHTTP object with the given URL</li>
<li>Sends an HTTP request to the server</li>
</ol>
<hr />
<h2>Example explained &#8211; The PHP Page</h2>
<p>The PHP page called by the JavaScript, is called   &quot;getuser.php&quot;.</p>
<p>The PHP script runs an SQL query against a MySQL database, and   returns the result as HTML:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $q=$_GET[&quot;q&quot;];</p>
<p>        $con = mysql_connect(&#8216;localhost&#8217;, &#8216;peter&#8217;, &#8216;abc123&#8242;);<br />
        if (!$con)<br />
        {<br />
        die(&#8216;Could not connect: &#8216; . mysql_error());<br />
        }</p>
<p>        mysql_select_db(&quot;ajax_demo&quot;, $con);</p>
<p>        $sql=&quot;SELECT * FROM user WHERE id = &#8216;&quot;.$q.&quot;&#8217;&quot;;</p>
<p>        $result = mysql_query($sql);</p>
<p>        echo &quot;&lt;table border=&#8217;1&#8242;&gt;<br />
        &lt;tr&gt;<br />
        &lt;th&gt;Firstname&lt;/th&gt;<br />
        &lt;th&gt;Lastname&lt;/th&gt;<br />
        &lt;th&gt;Age&lt;/th&gt;<br />
        &lt;th&gt;Hometown&lt;/th&gt;<br />
        &lt;th&gt;Job&lt;/th&gt;<br />
        &lt;/tr&gt;&quot;;</p>
<p>        while($row = mysql_fetch_array($result))<br />
        {<br />
        echo &quot;&lt;tr&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['FirstName'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['LastName'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['Age'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['Hometown'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;td&gt;&quot; . $row['Job'] . &quot;&lt;/td&gt;&quot;;<br />
        echo &quot;&lt;/tr&gt;&quot;;<br />
        }<br />
        echo &quot;&lt;/table&gt;&quot;;</p>
<p>        mysql_close($con);<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>When the query is sent from the JavaScript to the PHP page, the   following   happens:</p>
<ol>
<li>PHP opens a connection to a MySQL server</li>
<li>The correct person is found</li>
<li>An HTML table is created, and filled with data, and sent back to   the &quot;txtHint&quot;   	placeholder</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; AJAX and XML</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-xml/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-xml/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:50:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - AJAX and XML]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-xml/</guid>
		<description><![CDATA[AJAX can be used for interactive communication with an   XML   file.

AJAX XML example
The following example will [...]]]></description>
			<content:encoded><![CDATA[<p>AJAX can be used for interactive communication with an   XML   file.</p>
<hr />
<h2>AJAX XML example</h2>
<p>The following example will demonstrate how a web page can fetch   information from an XML file with AJAX technology.</p>
<form>
  Select a CD:</p>
<select name="cds" onchange="showCD(this.value)">
    <option value="Bob Dylan">Bob Dylan</option><br />
    <option value="Bee Gees">Bee Gees</option><br />
    <option value="Cat Stevens">Cat Stevens</option><br />
  </select>
</form>
<p></p>
<div id="txtHint"> <strong>CD info will be listed here&#8230;</strong></div>
<hr />
<h2>Example explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, an HTML   form, and a   div element:</p>
<table id="table1" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot; src=&quot;selectcd.js&quot;&gt;&lt;/script&gt;<br />
        &lt;/head&gt;</p>
<p>        &lt;body&gt;</p>
<p>        &lt;form&gt;<br />
        Select a CD:<br />
        &lt;select name=&quot;cds&quot; onchange=&quot;showCD(this.value)&quot;&gt;<br />
        &lt;option value=&quot;Bob Dylan&quot;&gt;Bob Dylan&lt;/option&gt;<br />
        &lt;option value=&quot;Bonnie Tyler&quot;&gt;Bonnie Tyler&lt;/option&gt;<br />
        &lt;option value=&quot;Dolly Parton&quot;&gt;Dolly Parton&lt;/option&gt;<br />
        &lt;/select&gt;<br />
        &lt;/form&gt;</p>
<p>        &lt;div id=&quot;txtHint&quot;&gt;&lt;b&gt;CD info will be listed   here&#8230;&lt;/b&gt;&lt;/div&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt;</td>
</tr>
</tbody>
</table>
<p> As you can see it is just a simple HTML form  with a simple drop down   box called &quot;cds&quot;.</p>
<p>The &lt;div&gt; below the form will be   used as a placeholder for info retrieved from the web server.</p>
<p>When the user selects data, a function called &quot;showCD&quot; is executed.   The   execution of the function is triggered by the &quot;onchange&quot; event. In other   words:   Each time the user change the value in the drop down box, the function   showCD is called.</p>
<hr />
<h2>Example explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code stored in the file &quot;selectcd.js&quot;:</p>
<table id="table2" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp</p>
<p>        function showCD(str)<br />
        {<br />
        xmlhttp=GetXmlHttpObject();<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Your browser does not support AJAX!&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;getcd.php&quot;;<br />
        url=url+&quot;?q=&quot;+str;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        document.getElementById(&quot;txtHint&quot;).innerHTML=xmlhttp.responseText;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return null;<br />
        }</td>
</tr>
</tbody>
</table>
<p>The stateChanged() and GetXmlHttpObject functions are the same as in   the PHP AJAX   Suggest chapter, you can go   to there for an explanation of those.</p>
<p><strong>The showCD() Function</strong></p>
<p>When a CD in the drop-down box is selected, the showCD() function   executes the following:</p>
<ol>
<li>Calls the GetXmlHttpObject() function to create an XMLHTTP object</li>
<li>Defines an URL (filename) to send to the server</li>
<li>Adds a parameter (q) to the URL with the content of the drop-down   box </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Each time the readyState property changes, the stateChanged()   function   	will be executed</li>
<li>Opens the XMLHTTP object with the given URL</li>
<li>Sends an HTTP request to the server</li>
</ol>
<hr />
<h2>Example explained &#8211; The PHP Page</h2>
<p>The server paged called by the JavaScript, is a PHP file called   &quot;getcd.php&quot;.</p>
<p>The PHP script loads an XML document, &quot;cd_catalog.xml&quot;,   runs a query against the XML file, and returns the result as HTML:</p>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        $q=$_GET[&quot;q&quot;];</p>
<p>        $xmlDoc = new DOMDocument();<br />
        $xmlDoc-&gt;load(&quot;cd_catalog.xml&quot;);</p>
<p>        $x=$xmlDoc-&gt;getElementsByTagName(&#8216;ARTIST&#8217;);</p>
<p>        for ($i=0; $i&lt;=$x-&gt;length-1; $i++)<br />
        {<br />
        //Process only element nodes<br />
        if ($x-&gt;item($i)-&gt;nodeType==1)<br />
        {<br />
        if ($x-&gt;item($i)-&gt;childNodes-&gt;item(0)-&gt;nodeValue == $q)<br />
        {<br />
        $y=($x-&gt;item($i)-&gt;parentNode);<br />
        }<br />
        }<br />
        }</p>
<p>        $cd=($y-&gt;childNodes);</p>
<p>        for ($i=0;$i&lt;$cd-&gt;length;$i++)<br />
        { <br />
        //Process only element nodes<br />
        if ($cd-&gt;item($i)-&gt;nodeType==1)<br />
        {<br />
        echo(&quot;&lt;b&gt;&quot; . $cd-&gt;item($i)-&gt;nodeName . &quot;:&lt;/b&gt; &quot;);<br />
        echo($cd-&gt;item($i)-&gt;childNodes-&gt;item(0)-&gt;nodeValue);<br />
        echo(&quot;&lt;br /&gt;&quot;);<br />
        }<br />
        }<br />
        ?&gt; </td>
</tr>
</tbody>
</table>
<p>When the CD query is sent from the JavaScript to the PHP page, the   following   happens:</p>
<ol>
<li>PHP creates an XML DOM object</li>
<li>Find all &lt;artist&gt; elements that matches the   	name sent from the JavaScript</li>
<li>Output the album information (send to the &quot;txtHint&quot; placeholder)</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-and-xml/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP Example &#8211; AJAX Suggest</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-suggest/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-suggest/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:47:03 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[PHP Example - AJAX Suggest]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=366</guid>
		<description><![CDATA[AJAX can be used to create more interactive   applications.

AJAX Suggest example
The following AJAX example will demonstrate how a [...]]]></description>
			<content:encoded><![CDATA[<p>AJAX can be used to create more interactive   applications.</p>
<hr />
<h2>AJAX Suggest example</h2>
<p>The following AJAX example will demonstrate how a web page can   communicate with a web server while a user enters data into an HTML   form. </p>
<p>Type a name in the input field below:</p>
<form action="">
  First name:</p>
<input id="txt1" onkeyup="showHint(this.value)" type="text" />
</form>
<p>Suggestions: <span id="txtHint"> </span></p>
<hr />
<h2>Example explained &#8211; The HTML page</h2>
<p>The HTML page contains a link to an external JavaScript, a simple   HTML form,   and a span element:</p>
<table id="table6" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;html&gt;<br />
        &lt;head&gt;<br />
        &lt;script type=&quot;text/javascript&quot; src=&quot;clienthint.js&quot;&gt;&lt;/script&gt;<br />
        &lt;/head&gt;<br />
        &lt;body&gt;</p>
<p>        &lt;form&gt;<br />
        First Name:  &lt;input type=&quot;text&quot; id=&quot;txt1&quot;  onkeyup=&quot;showHint(this.value)&quot; /&gt;<br />
        &lt;/form&gt;<br />
        &lt;p&gt;Suggestions: &lt;span   id=&quot;txtHint&quot;&gt;&lt;/span&gt;&lt;/p&gt;</p>
<p>        &lt;/body&gt;<br />
        &lt;/html&gt;</td>
</tr>
</tbody>
</table>
<p>The HTML form above has an input field called &quot;txt1&quot;. An event   attribute for this field defines a function to be triggered by the   onkeyup event. </p>
<p>The paragraph below the form contains a span called &quot;txtHint&quot;. The   span is used as a placeholder for data retrieved from the web server.</p>
<p>When a user inputs data, the function called &quot;showHint()&quot; is   executed. The execution of the function is triggered by the &quot;onkeyup&quot;   event. In other words:   Each time a user moves the finger away from a keyboard key inside the   input field, the function showHint is called.</p>
<hr />
<h2>Example explained &#8211; The JavaScript code</h2>
<p>This is the JavaScript code, stored in the file &quot;clienthint.js&quot;:</p>
<table id="table7" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> var xmlhttp</p>
<p>        function showHint(str)<br />
        {<br />
        if (str.length==0)<br />
        {<br />
        document.getElementById(&quot;txtHint&quot;).innerHTML=&quot;&quot;;<br />
        return;<br />
        }<br />
        xmlhttp=GetXmlHttpObject();<br />
        if (xmlhttp==null)<br />
        {<br />
        alert (&quot;Your browser does not support XMLHTTP!&quot;);<br />
        return;<br />
        }<br />
        var url=&quot;gethint.php&quot;;<br />
        url=url+&quot;?q=&quot;+str;<br />
        url=url+&quot;&amp;sid=&quot;+Math.random();<br />
        xmlhttp.onreadystatechange=stateChanged;<br />
        xmlhttp.open(&quot;GET&quot;,url,true);<br />
        xmlhttp.send(null);<br />
        }</p>
<p>        function stateChanged()<br />
        {<br />
        if (xmlhttp.readyState==4)<br />
        {<br />
        document.getElementById(&quot;txtHint&quot;).innerHTML=xmlhttp.responseText;<br />
        }<br />
        }</p>
<p>        function GetXmlHttpObject()<br />
        {<br />
        if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }<br />
        return null;<br />
        }</td>
</tr>
</tbody>
</table>
<h3>The showHint() function</h3>
<p>The showHint() function above is executed every time a character is   entered in the   &quot;txt1&quot; input field.</p>
<p>If there is input in the input field (str.length &gt; 0), the   showHint() function executes the following:</p>
<ul>
<li>Calls the GetXmlHttpObject() function to create an XMLHTTP object</li>
<li>Defines the URL (filename) to send to the server</li>
<li>Adds a parameter (q) to the URL with the content of the input field </li>
<li>Adds a random number to prevent the server from using a cached file</li>
<li>Each time the readyState property changes, the stateChanged()   function   	will be executed</li>
<li>Opens the XMLHTTP object with the given URL</li>
<li>Sends an HTTP request to the server</li>
</ul>
<p>If the input field is empty, the function simply clears the content   of the txtHint placeholder.</p>
<h3>The GetXmlHttpObject() function</h3>
<p>The showHint() function above calls a function named   GetXmlHttpObject().</p>
<p>The purpose of the GetXmlHttpObject() function is to solve the   problem of creating different XMLHTTP   objects for different browsers.
</p>
<h3>The stateChanged() function</h3>
<p>The stateChanged() function executes every time the state of the   XMLHTTP object changes.</p>
<p>When the state changes to 4 (&quot;complete&quot;), the content of the txtHint   placeholder is filled with the response text.</p>
<hr />
<h2>Example explained &#8211; The PHP page</h2>
<p>The code in the &quot;gethint.php&quot; checks an array of names and returns   the   corresponding names to the client:</p>
<table id="table5" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td>&lt;?php<br />
        // Fill up array with names<br />
        $a[]=&quot;Anna&quot;;<br />
        $a[]=&quot;Brittany&quot;;<br />
        $a[]=&quot;Cinderella&quot;;<br />
        $a[]=&quot;Diana&quot;;<br />
        $a[]=&quot;Eva&quot;;<br />
        $a[]=&quot;Fiona&quot;;<br />
        $a[]=&quot;Gunda&quot;;<br />
        $a[]=&quot;Hege&quot;;<br />
        $a[]=&quot;Inga&quot;;<br />
        $a[]=&quot;Johanna&quot;;<br />
        $a[]=&quot;Kitty&quot;;<br />
        $a[]=&quot;Linda&quot;;<br />
        $a[]=&quot;Nina&quot;;<br />
        $a[]=&quot;Ophelia&quot;;<br />
        $a[]=&quot;Petunia&quot;;<br />
        $a[]=&quot;Amanda&quot;;<br />
        $a[]=&quot;Raquel&quot;;<br />
        $a[]=&quot;Cindy&quot;;<br />
        $a[]=&quot;Doris&quot;;<br />
        $a[]=&quot;Eve&quot;;<br />
        $a[]=&quot;Evita&quot;;<br />
        $a[]=&quot;Sunniva&quot;;<br />
        $a[]=&quot;Tove&quot;;<br />
        $a[]=&quot;Unni&quot;;<br />
        $a[]=&quot;Violet&quot;;<br />
        $a[]=&quot;Liza&quot;;<br />
        $a[]=&quot;Elizabeth&quot;;<br />
        $a[]=&quot;Ellen&quot;;<br />
        $a[]=&quot;Wenche&quot;;<br />
        $a[]=&quot;Vicky&quot;;</p>
<p>        //get the q parameter from URL<br />
        $q=$_GET[&quot;q&quot;];</p>
<p>        //lookup all hints from array if length of q&gt;0<br />
        if (strlen($q) &gt; 0)<br />
        {<br />
        $hint=&quot;&quot;;<br />
        for($i=0; $i&lt;count($a); $i++)<br />
        {<br />
        if (strtolower($q)==strtolower(substr($a[$i],0,strlen($q))))<br />
        {<br />
        if ($hint==&quot;&quot;)<br />
        {<br />
        $hint=$a[$i];<br />
        }<br />
        else<br />
        {<br />
        $hint=$hint.&quot; , &quot;.$a[$i];<br />
        }<br />
        }<br />
        }<br />
        }</p>
<p>        // Set output to &quot;no suggestion&quot; if no hint were found<br />
        // or to the correct values<br />
        if ($hint == &quot;&quot;)<br />
        {<br />
        $response=&quot;no suggestion&quot;;<br />
        }<br />
        else<br />
        {<br />
        $response=$hint;<br />
        }</p>
<p>        //output the response<br />
        echo $response;<br />
        ?&gt;</td>
</tr>
</tbody>
</table>
<p>If there is any text sent from the JavaScript (strlen($q) &gt; 0),   the following happens:</p>
<ol>
<li>Find a name matching the characters sent from the JavaScript</li>
<li>If no match were found, set the response string to &quot;no suggestion&quot;</li>
<li>If one or more matching names were found, set the response string   to all these names</li>
<li>The response is sent to the &quot;txtHint&quot; placeholder</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/php-example-ajax-suggest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX XMLHttpRequest</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/ajax-xmlhttprequest/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/ajax-xmlhttprequest/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:45:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[AJAX XMLHttpRequest]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/?p=364</guid>
		<description><![CDATA[The keystone of AJAX is the XMLHttpRequest object.

AJAX uses the XMLHttpRequest object
To get or send information from/to a database or [...]]]></description>
			<content:encoded><![CDATA[<p>The keystone of AJAX is the XMLHttpRequest object.</p>
<hr />
<h2>AJAX uses the XMLHttpRequest object</h2>
<p>To get or send information from/to a database or a file on the server   with   traditional JavaScript, you will have to make an   HTML form, and a user will have to click   the &quot;Submit&quot; button to send/get the information, wait for the server to   respond, then a new page will load with the   results. Because the server returns a new page each   time the user submits input, traditional web applications can   run slowly and tend to be less user-friendly.</p>
<p>With AJAX, your JavaScript communicates directly with the server,   through the   JavaScript <strong>XMLHttpRequest</strong> object.</p>
<p>With the XMLHttpRequest object, a web page can make a request to, and   get a response from a web   server &#8211; without reloading the page. The user will stay on the same   page, and he   or she will   not notice that scripts request pages, or send data to a   server in the background.</p>
<p>The XMLHttpRequest object is supported in all major browsers   (Internet Explorer,   Firefox, Chrome, Opera, and Safari).</p>
<hr />
<h2>AJAX &#8211; Browser support</h2>
<p>All new browsers use the built-in JavaScript <strong>XMLHttpRequest </strong>object   to create   an XMLHttpRequest object (IE5 and IE6 uses an ActiveXObject).</p>
<p>The JavaScript code for creating an XMLHttpRequest object:</p>
<table id="table1" border="0" cellpadding="0" cellspacing="0" width="100%">
<tbody>
<tr>
<td> if (window.XMLHttpRequest)<br />
        {<br />
        // code for IE7+, Firefox, Chrome, Opera, Safari<br />
        return new XMLHttpRequest();<br />
        }<br />
        if (window.ActiveXObject)<br />
        {<br />
        // code for IE6, IE5<br />
        return new ActiveXObject(&quot;Microsoft.XMLHTTP&quot;);<br />
        }</td>
</tr>
</tbody>
</table>
<p>The next chapter shows how to use the XMLHttpRequest object to   communicate with   a PHP server.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/ajax-xmlhttprequest/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX Introduction</title>
		<link>http://jaffnacampus.com/php/php-and-ajax/ajax-introduction/</link>
		<comments>http://jaffnacampus.com/php/php-and-ajax/ajax-introduction/#comments</comments>
		<pubDate>Thu, 18 Feb 2010 07:44:39 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP and AJAX]]></category>
		<category><![CDATA[AJAX Introduction]]></category>

		<guid isPermaLink="false">http://jaffnacampus.com/php/php-and-ajax/ajax-introduction/</guid>
		<description><![CDATA[AJAX = Asynchronous JavaScript and XML
AJAX is not a new programming language, but a new technique for   creating [...]]]></description>
			<content:encoded><![CDATA[<h2>AJAX = Asynchronous JavaScript and XML</h2>
<p>AJAX is not a new programming language, but a new technique for   creating better, faster, and more interactive web applications.</p>
<p>With AJAX, a JavaScript can communicate directly with the server,   with the <strong>XMLHttpRequest</strong> object. With this object,   a JavaScript can   trade data with a web server, without reloading the page.</p>
<p>AJAX uses asynchronous data transfer (HTTP requests) between the   browser and the web server, allowing web pages to request   small bits of information from the server instead of whole pages.</p>
<p>The AJAX technique makes Internet applications smaller, faster and   more user-friendly.</p>
<hr />
<h2>AJAX is based on Internet standards</h2>
<p>AJAX is based on the following web standards:</p>
<ul>
<li>JavaScript</li>
<li>XML</li>
<li>HTML</li>
<li>CSS</li>
</ul>
<p> <em><strong>AJAX applications are browser- and   platform-independent.</strong></em><strong></strong></p>
<hr />
<h2>AJAX is about better Internet-applications</h2>
<p>Internet-applications have many benefits over desktop applications;   they can reach a larger audience, they are easier to install and   support, and easier to develop.</p>
<p>However, Internet-applications are not always as &quot;rich&quot; and   user-friendly as traditional desktop applications. </p>
<p>With AJAX, Internet applications can be made richer and more   user-friendly.</p>
<hr />
<h2>Start using AJAX today</h2>
<p>There is nothing new to learn.</p>
<p>AJAX is based on existing standards. These standards have been used   by developers for several years.</p>
<hr />
<h2>PHP and AJAX</h2>
<p>There is no such thing as an AJAX server. AJAX runs in your browser.   AJAX uses HTTP requests   to request small pieces of information from the server, instead of whole   pages.</p>
<p>In our PHP tutorial we will demonstrate how a web page can   communicate with a   PHP web server online.</p>
]]></content:encoded>
			<wfw:commentRss>http://jaffnacampus.com/php/php-and-ajax/ajax-introduction/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

