Thursday, 23 May 2013

Introduction to server scripting

PHP :

PHP (Hypertext Preprocessor) is one of the server scripting language widely used. It helps us to make dynamic and interacting web pages.

PHP script goes is placed between <?php  [code goes here] ?>

To communicate between HTML and PHP we generally use forms. There are two methods to collect the data from html, GET and POST.


Sample form : 

<form action="welcome.php" method="post"/"get"> // any one of post or get should be specified.
Name: <input type="text" name="fname">
Age: <input type="text" name="age">
<input type="submit">
</form>


GET Method : The information sent using "GET" method is visible in url. This method should not be used in sending  passwords and other sensitive information. It is not suitable to send very large variables.

Sample PHP code to retrieve data using GET :


Welcome <?php echo $_GET["fname"]; ?>.<br>
You are <?php echo $_GET["age"]; ?> years old!


POST Method : Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.  

Sample PHP code to retrieve data using POST:

Welcome <?php echo $_POST["fname"]; ?>!<br>
You are <?php echo $_POST["age"]; ?> years old.



Note: $_REQUEST can be used for both POST and GET Method .

Sample PHP code using $_REQUEST :

Welcome <?php echo $_REQUEST["fname"]; ?>!<br>
You are <?php echo $_REQUEST["age"]; ?> years old.




Note : Validate form before sending the data to the server so that if any form is wrongly submitted is verified using javascript[browser] and not by php[server]. Hence reducing the server load. 





PHP Database :

MYSQL is widely used database with PHP. MYSQL runs on the server like PHP.

Step 1: Connect to MYSQL Server

PHP Code : mysqli_connect(host,username,password); 
Explained : In XAMPP  host is "localhost"

If username and password are not configured, then by default username is "root" and password is "" 

Step 2: Create a Database 
You can create a database at cpanel {MYSQL} or by using PHP code
PHP Code: http://w3schools.com/php/php_mysql_create.asp


Step 3:  
Creating a TABLE to store the data
Sample code : http://w3schools.com/php/php_mysql_create.asp

Step 4: Do stuff you need to do like inserting data,editing data,deleting data

Step 5: Close the connection 
Sample code : http://w3schools.com/php/php_mysql_connect.asp


Will upload a video on this very soon..



Please post your doubts or suggestions in the comment box below.



No comments:

Post a Comment