Monday, November 1, 2010

Implementing AJAX

In the last post, we have learnt that what is AJAX.
Now in this post, we will implement it using PHP.
 The only important thing at the moment is that AJAX uses JavaScript so it needs to be enabled in your browser to successfully implement it.
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. At the end we will put the result into the second input field.
Let us see some simple html code:
<html>
<head><title>Ajax - PHP example</title>
</head>
<body>
<form name="testForm">
 Input text: <input type="text"  onkeyup="doWork();" name="inputText" id="inputText" />
 Output text: <input type="text" name="outputText" id="outputText" />
 </form>
</body>
</html>

Tuesday, October 26, 2010

Introduction to AJAX

·         Hmm... very interesting topic now i.e. AJAX.

·         First of all, what is AJAX????

·         AJAX = Asynchronous JavaScript and XML.

·         AJAX is a technique for creating fast and dynamic web pages.

·         It is a group of interrelated web development techniques used on the client-side to create interactive web applications.

·         With Ajax, web applications can retrieve data from the server asynchronously in the background without interfering with the display and behavior of the existing pageAJAX allows web pages to be updated asynchronously by exchanging small amounts of data with the server behind the scenes. This means that it is possible to update parts of a web page, without reloading the whole page.

·         Data is usually retrieved using the XMLHttpRequest object.

·         Like DHTML and LAMP, Ajax is not a technology in itself, but a group of technologies.

·         Classic web pages, (which do not use AJAX) must reload the entire page if the content should change.

·         Examples of applications using AJAX: Google Maps, Gmail, Youtube, and Facebook tabs.

Wednesday, September 8, 2010

PHP Mail

• PHP Mail Introduction: One of the major uses of a server side scripting language is to provide a way of sending e-mail from the server and, in particular, to take form input and output it to an e-mail address. The mail() function allows you to send emails directly from a script.

• Requirements: For the mail functions to be available, PHP requires an installed and working email system i.e SMTP server that PHP can connect to.

Knowing SMTP server: SMTP is the acronym for Simple Mail Transfer Protocol, and an SMTP server is the machine that runs this protocol and sends out the mail. Running the protocol essentially means running a program such as Sendmail or Qmail if you're on a non-Windows machine. On Windows, the SMTP Service that is part of the Windows NT Service Pack or built in to the Windows 2000 operating system is typically the one running.

• "How can a server be available if it's not currently being run?" you might ask. Well, if that machine is connected to the Internet via a dial-up connection (or DSL or cable), you can use your ISP's outgoing mail server. For example, if your development machine is a Windows 98 box with a 56Kbps modem connected to the Internet via EarthLink, then you can use mail.earthlink.net as your SMTP server. Whatever e-mail client you use (Eudora, Outlook, Netscape Mail, and so on) as your outgoing mail server will also function within your PHP code as your SMTP server. The trick is making PHP aware of this little fact.

• The program to be used is defined by the configuration settings in the php.ini file.

Installation: The mail functions are part of the PHP core. There is no installation needed to use these functions.

• Runtime Configuration: The behavior of the mail functions is affected by settings in the php.ini file. In the php.ini master configuration file located in wamp icon, there are a few directives that need to be set up so that the mail() function works properly.

The options you want to check are:

• SMTP

• sendmail_from

• sendmail_path

Saturday, August 21, 2010

Thursday, August 19, 2010

PHP Quiz

Guys!!!Let’s have some fun...here are some questions 4 u based on php. So ready????Here they are:

1. A script is a

a. Program or sequence of instructions that is interpreted or carried out by processor directly

b. Program or sequence of instruction that is interpreted or carried out by another program

c. Program or sequence of instruction that is interpreted or carried out by web server only

d. None of above



2. When compared to the compiled program, scripts run

a. Faster

b. Slower

c. The execution speed is similar

d. All of above



3. PHP is a widely used ……………. scripting language that is especially suited for web development and can be embedded into html

a. Open source general purpose

b. Proprietary general purpose

c. Open source special purpose

d. Proprietary special purpose

Wednesday, August 18, 2010

PHP and Database

Select Statement:


• So far, we have created a new table and inserted data into that table. In this lesson we will cover the most common MySQL Query that is used to retrieve information from a database.

• The SELECT statement is used to select data from a database.

Syntax: SELECT column_name(s)FROM table_name

