Monday, April 4, 2011

Ajax PHP Basics

Ajax PHP Example

In this article I do not want to show you the history of AJAX and discuss its pros and cons, but only focus on how to create a basic working AJAX - PHP communication.

The only important thing at the moment is that AJAX uses JavaScript so it need to be enabled in your browser to successfully complete this tutorial.

To demonstrate the AJAX PHP connection we will create a very simple form with 2 input fields. In the first field you can type any text and we will send this text to our PHP script which will convert it to uppercase and sends it back to us.

So let's list what we need to do:

* Listen on key-press event on the input field.
* In case of key-press send a message to the PHP script on the server.
* Process the input with PHP and send back the result.
* Capture the returning data and display it.

PHP is a highly powerful programming language that can be used for web development as well as native applications. Data intensive online sites such as forums are heavily used with PHP as the programming language can easily handle the massive data changes online.
Sponsored Links


One of the most popular functionalities provided by PHP is the web form. These are fields wherein user data is required before access to any type of information is allowed. Through PHP, the needed information for online access can be extracted and the data can be used for other functions online.

But even with the power of PHP in developing web forms, it's still not as interactive compared to other online web forms. Users still have to click on submit and wait for the next page or page refresh just to know if the fields entered are correct. This setting can be quite frustrating if the web form is very long.

This is where the magic of Ajax can be used. With the help of Ajax functions, users will be able to learn more about the data entered in nearly real-time basis. This cuts down the need to refresh before verifying any data.

The key to this functionality in Ajax is the available functions to check on the data as they are entered. Certain functions can be added to PHP web forms so that the information can be verified without refreshing the information. These functions are not called when the user clicks on "submit" but the function is called as soon as the information is available.

Validation through Available Functions

There are many developers who can easily provide real-time validation process in the web forms. But there are too many libraries for Ajax and JavaScript functions that building an application can be very easy. Instead of developing a new function, developers can simply check into various libraries and frameworks for the needed function.

In this case, the best Ajax function that can support the web form on PHP comes from Prototype framework. There are other frameworks that have the real time validation but Prototype offers an easy to understand function.

Prototype uses "AjaxValidates.php" so that it will enable real time validation of the data entered. Of course, the source "prototype.js" has to be called so that the validation function created specifically for PHP can be used.

Connecting the Ajax Function to PHP Web Form

While the Ajax function that validates the data entered in real time is important, it will never exist without an efficient PHP web form. Developers have to successfully create a PHP web form so that the Ajax function should be able to interpret the entered data.

At first glance, the PHP web form developed for Ajax is almost the same. But since the function provided by Ajax validates in real time, the functions are added after each field. Users can simply transfer to another field in the web form while Ajax processes the data on the previous field and return with the prompt in case the data entered cannot be accepted.

Wednesday, March 23, 2011

Associative Arrays

