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

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

By Andy Harris

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.