Example: The following example selects all the data stored in the "Persons" table created in previous lesson.

<?php

$con = mysql_connect("localhost","root","abc123");

if (!$con)

{

die('Could not connect: ' . mysql_error());

}

mysql_select_db("my_db", $con);

$result = mysql_query("SELECT * FROM Persons");

while($row = mysql_fetch_array($result))

{

echo $row['FirstName'] . " " . $row['LastName'] ] . " " . $row['Age'];

echo "<br />";

}

mysql_close($con);

?>

Monday, August 16, 2010

PHP and Database

Create Statement:


• Create a Database:

• The CREATE DATABASE statement is used to create a database in MySQL.

Syntax: CREATE DATABASE database_name;

• To get PHP to execute the statement above we must use the mysql_query() function.

mysql_query - Send a MySQL query.

Description: resource mysql_query ( string query [, resource link_identifier] ).

• mysql_query() sends an unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.

Parameters :

query: A SQL query (The query string should not end with a semicolon).

link_identifier: The MySQL connection. If the link identifier is not specified, the last link opened by mysql_connect() is assumed. If no such link is found, it will try to create one as if mysql_connect() was called with no arguments. If by chance no connection is found or established, an E_WARNING level warning is generated.

Return Values:

• For SELECT, SHOW, DESCRIBE or EXPLAIN statements, mysql_query() returns a resource on success, or FALSE error.

• For other type of SQL statements, UPDATE, DELETE, DROP, etc, mysql_query() returns TRUE on success or FALSE on error.

Saturday, August 14, 2010

PHP and Database

• Its tym 2 add some dynamic content 2 ur website. The best choice for ease-of-use, price and support is the combination of PHP and MySQL.
• MySQL is currently the most popular open source database server in existence.

• So what is a database actually??

• A database is a structure that comes in two flavors: a flat database and a relational database.

Flat database is a simple database design consisting of one large table instead of several interconnected tables. Called ‘flat’ because of its only two dimensional (data fields and records) structure, these database cannot represent complex data relationships. Also called flat file database or flatform database.

• Whereas, a relational database takes this "flat file" approach several logical steps further, allowing the user to specify information about multiple tables and the relationships between those tables, and often allowing much more declarative control over what rules the data in those tables must obey (constraints). These use language like SQL.

• In a relational structured database there are tables that store data. The columns define which kinds of information will be stored in the table. An individual column must be created for each type of data you wish to store (i.e. Age, Weight, Height).

• On the other hand, a row contains the actual values for these specified columns. Each row will have 1 value for each and every column. For example a table with columns (Name, Age, Weight-lbs) could have a row with the values (Bob, 65, 165).

Tuesday, August 10, 2010

Sessions in PHP

Sessions in PHP


• Hmm....so what are sessions actually???? 

• A session is a semi-permanent interactive information interchange, also known as a dialogue, a conversation or a meeting, between two or more communicating devices, or between a computer and user.

• A session is set up or established at a certain point in time, and torn down at a later point in time.

• A session is typically, but not always, stateful, meaning that at least one of the communicating parts needs to save information about the session history in order to be able to communicate, as opposed to stateless communication, where the communication consists of independent requests with responses and HTTP communication is stateless.

• why use them???

• As a website becomes more sophisticated, so must the code that backs it. When you get to a stage where your website need to pass along user data from one page to another, it might be time to start thinking about using sessions.