An associative array,each ID key is associated with a value.
When storing data about specific named values,a numerical array is not always the best way to do it.
With associative arrays we can use the values as keys and assigns values them.
Example 1:
In this example we use an array to assign ages to the different persons:
$ages=array("Peter=>32, "Quamire"=>30, "Joe"=>34);
Example 2:
This example is the sames as example 1,but shows a different way of creating the aray:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
The ID Keys can be used in a script:
<?php
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
echo "Peter is" . $ages['Peter'] . "years old.";
?>
The code above will output:
Peter is 32 years old.

Monday, March 21, 2011

Numeric Arrays

 A numeric array stores each array element with a numeric index.
 There are two methods to create a numeric array.
1.In the following example the index ae automatically assigned (the index stars at 0):
   $cars=array ("Saab","Volvo","BMW","Toyota");
2.In the following example we assign the index manually:
   $cars[0]="Saab"
   $cars[1]="Volvo";
   $cars[2]="BMW";
   $cars[3]="Toyota";
Example
In the following example you access the variables by reffering to the array name and index.
<?php
$cars[0]="Saab";
$cars[1]="Volvo";
$cars[2]="BMW";
$cars[3]="Toyota";
echo $cars[0] . "and" . $cars[1] . "are Swedish cars.";
?>
The code above will output:
Saab and Volvo are Swedish cars.

Wednesday, March 16, 2011

What is an Array?

A variable is a storage area holding a number or text.The problem is,a variable will hold only one value.
An array is a special variable,which can store multiple values in one single variable.
If you have a list of items (a list of car names,for example),storing the cars in single variables could look like this:
$cars1="Saab";
$cars2="Volvo";
$cas3="BMW";

However,what if you want to loop through the cars and find a specific one?And wha if you had not 3 cars,bu 300?
The best solution here is to use an array!
An array can hold all your variable values under a single name.And you can access the values by refferingto the array name.
Each element in the array has its own index so that it can be easily accessed.
In PHP,there are three kinds of arrays:
 *Numeric array-An array with a numeric index.
 *Associative array-An array each ID key is assaociated with a value.
 *Multidimensinal array-An array containing one or more arrays.

Tuesday, March 15, 2011

String Variables in PHP

String variables are used for values that contain characters.
In this chapter we are going to look at the most common functions and operators used to manipulate strings in PHP.
After we create a string we can manipulate it.A string can be used directly in a function or it can be stored in a variable.
Below,the PHP script assigns the text "Hello World" to a string variable called $txt:    
<?php
$txt="Hello World";
echo $txt;
?>

The output of the code above will be:
"Hello World"

Now,lets try to use some different functions and operators to manipulate the string.
The Concatenation Operator
There is only one string operator in PHP.
The concatenation operator (.) is used to put two string values together.
To concatenate two string variables together,use the concatenation operator:
<?php
$txt1="Hell World!";
$txt2="What a nice day!";
echo $txt1 . " " . $txt2;

The output of the code above will be:
Hello World!What a nice day!
If we look at the code above you see hat we used he concatenation operator two times.This is because we had to insert a third string (a space character),to separate the two strings.

The strlen( ) funtion
The strlen ( ) function is used to return the length of a string.
Lt's find the length of a string:
<?php
echo strlen("Hello World!");
?>

The output of the code wll be:
12
The length of a string is often used in loops or other functions,when it is imporant to know when the sring end .(i.e in a loop,we would want to stop the loop after the last character in the string).
The strpos( ) funcion
he strpos( ) funtion is used to search for character within a string.
If a match is found,this funcion will return the position of the first match.If no match is found,it will return FALSE.
Let's see if we can find the string "world" in our string:
<?php
echo strpos("Hello world!","world");
?>

The output code will be:
6
The position f the string "world" in our string is position 6.The reason that is 6 (and not 7),is that the firstsition in the string is 0,and not 1.

Friday, March 11, 2011

Naming Rules for variables

*A variable name must start with a letter or an underscore "_"
*A variable name can only contain alpha-numeric characters and undersores (a-z,A-Z.0-9,and_)
*A variable name should not contain spaces.If a variable name is more han one word,i should be separated with an undescore ($my_string),or with capitalizaion ($myString)
*Operators are used to operate on values.
*PHP Operators.
*This section lists the different operators used in PHP.

Arithmetic Operators
   
Operator
Description
Example
Result
+
Addition
x=2
x+2
4
-
Subtraction
X=2
5-x
3
*
Multiplication
x=4
x*5
20
%
Modulus(division remember)
5%2
10%8
10%2
1
2
0
/
Division
15/5
5/2
3
2.5
++
Increment
X=5
x++
x=6
--
Decrement
x=5
x--
x=4

Thursday, March 10, 2011

Variables in PHP

variables are used for storing a value,like text strings,numbers or arrays.
when a variable is declared,it can be used over and over again in your script.  
All variables in PHP with a $sign symbol.
The correct way of declaring a variable in PHP:
 $var_name=value; 

New PHP programmers often forget the $sign at the beginning of the variable.In that case it will no work.
Let's try creating a variable containing a string,and a variable containing a number:

    <?php
     $txt="Hello World!";
      
     $x=16;

     ?>

PHP is a Loosely Typed Language. 
In PHP,a variable does not need to be declared before adding a value to it.
In the exmple above,you see that you do not have to tell PHP which data  type the variable is. 
PHP automatically converts the variable to the correct data type,depending on it's value.
In a strongly typed programming language,you have to declare (define) the type and name of  the variable before use it.
In PHP,the variables is declared automatically when you use it.