PHP: A two-step process to display the form

An alternative to the earlier post where we do not have to suppress the warning message is to design the form in a 2-step process.

The first step is to display the form.  The second step is to process the form.

The pattern here is, if form data has been submitted, process it; otherwise, print out a form, is common in PHP programs.

The form makes use of heredoc as explained in this post.

<?php

if (array_key_exists('my_name', $_POST)) {
    print "Hello, " . $_POST['my_name'];
} else {
    echo <<< html_output
<form method="post" action="$_SERVER[PHP_SELF]">
 Your name: <input type="text" name="my_name">
<br/>
<input type="submit" value="Say Hello">
</form>
html_output;
}

?>

Series Navigation<< PHP: A simple form using $_POST
PHP: Form processing with functions >>