• A normal HTML website will not pass data from one page to another. In other words, all information is forgotten when a new page is loaded. This makes it quite a problem for tasks like a shopping cart, which requires data(the user's selected product) to be remembered from one page to the next.

• PHP session solves this problem by allowing you to store user information on the server for later use (i.e. username, shopping cart items, etc). However, this session information is temporary and is usually deleted very quickly after the user has left the website that uses sessions.

• It is important to ponder if the sessions' temporary storage is applicable to your website. If you require a more permanent storage you will need to find another solution, like a MySQL database.

• Sessions work by creating a unique identification(UID) number for each visitor and storing variables based on this ID. This helps to prevent two users' data from getting confused with one another when visiting the same webpage.

• Note: If you are not experienced with session programming it is not recommended that you use sessions on a website that requires high-security, as there are security holes that take some advanced techniques to plug.

Monday, August 9, 2010

Cookies in PHP

 Cookies:


• Before discussing cookies in php, the question is what actually cookies are???? 

• Hmm... so a computer cookie is a small text file which contains a unique ID tag, placed on your computer by the website. The website saves a complimentary file with a matching ID tag. In this file various information can be stored, from pages visited on the site, to information voluntarily given to the site. When you revisit the site days or weeks later, the site can recognize you by matching the cookie on your computer with the counterpart in its database.

• U can also say that, a cookie consists of one or more name-value pairs containing bits of information, which may be encrypted for information privacy and data security purposes. The cookie is sent as an HTTP header by a web server to a web browser and then sent back unchanged by the browser each time it accesses that server.

• There are two types of computer cookies: temporary and permanent. Temporary cookies, also called session cookies, are stored temporarily in your browser's memory and are deleted as soon as you end the session by closing the browser. Permanent cookies, also called persistent cookies, are stored permanently on your computer's hard drive and, if deleted, will be recreated the next time you visit the sites that placed them there.

Thursday, August 5, 2010

Conditional Statements and PHP Functions

Conditional Statements:


• Like in C, in php too v have 4 types of conditional statements:

if statement - use this statement to execute some code only if a specified condition is true.

if...else statement - use this statement to execute some code if a condition is true and another code if the condition is false.

if...elseif....else statement - use this statement to select one of several blocks of code to be executed.

switch statement - use this statement to select one of many blocks of code to be executed.

• These statements r used with same syntax as used in C.

Wednesday, August 4, 2010

Concatenation Operator and Form Handling

The Concatenation Operator:


• The concatenation operator (.) is used to put two strings together like (+) is used in java 4 concatenation.

• Consider the code:

<?php

$txt1=”Hello World!”;

$txt2=”What a nice day!”;

echo $txt1.” ”.$txt2;

?>

Display:

Hello World! What a nice day!

• If we look at the code above you see that we used the concatenation operator two times. This is because we had to insert a third string (a space character), to separate the two strings.



PHP Form Handling:

• U all r familiar with form tag used in html. Let us see an html form having two input fields and one submit button.

<html>

<body>

<form action="welcome.php" method="post">

Name: <input type="text" name="fname" >

Age: <input type="text" name="age" >

<input type="submit" value=”SUBMIT”>

</form>

</body>

</html>

Monday, August 2, 2010

PHP Comment and PHP echo

PHP Comment:


• Syntax for single line comment-

   o <?php

   o #This is an example comment

   o //This is another example comment

   o echo "Sentence 1."; //This is our first statement

   o echo "<br />"; #This is a line break

   o echo "Sentence 2."; // This is our second statement

   o ?>

• U will notice that both types of commenting styles i.e. ‘//’ and ‘#’ r used on a single line .

• But it is very tedious task when needs to comment multiple lines. So there is another way of commenting. This style is used for multiple lines:

  o <?php

  o /* This is an easier

  o way to write comments

  o on multiple lines */

  o ?>

• So ‘/* and */’ is used for multiple line comments.


PHP-echo


• As u know php command echo is used 4 outputting text 2 a web browser but how to use echo command 4 displaying values stored in variables????

• Observe the following code:

<?php

$myString=”Hello!”;

echo $myString;

echo “<h5>I luv using PHP!!</h5>”;

?>

Display:

Hello!

I luv using PHP!!

Saturday, July 31, 2010

Happy Friendship Day

Good news good news!!!!!!!!
no php lessons on Saturday and Sunday....
So njoy ur Sunday and Happy Friendship Day
Have fun!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Friday, July 30, 2010

PHP Variables

• As u know variables r used 4 storing values like text strings, numbers or arrays and when a variable is declared, it can b used over and over again in the script.

• But how variables r declared in php script???[:o]

• All variables in php start wid $ sign symbol.
e.g. $var_name = value;

• Let us take some php code:

• PHP is a loosely typed language:
o In PHP, a variable does not need to be declared before adding a value to it.
o In the example above, you see that you do not have to tell PHP which data type the variable is.
o PHP automatically converts the variable to the correct data type, depending on its value.[:)]
o In a strongly typed programming language, you have to declare (define) the type and name of the variable before using it.
o In PHP, the variable is declared automatically when you use it.

• Naming Rules for variables:
o A variable name must start with a letter or an underscore "_".
o A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ).
o A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)

Thursday, July 29, 2010

PHP Syntax

• PHP syntax is very much similar to language C but the whole code of php is contained within a tag.

