FORM element

 WORKING WITH DATA and PHP FUNCTION:

FORM element:-

  • Forms are used to get input from the user and submit it to the webserver for processing.
  • It defines in an HTML tag that contains graphical user interface items such as input box, checks boxes radio buttons, etc.
  • It is using the <form>...</form> tags .
  •  It shows where and how will send the contents of the elements.
  • It contains once submitted.
  • PHP script receives the data from the form and performs an action, ex. updating the database contents
  • , sending an e-mail, testing data format, and so on.
  • This topic process following steps:-
    • Creating forms using buttons, text boxes, and other form elements
    • Creating PHP scripts to process HTML forms
    • use $_POST and $_GET to retrieve data
    • Passing hidden information to the form processing script via hidden form controls and a URL query string First Form Say My Name.

 Example: - A name entered in a form.

1. Create a text file named form1.html and open it in your favorite text editor.

2. Enter the following code:

 

A Simple HTML Form

 <!DOCTYPE html>

 <html>

<head>

<title>A simple HTML form</title>

 </head>

<body>

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

<p><label for=”user”>Name:</label><br/>

  <input  type="text" id="user" name="user"></p>

<p><label for=”message”>Message:</label><br/>

<textarea  id="message" name="message" rows=”5” cols=”40”></textarea></p>

 <button  type="submit" name="submit" value="send">Send Message</button>

 </form>

</body>

 </html>

 

  • Save this text file as ab.html and place that file in web server document root (ex. htdocs in xampp).
  • This defines a form that contains a text field with the name “user”, a text area with the name “message” and a submit button.
  • The FORM element’s ACTION argument points to a file called abc.php, which processes the form information.
  • The method of this form is POST, so the variables are stored in the $_POST superglobal.

 

Reading Input from a Form

The GET Method

The GET method sends the encoded user information appended to the page request.

The page and the encoded information are separated by the? character.

Example:- http://www.test.com/index.htm?name1=value1&name2=value2

 

·   The GET method produces a long string that appears in your server logs, in the browser's Location: box.

·        The GET method is restricted to sending up to 1024 characters only.

·        Never use the GET method if you have a password or other sensitive information to be sent to the server.

·        GET can't be used to send binary data, like images or word documents, to the server.

·        The data sent by GET method can be accessed using the QUERY_STRING environment variable.

·        The PHP provides a $_GET associative array to access all the sent information using the GET method.

  • This is the built-in PHP superglobal array variable that is used to get values submitted via the HTTP GET method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method displays the form values in the URL.
  • It’s ideal for search engine forms as it allows the users to bookmark the results.

 Syntax.

<?php

$_GET['variable_name'];

?>

  HERE,

  • “$_GET[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

 The POST Method

The POST method transfers information via HTTP headers. The information is encoded as described in the case of GET method and put into a header called QUERY_STRING.

·        The POST method does not have any restriction on the data size to be sent.

·        The POST method can be used to send ASCII as well as binary data.

·        The data sent by the POST method goes through the HTTP header so security depends on the HTTP protocol. By using Secure HTTP, you can make sure that your information is secure.

·        The PHP provides a $_POST associative array to access all the sent information using the POST method.

  • This is the built-in PHP superglobal array variable that is used to get values submitted via the HTTP POST method.
  • The array variable can be accessed from any script in the program; it has a global scope.
  • This method is ideal when do not want to display the form post values in the URL.
  • example:- login details to the server.

Syntax.

<?php

 $_POST['variable_name'];

?>

  HERE,

  • “$_POST[…]” is the PHP array
  • “'variable_name'” is the URL variable name.

 ·         The below diagram shows the difference between get and post





The most frequently used form element's attributes:

Attribute

Description

Name

Specifies the name of the form.

action

Specifies the URL of the program or script on the webserver that will be used for processing the information submitted via the form.

method

Specifies the HTTP method used for sending the data to the web server by the browser. The value can be either get (the default) or post.

target

Specifies where to display the response that is received after submitting the form. Possible values are _blank_self_parent, and _top.

enctype

Specifies how the form data should be encoded when submitting the form to the server. Applicable only when the value of the method attribute is post.

 

=========================================== 

Post a Comment

0 Comments