PHP: Form processing with functions

This entry is part 39 of 54 in the series PHP Tutorial

We can put the display and form processing code in 2 different functions.

The show_form function is more about using heredoc to display HTML.


// Logic to do the right thing based on
// the submitted form parameters
if (array_key_exists('my_name', $_POST)) {
} else {

// Do something when the form is submitted
function process_form()
    print 'Hello, ' . $_POST['my_name'];

// Display the form
function show_form()
    print<<< html_output
<form method='POST' action='$_SERVER[PHP_SELF]'>
Your name: <input type='text' name='my_name'>
<input type='submit' value='Say Hello'>

Series Navigation<< PHP: A two-step process to display the form
PHP: Using hidden parameters to test for form submission >>

Leave a comment

Leave a Reply