Web forms and PHP

From Wiki @ Karl Jones dot com
Revision as of 13:47, 14 September 2016 by Karl Jones (Talk | contribs) (See also)

Jump to: navigation, search

This article discusses web forms and PHP.

Example

The examples below are based on this page @ php.net.

Create an HTML page (call it formpage.html, for convenience) containing this web form:

<form action="action.php" method="post">
 <p>Your name: <input type="text" name="name" /></p>
 <p>Your age: <input type="text" name="age" /></p>
 <p><input type="submit" /></p>
</form>

Note that the form's action attribute is set to action.php. When the user submits the form, the form data will be sent to a PHP page named action.php (in the same folder as the web form page). The file name action.php is arbitrary -- any valid file name will work.

The action.php page contains the following:

Hi <?php echo htmlspecialchars($_POST['name']); ?>.
You are <?php echo (int)$_POST['age']; ?> years old.

When the user fills out and the submits the web form, the action.php page will display something like this:

Hi Joe. You are 22 years old.

Note that both formpage.html and action.php should contain complete and valid HTML. The above examples show only excerpts, not the complete HTML.

See also

External links