<?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>Coding Friends &#187; HTML  &amp; CSS</title>
	<atom:link href="http://www.codingfriends.com/index.php/category/programming/html/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codingfriends.com</link>
	<description>Coding Friends, place for developers.</description>
	<lastBuildDate>Sun, 04 Dec 2011 21:11:08 +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>CKEditor &#8211; adding custom style sheet</title>
		<link>http://www.codingfriends.com/index.php/2010/12/10/ckeditor-adding-custom-style-sheet/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ckeditor-adding-custom-style-sheet</link>
		<comments>http://www.codingfriends.com/index.php/2010/12/10/ckeditor-adding-custom-style-sheet/#comments</comments>
		<pubDate>Fri, 10 Dec 2010 22:11:57 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[CKEditor]]></category>
		<category><![CDATA[CSS]]></category>
		<category><![CDATA[Style sheet]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1195</guid>
		<description><![CDATA[Within the CKEditor editor you can add in custom style sheets to assist with styling of the content within the editor and here is how you do it CKEDITOR.config.contentsCss = '/css/mycssfilename.css' ; After you have created the instance of the CKEditor you just need to add in to the configuration the contents cascading style sheet [...]]]></description>
			<content:encoded><![CDATA[<p>Within the <a href="http://ckeditor.com">CKEditor</a> editor you can add in custom style sheets to assist with styling of the content within the editor and here is how you do it</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">CKEDITOR.<span style="color: #660066;">config</span>.<span style="color: #660066;">contentsCss</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">'/css/mycssfilename.css'</span> <span style="color: #339933;">;</span></pre></div></div>

<p>After you have created the instance of the CKEditor you just need to add in to the configuration the contents cascading style sheet file.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/12/10/ckeditor-adding-custom-style-sheet/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>JQuery</title>
		<link>http://www.codingfriends.com/index.php/2010/06/28/jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jquery</link>
		<comments>http://www.codingfriends.com/index.php/2010/06/28/jquery/#comments</comments>
		<pubDate>Mon, 28 Jun 2010 12:37:43 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=1081</guid>
		<description><![CDATA[JQuery is a library API within the javascript language, the API (Application Program Interface) is classes/functions that do all of the work for you and you can access them. Both google and Microsoft host the JQuery API for you, and all you need to do is to load in the javascript from either or server [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/JQuery">JQuery</a> is a library API within the javascript language, the API (Application Program Interface) is classes/functions that do all of the work for you and you can access them.</p>
<p>Both google and Microsoft host the JQuery API for you, and all you need to do is to load in the javascript from either or server hosted setup, for google it would be this to get version 1.4.2 of the JQuery language.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.google.com/jsapi&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
google.load(&quot;jquery&quot;, &quot;1.4.2&quot;);
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
&nbsp;
-- or one line
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>or with MS you can use</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">src</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://ajax.microsoft.com/ajax/jquery/jquery-1.4.2.min.js&quot;</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span></pre></div></div>

<p>once you have the JQuery API loaded you can then access the HTML document objects by using the $ symbol and act on any part of that document object that supports the functions that you want to do, e.g. a <a href="http://www.w3schools.com/tags/tag_div.asp">div</a> tag would have different options for styling compared to a <a href="http://www.w3schools.com/tags/tag_span.asp">span</a> tag.</p>
<p>The <a href="http://api.jquery.com/">API</a> library set is defined abit more in that link, but in whole it is a very effective and usable API library that just makes life more fun <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<p>For example the code below will output a mini function that will for &#8220;each&#8221; number in the first parameter (can be anything, object, letters etc) and do the function, will in this case will square the value, but the .each command is very powerful,</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">$.each([1,2,3], function()
{
	document.write(this * this);
});</pre></div></div>

<p>the reason why it is very powerful is because if you want to select a group of items and then on each of them alter there elements, then you just need to use the JQuery  selection process, like so</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">$(&quot;div&quot;).each</pre></div></div>

<p>and this will alter all of the div&#8217;s on the web page accordlying.  Shall do some more posts on JQuery API once I have a full set of examples to attached to the post.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/06/28/jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web page inputs and insert into database &#8211; Part 2</title>
		<link>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database-part-2/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=web-page-inputs-and-insert-into-database-part-2</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database-part-2/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 21:02:51 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[datainsert]]></category>
		<category><![CDATA[sanitize]]></category>
		<category><![CDATA[web page]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=812</guid>
		<description><![CDATA[As from here, where I outlined the javascript, html part of the exercised here is the php and mysql parts of the problem. Here is the table that I created within MySQL CREATE TABLE `User` &#40; `id` INT&#40;11&#41; NOT NULL AUTO_INCREMENT, `firstName` VARCHAR&#40;50&#41; DEFAULT NULL, `lastName` VARCHAR&#40;50&#41; DEFAULT NULL, `email` VARCHAR&#40;50&#41; DEFAULT NULL, `phoneNum` VARCHAR&#40;20&#41; [...]]]></description>
			<content:encoded><![CDATA[<p>As from <a href="http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/">here</a>, where I outlined the javascript, html part of the exercised here is the php and mysql parts of the problem.</p>
<p>Here is the table that I created within <a href="http://www.mysql.com/">MySQL</a></p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`User`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`firstName`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`lastName`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`email`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">50</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`phoneNum`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`guid`</span> <span style="color: #993333; font-weight: bold;">VARCHAR</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">36</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>MyISAM <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">=</span><span style="color: #cc66cc;">9</span> <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>latin1;</pre></div></div>

<p>As from my other post about MySQL <a href="http://www.codingfriends.com/index.php/2010/03/11/mysql-triggers/">triggers</a>, so to find out more information about why please check out that page, and here is the trigger that I created for the above table.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TRIGGER</span> User_insert 
<span style="color: #993333; font-weight: bold;">BEFORE</span> <span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">ON</span> <span style="color: #993333; font-weight: bold;">USER</span> 
<span style="color: #993333; font-weight: bold;">FOR</span> EACH <span style="color: #993333; font-weight: bold;">ROW</span> 
<span style="color: #993333; font-weight: bold;">BEGIN</span> 
        <span style="color: #993333; font-weight: bold;">SET</span> <span style="color: #993333; font-weight: bold;">NEW</span><span style="color: #66cc66;">.</span>guid <span style="color: #66cc66;">=</span> uuid<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>; 
<span style="color: #993333; font-weight: bold;">END</span></pre></div></div>

<p>The reason why I created this for was because within the exercise they asked &#8220;would forward planning, i.e. adding a unique code to the user details that could be used to validate their email address&#8221;, which by using a uuid, you can use that as a unique code.</p>
<p>so the only part left is the PHP that will connect to the database and also sanitize the inputs within sql insert. So created a class that has a constructor that will open a database connection to a database</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$dbLink</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">/* connect to the database*/</span>
      <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database present&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database within the database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here I try to sanitize the insert string so that it will use the <a href="http://uk3.php.net/mysql_real_escape_string">mysql_real_escape_string</a>, this will use a php function that helps with SQL injections, also I am using the <a href="http://uk.php.net/stripslashes">stripslashes</a> and then <a href="http://uk.php.net/trim">trim</a>  the string to make sure that there is no white spaces (or any valid text left).</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">/* santize the input for a mysql database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> santizeInput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then to insert the data, just use the <a href="http://uk.php.net/mysql_query">mysql_query</a> and the sanitize function above to insert data into the database.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">      <span style="color: #666666; font-style: italic;">/* insert the data into the database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error inserting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span></pre></div></div>

<p>And here is where I get the data from the form post from the web page and then double sanitize the data and then create a database link, which in-turn use to insert the data.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">    <span style="color: #666666; font-style: italic;">// could use foreach loop $_POST inputs, but I personally prefer to pick them up.</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'secondname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* could do additional checks on input incase it is sent via backend POST and not via the webpage,  could do with regular expression as well ? */</span>
    <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> databaseAccess<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* can santize the inputs to make sure that there is some data to &quot;play&quot; with */</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>	
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into User (firstname, lastname, email, phoneNum) values (<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$firstname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$secondname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$email</span><span style="color: #000099; font-weight: bold;">\&quot;</span>, <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$phonenumber</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Data inserted&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span></pre></div></div>

<p>I did write within the exercise that since someone may try and post the data to the server within using the webpage (naughty people that they are!!) you could also check the inputs again for there data validity.</p>
<p>Here is the full code for the web page in total.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000000; font-weight: bold;">class</span> databaseAccess
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000088;">$dbLink</span><span style="color: #339933;">;</span>
      <span style="color: #666666; font-style: italic;">/* connect to the database*/</span>
      <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$database</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$host</span><span style="color: #339933;">,</span> <span style="color: #000088;">$user</span><span style="color: #339933;">,</span> <span style="color: #000088;">$password</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database present&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$database</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;no database within the database&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* disconnect */</span>
      <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> <span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* santize the input for a mysql database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> santizeInput<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">return</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">mysql_real_escape_string</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
&nbsp;
      <span style="color: #666666; font-style: italic;">/* insert the data into the database */</span>
      <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> insertData<span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
	<span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sqlData</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">dbLink</span><span style="color: #009900;">&#41;</span> or <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error inserting data&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">function</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$theStr</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span> 
      <span style="color: #b1b100;">else</span> 
	<span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// could use foreach loop $_POST inputs, but I personally prefer to pick them up.</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'firstname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'secondname'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'email'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_POST</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'phonenumber'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">/* could do additional checks on input incase it is sent via backend POST and not via the webpage, not sure if SOAP are looking for that as well ? 
      could do with regular expression as well ? */</span>
    <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> databaseAccess<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;User&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;PW&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Test&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">/* can santize the inputs to make sure that there is some data to &quot;play&quot; with */</span>
    <span style="color: #000088;">$firstname</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$secondname</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$email</span> <span style="color: #339933;">=</span><span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$email</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonenumber</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">santizeInput</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonenumber</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$firstname</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> checkLength<span style="color: #009900;">&#40;</span><span style="color: #000088;">$secondname</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>	
	<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;insert into User (firstname, lastname, email, phoneNum) values (<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$firstname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$secondname</span><span style="color: #000099; font-weight: bold;">\&quot;</span>,<span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$email</span><span style="color: #000099; font-weight: bold;">\&quot;</span>, <span style="color: #000099; font-weight: bold;">\&quot;</span><span style="color: #006699; font-weight: bold;">$phonenumber</span><span style="color: #000099; font-weight: bold;">\&quot;</span>)&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #000088;">$db</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">insertData</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;Data inserted&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;script language=&quot;javascript&quot;&gt;
  /* check the length of the element, focus is none present */
  function lengthCheck(elem, thename)
  {
    if (elem.value.length&gt; 0) 
      return true;
    else
    {
      alert(&quot;Please insert the &quot; + thename);
      elem.focus();
    }
  }
&nbsp;
  /* check a email address, using regular expression */
  function emailChecker(elem)
  {
    var reg = /^[\w\-\.\+]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid email address&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  /* check against a phone number. a number being between 11-15 numbers*/
  function checkPhone(elem)
  {
    var reg = /^[0-9]{11,15}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid phone number&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  function checkInputs()
  {
    // obtain inputs
    var firstname = document.getElementsByName(&quot;firstname&quot;).item(0);
    var secondname = document.getElementsByName(&quot;secondname&quot;).item(0);
    var email= document.getElementsByName(&quot;email&quot;).item(0);
    var phonenum = document.getElementsByName(&quot;phonenumber&quot;).item(0);
    /* check the inputs */
    if (lengthCheck(firstname, &quot;first name&quot;)) 
      if (lengthCheck(secondname, &quot;second name&quot;))
	if (emailChecker(email))
	  if (checkPhone(phonenum))
	    return true;
    return false;
  }
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form name=&quot;input&quot; action=&quot;insertData.php&quot; method=&quot;post&quot; onSubmit=&quot;return checkInputs()&quot;&gt;
First Name :
&lt;input type=&quot;text&quot; name=&quot;firstname&quot;/&gt;
&nbsp;
Second Name : 
&lt;input type=&quot;text&quot; name=&quot;secondname&quot;/&gt;
&nbsp;
Email : 
&lt;input type=&quot;text&quot; name=&quot;email&quot;/&gt;
&nbsp;
Phone number : 
&lt;input type=&quot;text&quot; name=&quot;phonenumber&quot;/&gt;
&nbsp;
&lt;input type=&quot;submit&quot; value=&quot;Submit&quot;/&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>If you save that as insertData.php then open up within your web-server.  You will be able to insert data into a database with some javascript / php checks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Web page inputs and insert into database</title>
		<link>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=web-page-inputs-and-insert-into-database</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/#comments</comments>
		<pubDate>Tue, 16 Mar 2010 20:38:04 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL (Structured Query Language)]]></category>
		<category><![CDATA[data insert]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[santize]]></category>
		<category><![CDATA[web page]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=809</guid>
		<description><![CDATA[Also from the other posts, Join files together and simple calculator , I was also asked to do &#8220;Accept form input for a user registration form, and store results in a MySQL table, using PHP. Applicant should demonstrate a knowledge of input validation, and using server-side code to avoid sql injection exploits. The user data [...]]]></description>
			<content:encoded><![CDATA[<p>Also from the other posts, <a href="http://www.codingfriends.com/index.php/2010/03/11/join-files-together-into-a-single-file-and-expand-out/">Join files together</a> and <a href="http://www.codingfriends.com/index.php/2010/03/11/simple-calculator/">simple calculator</a> , I was also asked to do</p>
<p>&#8220;Accept form input for a user registration form, and store results in a MySQL table, using PHP. Applicant should demonstrate a knowledge of input validation, and using server-side code to avoid sql injection exploits. The user data should include first name, last name, email, an phone number.</p>
<p>Usage of Javascript pre-validation would be a plus, as would forward planning, i.e. adding a unique code to the user details that could be used to validate their email address. Suitable MySQL table schema should be demonstrated.&#8221;</p>
<p>To start with, I started at the data insert within a web page</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;insertData.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">onSubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return checkInputs()&quot;</span>&gt;</span>
First Name :
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
Second Name : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;secondname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
Email : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
Phone number : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;phonenumber&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span></pre></div></div>

<p>Which takes in the required input&#8217;s and also within the <a href="http://www.w3schools.com/TAGS/tag_form.asp">form</a> HTML tag, but before sending to the back end PHP part of the exercise, there was a requirement to do some Javascript checking on the inputs.</p>
<p>So here is the javascript that will check the inputs, within the form onsubmit action I call this function checkInputs and the return value (true/false) is returned which gives the form either a action to post back to the server (true return) or to wait for the user correct there inputs (false return).</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    <span style="color: #003366; font-weight: bold;">var</span> firstname <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstname&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I get the data from the webpage, <a href="http://www.java2s.com/Code/JavaScriptReference/Javascript-Methods/getElementsByName.htm">getElementsByName</a> (which since it is a name there could be x amount of elements with that name, so I want the first one (.item(0))</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> checkInputs<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;">// obtain inputs</span>
    <span style="color: #003366; font-weight: bold;">var</span> firstname <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;firstname&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> secondname <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;secondname&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> email<span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;email&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> phonenum <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementsByName</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;phonenumber&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #000066; font-weight: bold;">item</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009966; font-style: italic;">/* check the inputs */</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lengthCheck<span style="color: #009900;">&#40;</span>firstname<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;first name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>lengthCheck<span style="color: #009900;">&#40;</span>secondname<span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;second name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>emailChecker<span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>checkPhone<span style="color: #009900;">&#40;</span>phonenum<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
	    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>and then after getting the elements, I then call different additional functions that I did write within javascript to check the inputs gained.  Here I check the length of a element passed within the one of the parameters within the function parameter list, with using objects you can access the objects value.length (javascript object of a element) and also use the focus function with the element which will focus the element on the webpage for the user to know where to check there input (also with a alert window to say why, e.g. &#8220;please insert some data&#8221;.)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> lengthCheck<span style="color: #009900;">&#40;</span>elem<span style="color: #339933;">,</span> thename<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">value</span>.<span style="color: #660066;">length</span><span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> 
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please insert the &quot;</span> <span style="color: #339933;">+</span> thename<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is a way of using <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a> to check email inputs, basically it first checks to make sure there is a name before the &#8220;@&#8221; and also a at between 2 and 4 names with a &#8220;.&#8221; better them.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #009966; font-style: italic;">/* check a email address, using regular expression */</span>
  <span style="color: #003366; font-weight: bold;">function</span> emailChecker<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> reg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[\w\-\.\+]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">value</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>reg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please insert a valid email address&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Here is a similar way as above for checking a phone number input between 11-15 numbers</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #009966; font-style: italic;">/* check against a phone number. a number being between 11-15 numbers*/</span>
  <span style="color: #003366; font-weight: bold;">function</span> checkPhone<span style="color: #009900;">&#40;</span>elem<span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> reg <span style="color: #339933;">=</span> <span style="color: #009966; font-style: italic;">/^[0-9]{11,15}$/</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>elem.<span style="color: #660066;">value</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span>reg<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">else</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #000066;">alert</span> <span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Please insert a valid phone number&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      elem.<span style="color: #000066;">focus</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is the web page part, and here is the second part where I insert the data into database with php.</p>
<p>But here is the full web page part of the first part.</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">script</span> <span style="color: #000066;">language</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;javascript&quot;</span>&gt;</span>
  /* check the length of the element, focus is none present */
  function lengthCheck(elem, thename)
  {
    if (elem.value.length&gt; 0) 
      return true;
    else
    {
      alert(&quot;Please insert the &quot; + thename);
      elem.focus();
    }
  }
&nbsp;
  /* check a email address, using regular expression */
  function emailChecker(elem)
  {
    var reg = /^[\w\-\.\+]+\@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid email address&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  /* check against a phone number. a number being between 11-15 numbers*/
  function checkPhone(elem)
  {
    var reg = /^[0-9]{11,15}$/;
    if (elem.value.match(reg))
      return true;
    else
    {
      alert (&quot;Please insert a valid phone number&quot;);
      elem.focus();
      return false;
    }
  }
&nbsp;
  function checkInputs()
  {
    // obtain inputs
    var firstname = document.getElementsByName(&quot;firstname&quot;).item(0);
    var secondname = document.getElementsByName(&quot;secondname&quot;).item(0);
    var email= document.getElementsByName(&quot;email&quot;).item(0);
    var phonenum = document.getElementsByName(&quot;phonenumber&quot;).item(0);
    /* check the inputs */
    if (lengthCheck(firstname, &quot;first name&quot;)) 
      if (lengthCheck(secondname, &quot;second name&quot;))
	if (emailChecker(email))
	  if (checkPhone(phonenum))
	    return true;
    return false;
  }
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">script</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;input&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;insertData.php&quot;</span> <span style="color: #000066;">method</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;post&quot;</span> <span style="color: #000066;">onSubmit</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;return checkInputs()&quot;</span>&gt;</span>
First Name :
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;firstname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
Second Name : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;secondname&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
Email : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;email&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
Phone number : 
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;phonenumber&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;submit&quot;</span> <span style="color: #000066;">value</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Submit&quot;</span><span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/16/web-page-inputs-and-insert-into-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>AJAX &#8211; setup and test</title>
		<link>http://www.codingfriends.com/index.php/2010/03/08/ajax-setup-and-test/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=ajax-setup-and-test</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/08/ajax-setup-and-test/#comments</comments>
		<pubDate>Mon, 08 Mar 2010 09:53:22 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[setup and test]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=782</guid>
		<description><![CDATA[AJAX is the way of communicating with a back end server without having to send the full information (you can of course) but for example you could just send a username check to see if it is available, but the main thing is that you do not send back a full page but only the [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://en.wikipedia.org/wiki/Ajax_%28programming%29">AJAX</a> is the way of communicating with a back end server without having to send the full information (you can of course) but for example you could just send a username check to see if it is available, but the main thing is that you do not send back a full page but only the part that you want to update.  </p>
<p>With reference to the example of a username, you could just send the username and send back either yes or no response which saves allot of time and traffic from the client to the server (and makes the whole web page experience nicer). </p>
<p>All the AJAX is shorthand for &#8220;Asynchronous JAvascript and Xml&#8221;, asynchronous means that you can do something else whilst waiting for the response (put the kettle on and get a cup for the drink whilst the kettle is boiling) thus with javascript on the client web browser sends a request to a web page on the server with XML wrappings. </p>
<p>To get the basics lets start with the being</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #006600; font-style: italic;">// this function will return a XmlHttpRequest object that allows you to &quot;talk&quot; to the server.</span>
  <span style="color: #003366; font-weight: bold;">function</span> GetXmlHttpObject<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;">// IE7+, FF, Chrome, Opera, Safari</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// IE6 , IE5</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</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: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>the GetXmlHttpObject will return a object that will allow the javascript to talk to the backend server, the newer version is call a XMLHttpRequest whilst on older browsers it was part of the ActiveXObjects.</p>
<p>The next is to send a request to the backend server</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">    xmlHttpObject.<span style="color: #660066;">onreadystatechange</span><span style="color: #339933;">=</span>callBackFunction<span style="color: #339933;">;</span>
    xmlHttpObject.<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> GetUrl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    xmlHttpObject.<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: #339933;">;</span></pre></div></div>

<p>The onreadystatechange, will call a function (in javascript on the client browser) when the request alters from different states, the different states are </p>
<ul>
<li>1.open method invoked successful, open a connection with the server</li>
<li>2.server responsed with a valid header response.</li>
<li>3.server has sent some data, the response content is started to load.</li>
<li>4.server has finished sending all of data</li>
</ul>
<p>so from reading the states, you are really interested in state 4,  because that will have the data (server response) that you are interested in for this.</p>
<p>The .open forms the request to the XmlHttpRequest object to call (&#8220;GET&#8221; in HTML) the server web site, the GetUrl is just a variable that well call a php page (&#8220;ajaxbackendcall.php&#8221;) which takes a parameter called name and returns a string with the name in reverse (shall include that source code later).</p>
<p>And then the .send will start the ready states to change and sends the request to the backend server, here is the function that is called on the state change</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  <span style="color: #003366; font-weight: bold;">function</span> callBackFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpObject.<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: #000066;">alert</span><span style="color: #009900;">&#40;</span>xmlHttpObject.<span style="color: #660066;">responseText</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>What is happening here, is that from the state stages I am waiting on ready state to equal 4 ( when the server has finished responding) and then output the response from the responseText which is filled from the AJAX call to the backend server.</p>
<p>That is mainly it, here is some full code for you to try out save this as &#8220;codingfriends.com.ajax.test.html&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html3/strict.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
<span style="color: #339933;">&lt;</span>script type<span style="color: #339933;">=</span><span style="color: #3366CC;">&quot;text/javascript&quot;</span><span style="color: #339933;">&gt;</span>
  <span style="color: #006600; font-style: italic;">// the xmlHttpRequest object</span>
  <span style="color: #003366; font-weight: bold;">var</span> xmlHttpObject<span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the ajax call </span>
  <span style="color: #003366; font-weight: bold;">function</span> getInnerText<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;">// get the name to send to the server backend page</span>
    <span style="color: #003366; font-weight: bold;">var</span> namehere <span style="color: #339933;">=</span> document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;namehere&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// request the xmlHttpObject </span>
    xmlHttpObject <span style="color: #339933;">=</span> GetXmlHttpObject<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// if there is not xmlHttpRequest object being allowed to be created then the browser does not support it.</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpObject<span style="color: #339933;">==</span><span style="color: #003366; font-weight: bold;">null</span><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;Browser does not support XmlHttp calls e.g. AJAX&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
      <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// fill in the request details, e.g. the url to call and also the query string inserted into the url</span>
    <span style="color: #003366; font-weight: bold;">var</span> GetUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;ajaxbackendcall.php&quot;</span><span style="color: #339933;">+</span><span style="color: #3366CC;">&quot;?name=&quot;</span><span style="color: #339933;">+</span>namehere.<span style="color: #660066;">value</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// here is the call to the server.</span>
    <span style="color: #006600; font-style: italic;">// to start with setup which function to call when a ready state on the XmlHttpRequest object has changed</span>
    xmlHttpObject.<span style="color: #660066;">onreadystatechange</span><span style="color: #339933;">=</span>callBackFunction<span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// request the html &quot;GET&quot; to obtain the url (e.g. the backend server page, dynmaic normally)</span>
    xmlHttpObject.<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> GetUrl<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #006600; font-style: italic;">// and then issue it</span>
    xmlHttpObject.<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: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// the function that is called once the XmlHttpRequest object state has changed</span>
  <span style="color: #006600; font-style: italic;">/* the readyStates are 
    1 open method invoked successful, open a connection with the server
    2 server responsed with a valid header response.
    3 server has sent some data, the response content is started to load.
    4 server has finished sending all of data
  */</span>
  <span style="color: #006600; font-style: italic;">// so we listen for readyState 4 when all finished</span>
  <span style="color: #006600; font-style: italic;">// and output the response text into the div id innertextoutput</span>
  <span style="color: #003366; font-weight: bold;">function</span> callBackFunction<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
  <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>xmlHttpObject.<span style="color: #660066;">readyState</span> <span style="color: #339933;">==</span> <span style="color: #CC0000;">4</span><span style="color: #009900;">&#41;</span>
      document.<span style="color: #660066;">getElementById</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;innertextoutput&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">innerHTML</span><span style="color: #339933;">=</span>xmlHttpObject.<span style="color: #660066;">responseText</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #006600; font-style: italic;">// this function will return a XmlHttpRequest object that allows you to &quot;talk&quot; to the server.</span>
  <span style="color: #003366; font-weight: bold;">function</span> GetXmlHttpObject<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;">// IE7+, FF, Chrome, Opera, Safari</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">XMLHttpRequest</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">new</span> XMLHttpRequest<span style="color: #339933;">;</span>
&nbsp;
      <span style="color: #006600; font-style: italic;">// IE6 , IE5</span>
      <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>window.<span style="color: #660066;">ActiveXObject</span><span style="color: #009900;">&#41;</span> <span style="color: #000066; font-weight: bold;">return</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: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>script<span style="color: #339933;">&gt;</span>
&lt;/head&gt;
&lt;body&gt;
&lt;a href=&quot;#&quot; onclick=&quot;javascript:getInnerText()&quot;&gt;click here to return text from a ajax call&lt;/a&gt;
&lt;br/&gt;
Enter the name here &lt;input id=&quot;namehere&quot;/&gt;
&lt;br/&gt;
&lt;div id=&quot;innertextoutput&quot;&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;</pre></div></div>

<p>and then save this as the ajaxbackendcall.php file to call (in the same directory as the code above and also within a directory that has PHP plugin enable for that webserver be it apache or IIS.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
    <span style="color: #000088;">$name</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'name'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// change the output to say something else, here I am just reversing the name</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
      <span style="color: #666666; font-style: italic;">// have to insert something into it so that php does not make it into a array but a string</span>
      <span style="color: #000088;">$namereturn</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #339933;">;</span>
      <span style="color: #000088;">$j</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
      <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #990000;">strlen</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span>
      <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$namereturn</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$j</span><span style="color: #339933;">++</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$name</span><span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
      <span style="color: #009900;">&#125;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;normal name <span style="color: #006699; font-weight: bold;">{$name}</span> and in reverse <span style="color: #006699; font-weight: bold;">{$namereturn}</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">else</span>
      <span style="color: #b1b100;">return</span> <span style="color: #0000ff;">&quot;No name inputted&quot;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

<p>The output would be similar to the below. OUTPUT<br />
<script type="text/javascript">
  // the xmlHttpRequest object
  var xmlHttpObject;  // the ajax call 
  function getInnerText()
  {
    // get the name to send to the server backend page
    var namehere = document.getElementById("namehere");    
    // request the xmlHttpObject 
    xmlHttpObject = GetXmlHttpObject();    
    // if there is not xmlHttpRequest object being allowed to be created then the browser does not support it.
    if (xmlHttpObject==null)
    {
      alert("Browser does not support XmlHttp calls e.g. AJAX");
      return;
    }     
    // fill in the request details, e.g. the url to call and also the query string inserted into the url
    var GetUrl = "/phptest/ajaxbackendcall.php"+"?name="+namehere.value;
    // here is the call to the server.
    // to start with setup which function to call when a ready state on the XmlHttpRequest object has changed
    xmlHttpObject.onreadystatechange=callBackFunction;
    // request the html "GET" to obtain the url (e.g. the backend server page, dynmaic normally)
    xmlHttpObject.open("GET", GetUrl, true);
    // and then issue it
    xmlHttpObject.send(null);
  }
  // the function that is called once the XmlHttpRequest object state has changed
  /* the readyStates are 
    1 open method invoked successful, open a connection with the server
    2 server responsed with a valid header response.
    3 server has sent some data, the response content is started to load.
    4 server has finished sending all of data
  */
  // so we listen for readyState 4 when all finished
  // and output the response text into the div id innertextoutput
  function callBackFunction()
  {
    if (xmlHttpObject.readyState == 4)
      document.getElementById("innertextoutput").innerHTML=xmlHttpObject.responseText;
  }  
  // this function will return a XmlHttpRequest object that allows you to "talk" to the server.
  function GetXmlHttpObject()
  {
      // IE7+, FF, Chrome, Opera, Safari
      if (window.XMLHttpRequest) return new XMLHttpRequest;      
      // IE6 , IE5
      if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
  }
</script><br />
<a onclick="javascript:getInnerText()">click here to return text from a ajax call</a><br />
Enter the name here<br />
<input id="namehere"/>
<div id="innertextoutput"></div>
<p>END OF OUTPUT</p>
<p>If you do not have a PHP webserver to test with, you can just alter the codingfriends.com.ajax.test.html code by altering the backend web page to call to this</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> GetUrl <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;nonephpbackend.html&quot;</span></pre></div></div>

<p>and create a page within the same directory as the codingfriends.com.ajax.test.html page and place something inside it like</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">hi there</pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/08/ajax-setup-and-test/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>hover over to show a hidden element</title>
		<link>http://www.codingfriends.com/index.php/2010/03/03/hover-over-to-show-a-hidden-element/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hover-over-to-show-a-hidden-element</link>
		<comments>http://www.codingfriends.com/index.php/2010/03/03/hover-over-to-show-a-hidden-element/#comments</comments>
		<pubDate>Wed, 03 Mar 2010 10:46:28 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[css only]]></category>
		<category><![CDATA[hidden]]></category>
		<category><![CDATA[hide]]></category>
		<category><![CDATA[hover]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=764</guid>
		<description><![CDATA[Sometimes you may not have the luxury of having javascript to do some of the great things that it can do, like showing hidden content on the screen when you hover over a HTML element (like a A HTML tag for example). In CSS you can achieve a similar result with using some of the [...]]]></description>
			<content:encoded><![CDATA[<p>Sometimes you may not have the luxury of having javascript to do some of the great things that it can do, like showing hidden content on the screen when you hover over a HTML element (like a A HTML tag for example).  In CSS you can achieve a similar result with using some of the overflow and styling elements of un/ordered lists to accomplish a similar result.</p>
<p>If you save this code as codingfriends_showhide.html and then open it up in <a href="http://www.mozilla-europe.org/en/firefox/">Firefox</a> or your browser of choice (not sure if it will work in IE6 shall have to check it !). and then just hover over the block and it will display the hidden message <img src='http://www.codingfriends.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #00bbdd;">&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Strict//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd&quot;&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">html</span> xmlns<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">meta</span> <span style="color: #000066;">http-equiv</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Content-Type&quot;</span> <span style="color: #000066;">content</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/html; charset=UTF-8&quot;</span> <span style="color: #66cc66;">/</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">title</span>&gt;</span>CSS - show/hide on hover<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">title</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">style</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text/css&quot;</span>&gt;</span>
/* the main ul block, there is no list style at all e.g. all together */
#showhideblock {
	list-style:none;
	margin:0;
	padding:0;
}
/* the main ul block elements push to the left and make there display inline e.g. no breaks */
#showhideblock li {
	float:left;
	width:200px;
	height:55px;
	display:inline;
	border:1px solid black; 
}
&nbsp;
/* the basic showing element of the list */
#showhideblock li .show{
	display:block;
	width:200px;
	height:55px;
}
/* hide the hiding element of the list with the height of 0 and overflow set to hidden */
/* but once shown display in black */
#showhideblock li .hide {
	color : black;
	height: 0; /* hide the .hide element */
	overflow: hidden;
	background:black;
}
/* when you hover over the shown element, set the hidden element sizes */
#showhideblock li:hover .hide, #showhideblock li.over .hide {
	height: 55px;
	width:  200px;
}
/* when you hover over the shown element set the shown element height and overflow values */
#showhideblock li:hover .show, #showhideblock li.over .show {
	height: 0;
	overflow: hidden;
}
&nbsp;
/* set the show a href links to colour of black (on the white background of the shown element */
/* and the textual size to be bigger than normal */
#showhideblock li a {
	color:black;
	font-size:1.5em;
}
/* since changing the background colour of the hidden element to black, change the on hover */
/* to have a textual colour of white */
#showhideblock li a:hover {
	color:white;
}
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">style</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">head</span>&gt;</span>
&nbsp;
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;showhideblock&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;show&quot;</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span>  <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;The image to show&quot;</span>&gt;</span>Hover over me, what has been hidden ?<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;hide&quot;</span>&gt;</span>
	<span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">title</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;the hidden link&quot;</span>&gt;</span>Codingfriends - I was hidden here all along<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">body</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">html</span>&gt;</span></pre></div></div>

<p>In essence the main parts of the code are as follows, you need to set the unordered list to have a list style of none with the list elements (li) having a display of being inline (which means that there is no breaks between the elements).</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">#showhideblock {
	list-style:none;
...
#showhideblock li {
	display:inline;</pre></div></div>

<p>with this in place you can use the size of show and hide elements with also the overflow set to hidden (when you hover over the block you need to set the shown element overflow to hidden and set the size of the hidden element so that it shows)</p>

<div class="wp_syntax"><div class="code"><pre class="html4strict" style="font-family:monospace;">#showhideblock li .hide {
	color : black;
	height: 0; /* hide the .hide element */
	overflow: hidden;</pre></div></div>

<p>And once you have saved the above file and opened up in your browser of choice when you now hover over the &#8220;Hover over me&#8221; text you will see the hidden text instead.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/03/03/hover-over-to-show-a-hidden-element/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java applet &#8211; hello world</title>
		<link>http://www.codingfriends.com/index.php/2010/01/30/java-applet-hello-world/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java-applet-hello-world</link>
		<comments>http://www.codingfriends.com/index.php/2010/01/30/java-applet-hello-world/#comments</comments>
		<pubDate>Sat, 30 Jan 2010 13:00:28 +0000</pubDate>
		<dc:creator>genux</dc:creator>
				<category><![CDATA[HTML  & CSS]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[applet]]></category>

		<guid isPermaLink="false">http://www.codingfriends.com/?p=603</guid>
		<description><![CDATA[A java applet is a web object that allows you to do some funky things within a web page, you could even have a game on the screen with a java applet. But as normal you have to start from the start as such, the most used starts are the classic &#8220;hello world&#8221; example. In [...]]]></description>
			<content:encoded><![CDATA[<p>A java applet is a web object that allows you to do some funky things within a web page, you could even have a game on the screen with a java applet.  But as normal you have to start from the start as such, the most used starts are the classic &#8220;hello world&#8221; example.</p>
<p>In a java applet, it is basically a class called Applet that you extend from and this has all of base code inside it so you only need to implement the virtual functions </p>
<ol>
<li>init</li>
<li>start</li>
<li>paint</li>
<li>destory</li>
<li>stop</li>
</ol>
<p>And these are the basic functions that allow you to call other classes of your choice.  The init &#8211; (initialize), setups up the environment of your choice, start is where the application (GUI interface) will run within, paint is what is painted to the applet screen, destory is when you destory the applet you may need to clean up some code, stop is when the application/applet is stopped.</p>
<p>The main basic class will extend from a applet (java.applet.Applet) so you will need to import that class library</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.applet.Applet</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// basic class extends the applet</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">applet</span>.<span style="color: #003399;">Applet</span> <span style="color: #009900;">&#123;</span></pre></div></div>

<p>and then implement the functions start,init,stop,destory and paint if required.  Below is more code that will resize the basic applet object and display (paint) the words Hello World using the Graphics class object <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/awt/Graphics.html#drawString%28java.lang.String,%20int,%20int%29">drawString</a> method.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.Graphics</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// need this for the Graphics part of the paint function</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.applet.Applet</span><span style="color: #339933;">;</span>		<span style="color: #666666; font-style: italic;">// to extend from.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld <span style="color: #000000; font-weight: bold;">extends</span> java.<span style="color: #006633;">applet</span>.<span style="color: #003399;">Applet</span> <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// resize the screen </span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	resize<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">150</span>,<span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> start<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// draw with the Graphics class object a string on the canvas, point 50, 25</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> paint<span style="color: #009900;">&#40;</span><span style="color: #003399;">Graphics</span> graphicsObj<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
	graphicsObj.<span style="color: #006633;">drawString</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World!&quot;</span>, <span style="color: #cc66cc;">50</span>,<span style="color: #cc66cc;">25</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> stop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> destroy<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Is you save that as &#8220;HelloWorld.java&#8221; and then you need to compile it into a class file (which is a common object file that java can read to run within the virtual machine).</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">javac HelloWord.java</pre></div></div>

<p>which will output the HelloWorld.class file, which you will need to include into the html code applet tag, below is the HTML code</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;HTML&gt;
&lt;head&gt;
&lt;title&gt;A hello world&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
Test output of the program
&lt;applet code=&quot;HelloWorld.class&quot; width=150 height=25&gt;
&lt;/applet&gt;
&lt;/body&gt;
&lt;/HTML&gt;</pre></div></div>

<p>The output would be</p>
<p>Test output of the program <applet code="HelloWorld.class" codebase="/Appletsexamples/"  archive="HelloWorld.jar" width="150" height="25"></applet></p>
<p>Note: If you look at the source code of this page, it will say</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;applet code=&quot;HelloWorld.class&quot; codebase=&quot;/Appletsexamples/&quot;  archive=&quot;HelloWorld.jar&quot; width=&quot;150&quot; height=&quot;25&quot;&gt;&lt;/applet&gt;</pre></div></div>

<p>I had to add in the codebase and archive because of the hosting, to create a jar file you just need to do</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">jar cf <span style="color: #000000; font-weight: bold;">&lt;</span>tarname<span style="color: #000000; font-weight: bold;">&gt;</span> <span style="color: #000000; font-weight: bold;">&lt;</span><span style="color: #c20cb9; font-weight: bold;">file</span><span style="color: #7a0874; font-weight: bold;">&#40;</span>s<span style="color: #7a0874; font-weight: bold;">&#41;</span> name<span style="color: #000000; font-weight: bold;">&gt;</span>
&nbsp;
jar cf HelloWorld.jar HelloWorld.class</pre></div></div>

<p>The jar file is basically a zipped archive.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.codingfriends.com/index.php/2010/01/30/java-applet-hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

