Home

How to Use PHP to Build a Program that Makes its Own HTML5 Form

|
Updated:  
2016-03-26 13:13:50
|
HTML5 and CSS3 All-in-One For Dummies
Explore Book
Buy On Amazon

An especially important application of the if structure is unique to PHP server-side programming. Up to now, many of your PHP programs required two separate files: an HTML page to get information from the user and a PHP program to respond to that code.

Wouldn't it be great if the PHP program could determine whether it had the data or not? If it has data, it will process it. If not, it just produces a form to handle the data. That would be pretty awesome, and that's exactly what you can do with the help of the if statement.

image0.jpg

The interesting thing happens when the user submits the form. The program calls itself! This time, though, ownForm recognizes that the user has sent some data and processes that information.

image1.jpg

This program doesn’t really require anything new, just a repurposing of some tools you already know. Take a look at the following code:

<!doctype html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>ownForm.php</title>
</head>
<body>
<?php
if (filter_has_var(INPUT_POST, "userName")){
 //the form exists - process it
 $userName = filter_input(INPUT_POST, "userName");
 print "<h1>Hi, $userName</h1>n";
} else {
 //no form present, so give 'em one
 print <<<HERE
 <form action = "
  method = "post">
 <fieldset>
  <label>Name</label>
  <input type = "text"
    name = "userName">
  <button type = "submit">
  submit
  </button>
 </fieldset>
 </form>
HERE;
} // end if
?>
</body>
</html>

Making a program “do its own stunts” like this is pretty easy. The key is using an if statement. However, begin by thinking about the behavior. In this example, the program revolves around the $userName variable. If this variable has a value, it can be processed. If the variable has not been set yet, the user needs to see a form so she can enter the data.

  1. Check for the existence of a key variable.

    Use the isset() function to determine whether the variable in question has been set. Check the $_REQUEST or one of the other superglobals ($_POST or $_GET) to determine whether the form has already been submitted. You need to check the existence of only one variable, even if the form has dozens.

  2. If the variable exists, process the form.

    If the variable exists, extract all the variables from the form and carry on with your processing.

  3. If the variable does not exist, build the form.

    If the variable does not exist, you need to make the form that will ask the user for that variable (and any others you need). Note that the action attribute of the form element should be null (“”). This tells the server to re-call the same program.

If you're using an HTML5 validator, it will complain about the empty action attribute. This is interesting because previous HTML and XHTML implementations required it in this situation. In this particular situation (a PHP program creating a form that will call the PHP program again), many web developers just live with the validator's complaints because the empty attribute explicitly defines what you want to do and it does no harm.

About This Article

This article is from the book: 

About the book author:

Andy Harris earned a degree in Special Education from Indiana University/Purdue University–Indianapolis (IUPUI). He taught young adults with severe disabilities for several years. He also taught himself enough computer programming to support his teaching habit with freelance programming.
Those were the exciting days when computers started to have hard drives, and some computers connected to each other with arcane protocols. He taught programming in those days because it was fun.
Eventually, Andy decided to teach computer science full time, and he still teaches at IUPUI. He lectures in the applied computing program and runs the streaming media lab. He also teaches classes in whatever programming language is in demand at the time. He has developed a large number of online video-based courses and international distance education projects.
Andy has written several books on various computing topics and languages including Java, C#, mobile computing, JavaScript, and PHP/MySQL.
Andy welcomes comments and suggestions about his books. He can be reached at [email protected].