Capturing Form Data with PHP ($_GET, $_POST, $_REQUEST)

Capturing Form Data with PHP ($_GET, $_POST, $_REQUEST) The form’s action attributes needs to contain the URL of the PHP script that will handle the form.

Example

< form action=”form_handler.php” method=”post” >

To read the data from a form, a user use a few superglobal variables.A superglobal is a built – in PHP variable that is available in any scope: at the top level of the script, within a function, or within a class method. superglobal array, which contains a list of all global variables used in the applications

There are three new superglobal arrays:

    • $_GET – It contains a list of all the field names and values sent by a form using the get method.

 

    • $_POST – It contains a list of all the field names and values sent by a form using the post method.

 

  • $_REQUEST – It contains the values of both the $_GET and $_POST arrays combined, along with the values of the $_COOKIE superglobal array.

Each of these three superglobal arrays contains the field names from the sent form as array keys, with the field values themselves as array values.

A user created a form using the get method,and that form contained the following control:

< input type=”text ” name=”emailAddress” value=”” / >
then a user can access the value that has been entered into the form using either the $_GET or the $_REQUEST superglobal:

$email = $_GET[“emailAddress”];
$email = $_REQUEST[“emailAddress”];

Example

Here, we have created a simple user registration form and then write a form handler script that reads the field values send from the form and display it.

So create the registration form and Save it with the following name registration.html in the document root folder:

Code

<!DOCTYPE html >
<html>
<head>
<title>Membership Form for eBIZ</title> <link rel=”stylesheet” type=”text/ css” href=”common.css” />
</head>
<body>
<h1>Membership Form for eBIZ</h1>
<p>Thanks for joining eBIZ. To register, please fill the details and Send it.</p>
<form action=”process_registration.php” method=”post”>
<div style=”width: 30em;”>
<label for=”firstName”>First name</label>
<input type=”text” name=”firstName” id=”firstName” value=”” />
<label for=”lastName”>Last name</label>
<input type=”text” name=”lastName” id=”lastName” value=”” />
<label for=”password1”>Choose a password</label>
<input type=”password” name=”password1” id=”password1” value=”” />
<label for=”password2”>Retype password</label>
<input type=”password” name=”password2” id=”password2” value=”” />
<label for=”genderMale”>Are you male…</label>
<input type=”radio” name=”gender” id=”genderMale” value=”M” />
<label for=”genderFemale”>…or female?</label>
<input type=”radio” name=”gender” id=”genderFemale” value=”F” />
<label for=”Courses”>What course you have selected?</label>
<select name=”Courses” id=”Courses” size=”1”>
<option value=”JAVA”>JAVA</option>
<option value=”ASP.NET”>ASP.NET</option>
<option value=”PHP”>PHP</option>
</select>
<label for=”comments”>Any comments?</label>
<textarea name=”comments” id=”comments” rows=”4” cols=”50”> </textarea>
<div style=”clear: both;”>
<input type=”submit” name=”submitButton” id=”submitButton” value=”Submit Details” />
<input type=”reset” name=”resetButton” id=”resetButton” value=”Reset Form” style=”margin-right: 20px;” />
</div>
</div>
</form>
</body>
</html>

Next, save the script as process_registration.php in the document root then open the registration.html URL in the Web browser. Fill in the fields in the form, then click Submit button. If all goes well, you should see a page displaying the data that you just entered.

<!DOCTYPE html>
<html>
<head>
<title>Thank You for joining Us</title>
<link rel=”stylesheet” type=”text/css” href=”common.css” />
</head>
<body>
<h1>Thank You for joining Us </h1>
<p>Thank you for registering. Here is the information you submitted:</p>
<dl>
<dt>First name</dt>
<dd><?php echo $_POST[“firstName”]?></dd>
<dt>Last name</dt>
<dd><?php echo $_POST[“lastName”]?></dd>
<dt>Password</dt>
<dd><?php echo $_POST[“password1”]?></dd>
<dt>Retyped password</dt>
<dd><?php echo $_POST[“password2”]?></dd>
<dt>Gender</dt>
<dd><?php echo $_POST[“gender”]?></dd>
<dt>Favorite Course</dt>
<dd><?php echo $_POST[“favoriteCourse”]?></dd>
<dt>Comments</dt>
<dd><?php echo $_POST[“comments”]?></dd>
</dl>
</body>
</html>