• All php codes must be contained within the following:

<?php

?>

Or in shorthand form

<?

?>

But this shorthand tag must be supported by your server and I suggest that standard form should be used i.e. <?php

• As you know that php code is inserted in html code, so if you save your file having html code embedded with php code with extension .php then that becomes your php file. But if you save your file with .html extension then php code will not be interpreted by the server.

• A simple php program having php code embedded in html code:

<html>

<head>

<title>My first php page</title>

</head>

<body>

<?php

echo “Hello World...”;

?>

</body>

</html>



Output:

Hello World...

• After writing this code on a text-editor like notepad, save this file with .php extension(helloworld.php) in www(C:/wamp/www) directory. Now you can run your file on your localhost.

• Let us discuss code:

you all are familiar with html code. New thing is code inside <?php, ?> tags. Echo is a command used for displaying something, you can also use ‘print’ command in place of ‘echo’. What you want to display on screen, write in inverted commas. At the end of each statement there should be a semicolon.

• White space is ignored between php statements.

Wednesday, July 28, 2010

PHP Installation

PHP Installation:

For PHP to work, you need a server as PHP is a server-side scripting language. If your server supports PHP you don't need to do anything.

Just create some .php files in your web directory, and the server will parse them for you. Because it is free, most web hosts offer PHP support.

However, if your server does not support PHP, you must install PHP.

Then you have to download PHP, a database and a server.

PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.).

PHP is compatible with almost all servers used today (Apache, IIS, etc.).

Alternative method:

Install WAMP server on your computer.

WAMPs are packages of independently-created programs installed on computers that use a Microsoft Windows operating system.

In WAMP, W stands for windows, A stands for apache server, M stands for mysql, P stands for php, However, for linux users there is LAMP available. L stands for Linux and remain same.

Functionalities of WAMP:

WampServer's functionalities are very complete and easy to use so we won't explain here how to use them.

With a left click on WampServer's icon, you will be able to:

- manage your Apache and MySQL services

- switch online/offline (give access to everyone or only localhost)

- install and switch Apache, MySQL and PHP releases

- manage your servers settings

- access your logs

- access your settings files

- create alias


With a right click :

- change WampServer's menu language

- access this page

How to start WAMP:

When you install WampServer, a "www" directory is created (generally c:\wamp\www). Create a directory inside for your project and put your PHP files in it.

Click on the link "Localhost" in the WampServer menu or open your browser and open the http://localhost address.


Add Apache, MySQL and PHP releases

WampServer allows you to install almost all the existing releases of Apache, PHP and MySQL so you can reproduce exactly the settings of your production server.

 Then click on the WampServer menu and activate the release that you want to use.


Wait until the WampServer icon become white again and start to work.

PHP Extensions:

PHP files have file extension of “.php”, “.php3”, “.phtml”.

But mostly .php is used.

Important terms:

Localhost: A server application running on your on computer. It always translates to the loopback IP address 127.0.0.1 in IPv4, or ::1 in IPv6.

Tuesday, July 27, 2010

Introduction to PHP

Nowadays, php is used almost in the design of every website. But what is basically php? How it is used? These are the questions which comes to your mind. So let me solve your problem by providing you lessons on daily basis and you can ask about any type of query which comes to your mind. So let us start with today's lesson : Introduction to php (i.e from where it is originated,whats the use of php etc.).
PHP(recursive acronym for PHP:Hypertext Preprocessor):

  • widely used open source general purpose scripting language.
  • originally designed for web development to produce dynamic web-pages.
  • For this purpose, php code is embedded to html code and interpreted by a web server with a PHP processor module,which generates web page.
  • Creator of PHP: Rasmus Lerdorf
  • License: free software created under PHP license.

Important terms:

  • Open Source Software: A computer software whose source code and certain other rights are available to users i.e. users can study,change and improve the software. e.g. PHP, Linux: computer Operating System, Apache: a web server, Firefox: a web browser etc.
  • Scripting Language: A scripting language is a form of programming language that is usually interpreted rather than compiled.e.g. VBScript, Smalltalk, Ruby etc.
  • Dynamic webpage: a kind of web-page which changes with time,user, the user interaction, the context etc. to maintain the page up-to-date.e.g News sites, search sites etc.
  • PHP-License: php is absolutely free as free software and incompatible with GNU General Public License.