Tech Guide Blog

Tech Guide Programming Tutorial Tips Tricks

AJAX Introduction for Beginners

Posted by Admin On July - 3 - 2010

Asynchronous JavaScript and XML or AJAX in brief, is a method we use to access data from server asynchronously, utilize JavaScript object, XMLHttpRequest. It called asynchronous because we in client-side, don’t have to refresh the page when do request to the server, make the web page more interactive, dynamic and faster than old way we create a web page.

Comparasion between Classic and Ajax

Comparison between Classic and Ajax

Comparison between Classic and Ajax

Comparison between Classic and Ajax

So how we can use it? The example below needs a server to try, you can use XAMPP or your web-server to give it a try. Create an Ajax Script first, I put it in head tag.

<html>
<head>
<script language="javascript" type="text/javascript">
num=1;
function ajaxFunction(){
	// Create an AJAX variable, name doesn't have to be same
	var ajaxRequest;
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	}
	catch (e){
		// Internet Explorer
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e){
				// Error from browser
				alert("ERROR: Reinstall your browser.");
				return false;
			}
		}
	}

	ajaxRequest.onreadystatechange = function(){
		// ready state = 4, the request has been processed, ready to get the data
		if (ajaxRequest.readyState == 4)
		{
			//we put the data requested to id = idn in your page
			document.getElementById("idn").innerHTML = ajaxRequest.responseText;
		}
	}
        // we send a variable to server , a var variable is filled with num value
	ajaxRequest.open("GET", "a.php?var="+num, true);
	ajaxRequest.send(null);

	num = num + 1;
	if (num == 5)
	num = 1;
}
</script>
</head>

How we call the function? Just create a button or something to trigger the function. For example when we press a button in that page.

<body>
<input type="button" value="press" onclick="ajaxFunction()" />
<br/>
<div id="idn">
This is where the request will be placed.
</div>
</body
</html>

I’ve mentioned php file “a.php” to receive the request, then we should create a php file with same filename mentioned above.

<?
echo $_GET['var'];
	if ($_GET['var'] == 1)
		echo "hahaha";
	else if ($_GET['var'] == 2)
		echo "hihihi";
	else if ($_GET['var'] == 3)
		echo "huhuhu";
        else if ($_GET['var'] == 4)
		echo "hehehe";
        else if ($_GET['var'] == 5)
		echo "hohoho";
?>

The output is depend on how many times you click the button. The first time you click the button, it should say “hahaha”, because the initial value of num variable is 1. However, if var value is 5, it will reset to 1 after the server processed the request, so the “hohoho” will never showed up.

Popularity: 2% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • MySpace
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter

Conditional in PL/SQL* Oracle 10g

Posted by Admin On January - 16 - 2010

In PL/SQL* Oracle, we can use conditional statement like other programming language, such as CASE and IF statement. How to do it in PL/SQL? Here’s the syntax for IF statement.

IF condition THEN
  statements;
[ELSIF condition THEN
  statements;]
[ELSE
  statements;]
END IF;

Just remember, I write ELSIF,  it’s on purpose and it’s not mistyping, in this case we write “else if” in PL/SQL as ELSIF. There is an example for IF statement usage in PL/SQL. This statement will ask your age and show output based on your age input.

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT age PROMPT 'Insert your age'
DECLARE
  age NUMBER := &age;
BEGIN
  IF age < 17 THEN
    DBMS_OUTPUT.PUT_LINE('You are teen');
  ELSIF age < 56 THEN
    DBMS_OUTPUT.PUT_LINE('You are adult');
  ELSE
    DBMS_OUTPUT.PUT_LINE('You are old');
END;
/

Just a little different than IF, CASE expression selects a result and returns it. The values returned by CASE is used to select one of several alternatives. The CASE syntax is shown below.

CASE selector
  WHEN axpression1 THEN result1
  WHEN expression2 THEN result2
  ...
  WHEN expressionN THEN resultN
  [ELSE resultN+1]
END;
/

For the example of CASE expression, we can use IF statement example above, just change it to CASE expression. We add text variable to store the return values from CASE expression since CASE return values.

SET SERVEROUTPUT ON
SET VERIFY OFF
ACCEPT age PROMPT 'Insert your age'
DECLARE
  age NUMBER := &age;
  text VARCHAR2(20);
BEGIN
  text:=
    CASE
      WHEN age < 17 THEN 'You are teen'
      WHEN age < 56 THEN 'You are adult'
      ELSE 'You are old'
    END;
  DBMS_OUTPUT.PUT_LINE(text);
END;
/

Example above didn’t use selector after CASE expression, you can use selector as alternatives way. I don’t really use CASE with selector anyway, but I will give you an example with selector below, I hope this can help.

SET SERVEROUTPUT ON
SET VERIFY OFF
DECLARE
  grade CHAR(1) := UPPER('&grade');
  text VARCHAR2(20);
BEGIN
  text :=
    CASE grade
      WHEN 'A' THEN 'Excellent'
      WHEN 'B' THEN 'Very Good'
      WHEN 'C' THEN 'Good'
      WHEN 'D' THEN 'Bad'
      WHEN 'E' THEN 'Poor'
      ELSE 'Not valid grade'
    END;
  DBMS_OUTPUT.PUT_LINE(text);
END;
/

I think it’s enough for Conditional in PL/SQL Oracle tutorial, maybe this can help you little, feel free to give comment to improve my blog.

Popularity: 37% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • MySpace
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter

Recursion in Programming

Posted by Admin On August - 9 - 2009

What exactly recursion is in programming? Recursion is a function that calls the function itself. Thus, in the body of function there is a command that called the function itself. If you use it well, it can solve a problem much more simple and easy. Recursion itself uses a large amount of memory, so be aware not to use it if not necessary. To make a good recursion, of course the functions must have a condition that makes it stop, so the function will not do an infinite looping. I’ve tried it without a stop condition, I compiled it and wow, NOD32 detected my .exe as a virus, I don’t know if the NOD32 made a false detection or not, but if it’s true, then it’s not too difficult to make a virus, eh? Just a little mistake in a program that you’ve made for good reasons and you’ve made a virus.

This code below is an example of how recursion function is working.

#include <stdio.h>
#include <conio.h>

void letter(char alp)
{
	if (alp <= 'e')
	{
		printf("%c ", alp);
		letter(alp + 1);
	}
}

void main()
{
	char temp = 'a';
	letter(temp);
	getch();
}

We call the function and it will print letters from ‘a’ to ‘e’. From the start of the program at void main, we fill a variable with char type called temp with ‘a’. We then call the letter function with temp as parameter, into the letter function, the variable named alp will be filled with ‘a’. Here’s the stop condition, if alp is lower or equal than ‘e’, the function will continue its routine, the function will print what is in the alp variable. After the function printed what is in the alp variable, it call itself with alp + 1 as the parameter, thus this looks like the function is going down 1 level. This routine ended when alp variable is filled with ‘f’, it will go up to the upper level and so on until back to the top level.

It’s a little confusing for me, so I made a ‘level’ for this explanation. Every time a function (you can say function1) called another function (function2), I decided to make a function2 as level 2 function, below the level 1 function (function1). So when letter function with temp as parameter called a letter function with alp + 1 parameter, I set in my mind the letter function with temp parameter as level 1 function and letter function with alp + 1 parameter as level 2, letter function with (alp + 1) + 1 parameter as level 3 function respectively. I hope this tutorial can help.

Popularity: 10% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • MySpace
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter