pTutorial

New way to learn PHP Example

PHP Form Data

How to access form data ?

You can access form data by using different super global array like $_GET, $_POST and $_REQUEST, but in this case $_POST is used for access the form data.

Create form using html 5

//Save this file reg_form.php

<html>
<body>
<h1>Access form data</h1>
<p>Please fill the form below</p>
<form action="33.php" method="post">
<table bgcolor="#F7F7F7">

<tr>
  <td>First name:</td>
  <td><input type="text" name="fn" id="fn1" value="" required /></td>
</tr>
<tr>
  <td>Last name:</td>
  <td><input type="text" name="ak" id="ak" value="" /></td>
</tr>
 <tr>
    <td>Email:</td>
    <td><input type="email" name="mail1" id="mail1" value="" /></td>
  </tr>
  <tr>
    <td>Password:</td>
    <td><input type="password" name="pass1" id="pass1" value="" /></td>
  </tr>
  <tr>
    <td>Gender:</td>
    <td>Male<input type="radio" name="g" id="gM" value="M" /> 
    Female<input type="radio" name="g" id="gF" value="F" /></td>
  </tr>
  <tr>
    <td>Country</td>
    <td><select name="country" id="country" size="1">
<option value="India">  India     </option>
<option value="USA">USA</option>
<option value="Brazil">Pakistan</option>
</select>
</td>
  </tr>
  <tr>
    <td>Newsletter</td>
    <td><input type="checkbox" name="new" id="new" value="yes" /></td>
  </tr>
  <tr>
    <td>comments</td>
    <td><textarea name="comments" id="comments" rows="4"
cols="22 "> </textarea></td>
  </tr>
  <tr>
    <td><input type="submit" name="sub" id="sub"value="Send" /></td>
    <td><input type="reset" name="reset1" id="reset1"
value="Reset" /></td>
  </tr>
  
</table>
</form>

</body>
</html>

PHP and HTML code for access the form data

//Save this code 33.php
<html>
<head>
<title>Thank You</title>
</head>
<body>
<table width="300">
  <tr>
    <td><b>Field</b></td>
    <td><b>Value</b></td>
  </tr>
  <tr>
    <td>First name:</td>
    <td><?php echo $_POST["fn"]?></td>
  </tr>
  <tr>
    <td>Last name</td>
    <td><?php echo $_POST["ak"] ?></td>
  </tr>
  <tr>
    <td>Email</td>
    <td><?php echo $_POST["mail1"]?></td>
  </tr>
  <tr>
    <td>Password</td>
    <td><?php echo $_POST["pass1"]?></td>
  </tr>
  <tr>
    <td>Gender</td>
    <td><?php echo $_POST["g"]?></td>
  </tr>
  <tr>
    <td>Countary</td>
    <td><?php echo $_POST["country"]?></td>
  </tr>
  <tr>
    <td>Newsletter?</td>
    <td><?php echo$_POST["new"]?></td>
  </tr>
  <tr>
    <td>Comments</td>
    <td><?php echo $_POST["comments"]?></td>
  </tr>
 
</table>



</body>
</html>

Output

access form data

At the time of form creation we are using Post method and action = '33.php'.

In this Form there are seven or Eight field all the have unique name that is used at the time of accessing the data of form

Both 33.php and reh-form.php are used table for formatting the data in proper manner.

Note : All the form field must have unique name or unique ID.


QUOTES OF THE DAY

To make your children capable of honesty is beginning of education.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER