Introduction to PHP – Part9

advertisement

Forms:

A form is an area where user can input data. The place in the form where user input data is called form element. A form element can be a text field, text area field, drop-down list, radio buttons and checkbox. In this tutorial we will apply the knowledge we have gained so far and use it with HTML forms. The best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. That means the data entered in HTML forms can be manipulated using PHP scripts.

Creating form

A form is created using <form> tag. To learn more about HTML please refer to HTML tutorials available in net. Let us start with a simple HTML form that we use for user feedback.

Example#1

<html>
<body>
<form > 
 <p>Name : <input type="text" name="name" /></p>
 <p>Email : <input type="text" name="email" /></p>
 <p>Subject : <input type="text" name="subject" /></p>
 <p>Message : <br/>
 <textarea rows="10" cols="30"> </textarea>
 <p><input type="submit" /></p>
</form>
</body>

The output of the above HTML code will be

Collecting information from HTML form using POST method

As we already mentioned, the best thing to use PHP with HTML is that any elements in HTML forms are directly available in PHP scripts. After filling the form, when we pressed Submit Query button, everything we filled in the form will be available in PHP script.

Example#2

<html>
<body>
<form action="action.php" method="post">
 <p>Name : <input type="text" name="name" /></p>
 <p>Email : <input type="text" name="email" /></p>
 <p>Subject : <input type="text" name="subject" /></p>
 <p>Message : <br/>
 <textarea rows="10" cols="30"> </textarea>
 <p><input type="submit" /></p>
</form>
</body>

We can notice that we added action and method in the above code, which tells the server what action is to be taken and what method is to be used. In this example, when we click on Submit Query button the page action.php is called and the method used is post. In order to access the form elements we should write action.php like this. Note that the file name should be action.php and saved in the same directory.

action.php

<?php
echo $_POST["name"]."<br/>";
echo $_POST["email"]."<br/>";
echo $_POST["subject"]."<br/>";
echo $_POST["message"];
?>

When we fill the form and click the submit button, action.php will be called, the URL will look like http://webanddesigners.com/action.php. Information filled in the form is now available in action.php page. post method use $_POST array to save the form information, the name of the form fields will be the key in the $_POST array.

Collecting information from HTML form using GET method

The following code is exactly same as code in example#2, except that the method used here is GET.

Example#3

<html>
<body>
<form action="action.php" method="get">
 <p>Name : <input type="text" name="name" /></p>
 <p>Email : <input type="text" name="email" /></p>
 <p>Subject : <input type="text" name="subject" /></p>
 <p>Message : <br/>
 <textarea rows="10" cols="30"> </textarea>
 <p><input type="submit" /></p>
</form>
</body>

action.php will look like as the following code. Note that when GET method is used the data elements are available in $_GET array. The data elements are accessed in the similar fashion.

action.php

<?php
echo $_GET["name"]."<br/>";
echo $_GET["email"]."<br/>";
echo $_GET["subject"]."<br/>";
echo $_GET["message"];
?>

Comparison of GET and POST method

When we use GET method, the information sent is visible. It will display the variable and value in browser’s address bar. The address bar will look like http://webanddesigners.com/action.php?name=John&email=john@webanddesigner.com&subject=hi&message=This+is+the+test+message+using+get+method. There is a limitation in the amount of data send using GET method, maximum of 100 characters can be send. One best thing about GET method is that, the page can be bookmarked; this is because all the variables and data are available in URL.

When we use POST method the information sent is completely invisible. In addition, there is no restriction on the amount of information to be sent. Up to 8mb of data can be sent using POST method. However, because data and variables are not available in URL, it is not possible to bookmark the page. The URL will look like http://webanddesigners.com/action.php

 

 

 

About the Author:

Bhoj R Dhakal is a programmer. He has few years of experience in software development and maintenance. He has published series of PHP tutorials for webanddesigners.
  • Steve

    Fantastic tutorial, really helpful. Thanks!

  • Patt

    Great tutorial. Thanks a lot.

  • Pingback: Web Development Using PHP | DevBlogr()

  • Pingback: 80+ Stunning Community Links for Web Designers and Developers | tripwire magazine()

  • Pingback: 80+ Stunning Community Links for Web Designers and Developers | Programming Blog()

  • Pingback: 80+ Stunning Community Links for Web Designers and Developers | Afif Fattouh - Web Specialist()

  • http://www.templatemagz.com/ Free WP Themes

    Thanks for your info, it’s very useful for me

  • http://www.namebadge.ca professional name badges

    Enjoyed the php post. Not many blogs worth bookmarking but this is going on Delicious now!

  • http://www.flashjuggler.com FlashJuggler

    Good tutorial. Very simple and fast. Thanks

  • http://webdirectory Estrella Frawley

    I was recommended this blog by way of my cousin. I’m not positive whether or not this submit is written by way of him as nobody else realize such specific about my difficulty. You’re amazing! Thank you!

    • http://www.webanddesigners.com Bhoj R Dhakal

      Sorry for replying late. So happy that the tutorial solved your difficulty. Thanks for the great compliment.

  • http://www.kazuc.com/ Kazuc is a resource for PHP clone scripts, only at 19.95$

    Pretty nice post. I simply stumbled upon your blog and wished to mention that I’ve really enjoyed surfing around your blog posts. After all I?ll be subscribing for your rss feed and I’m hoping you write once more soon!

    • http://www.webanddesigners.com Bhoj R Dhakal

      Thank you so much, you liked the tutorial. I am working to publish new series of tutorial for PHP in near future. Please subscribe our RSS feed for more information.

  • faysal123

    Here you will find free scripts with demonstration

    http://55points.blogspot.com

    ok