Articles added in Mathematics Lab | Cyber Lab is now open | Astronomy Lab is now open | VLSI Lab is now open | 120 SEO Tips article published | More exciting articles coming soon!




PHP Forms

Superglobals

  • Variables are either local or global. Local variables can be accessed only inside the function whereas global variables can be accessed only outside the function.

  • But php contains a set of predefined variables that can be accessed anywhere and no scope needs to be considered for it. These variables are called as superglobal variables.


  • THE LIST OF SUPERGLOBAL VARIABLES ARE:
  • $GLOBALS

  • $_SERVER

  • $_REQUEST

  • $_POST

  • $_GET

  • $_FILES

  • $_ENV

  • $_COOKIE

  • $_SESSION


  • $GLOBAL:
  • It is used to access global variables from anywhere. Global variables are stored in the $GLOBALS [index]. And the index is the name of the variable.

  • $_SERVER:
  • It holds the server paths, script locations and links. The $_SERVER["PHP_SELF"] returns the filename of the currently executing script.

  • $_POST:
  • It is used to collect data after an HTML form is submitted, provided that the method is specified as method="post"

  • $_GET:
  • It is also used to collect data after an HTML form is submitted; the method should be specified as method="get"

  • The post method is used more frequently in comparison to get.

  • THE post method is preferred because there is no restriction on the size of the data. Also the post method accepts binary as well as ASCII information



  • PHP FORMS:

  • An application of PHP is used to gather all the information of a user visiting a website and process the information. This is nothing but a form.



  • In the above example, the method used to fetch the information is post and it is specified by method="post". The form action specifies the location where the data should be sent after submitting the HTML form.

  • Then Name is printed, the input type is text as you type the information into the box provided and name="name" is the variable where the information typed by the variable is stored. Similarly for Department the box is provided for entering the information.

  • A submit button is provided so that after clicking it the information entered is submitted.

  • The information entered can be printed on the screen in the following way:


  • The output will be displayed on the screen as:

  • PHP Form

  • If you want to have a box to write the comments or for similar purposes you can use the textarea option. Basically a text area is for multiline input.

  • The syntax is:
    < textarea name="comment" rows="5" cols="40">< /textarea>

  • Here, the number of rows and columns is used to specify the width and height of the box.



  • RADIO BUTTONS:

    Radio Buttons
  • Here, the radio button is used to select the gender as either male or female.

  • The syntax for posting the radio buttons is:



  • Here, the input type specifies that it is a radio button by stating input type="radio", The variable gender specifies the name and the variable gender has two values Male and Female. The syntax specified in green provides the radio button and the word Male and Female gets printed on the screen.

  • To increase the security, htmlspecialchars() function is used. This function converts the special HTML tags like <,> by <, >.



  • To Insert Mandatory Fields:

    *mandatory field

    Insert Mandatory Fields

  • Here, the variable $Err is used to store the error message if a mandatory field is not filled. If there is no error then the input taken from the user is stored in the variable $name. This is done by the inbuilt function test_input.



  • INFORMATION

    * mandatory field.

    Name: *

    Department:
  • Here, the syntax in green is used to display the error. In the action part htmlspecialchars is used to prevent hacking.




  • VALIDATION OF THE FIELDS ENTERED:

  • For example in the above code name was the mandatory field but to check the accuracy of the information we have to validate the information entered.

  • Suppose we want only capital letters and whitespace in the name.



  • NOTE: preg_match() is an inbuilt function which searches for the pattern specified and returns true if the pattern is found else false.

  • Thus we can validate any field by specifying the pattern which is found in the specific field and checking it via preg_match.

  • The error generated is again stored in a variable and is displayed using the span class.

  • If you want the text entered in the fields to remain even after you have submitted the file, you need to add a little extra of php codes in it.All we need to do is put the scrip between the tags

  • E.g.:


  • This will retain the values that u insert in the fields.






  • CACKLE comment system





    Programming Resources
    Computer Networking Fundamentals Android Application