PHP Full Form

In this post I would explain how to process a form using PHP. Before we move to PHP code, lets see how our HTML code looks like which creates the form. I have included all possible basic controls (text, checkbox, radio, option list, etc) to demonstrate how fetching values for different type of controls differ when we write PHP code.



    Form with all controls
    

Student Registration Form

Algorithms OOP Data Structures Visa Master Card American Express

If you know HTML, you know there is nothing special in above file. Its just plain HTML. Notice, action value is full-form-handler.php. It means, when the user submit this form, full-form-handler.php would be executed to process the form data.
In our full-form-handler.php, we just want to retrieve each submitted value and show it on the next page. Lets see, how the php file looks like:

$name = $_REQUEST['name'];
$password = $_REQUEST['password'];
$credit_card = $_REQUEST['credit_card'];
$city = $_REQUEST['city'];
$languages = $_REQUEST['languages']; // Would return an array of all selected option items
 
// For checkboxes, only checked values would be submitted, so lets see what was checked
// isset() function must return not null, for parameters that are submitted
$subjects = '';
if(isset($_REQUEST['Algorithms'])){
$subjects = $subjects . ' Algorithms, ';
} if(isset($_REQUEST['OOP'])){
$subjects = $subjects . ' OOP, ';
} if(isset($_REQUEST['DataStructures'])){
$subjects = $subjects . ' Data Structures';
}

echo "Name : $name ";
echo "passwrod : $password ";
echo "Subjects: $subjects ";
echo "Credit Card : $credit_card ";
echo "City : $city ";
echo "Languages : ";

foreach($languages as $l) 
  echo $l . ',';

There are following points, you must understand about above code snippet:
  • Input type text and password are handled just normally. $_REQUEST['name'] return the string value.
  • In case of check boxes, only those check boxes values are submitted, which were selected/checked by user. Other check boxes are not submited. So we must have to check, which check boxes were received by server to know what user checked.
  • By defult, 'on' value is submitted by the browser for the checked checkbox. But if you want to receive a particular value, you must use the value attribute of checbox when you write HTML. For know, lets just proceed with default valu i.e. 'on'
  • About the radio button, you see we given same name to multiple radio buttons, why? because by doing so, we declare that only one radio button should be active at a time. For example, user credit card would belong to a single company. From same name, the web browser automatically deselect the previous crdit card of we choose another one.
  • We are only left with languages list. We know, we have allowed user to select multiple values. So PHP automatically create an array of all selected and submitted values and return us just that array. We just need to iterate that array to retrieve those values

Lets see, how our form looks like:


Here is the output generated by above PHP code:

Comments

  1. This technical post helps me to improve my skills set, thanks for this wonder article I expect your upcoming blog, so keep sharing..
    Regards,
    PHP Course Chennai|PHP Training

    ReplyDelete
  2. Nice post.This post helps me alot in preparing a form using PHP.Thanks for this excellent post.
    Keep writing blog posts.keep sharing.
    have a look on ,custom software development company,
    Yii2 development
    WP development
    Ionic Development
    NodeJS development
    Angular Development

    ReplyDelete
  3. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    Webdesining course in chennai

    ReplyDelete

Post a Comment