Creating a simple, 1-page PHP mail, contact, or feedback form
With some clever coding, you can bypass the need to submit form data from one page to another and instead manage an entire PHP form application in one file. By posting form data to the same page, complex form maintenance becomes very simple.
Example of standard 2-page PHP form application
It is very common to see a 2-page setup for PHP (and other server-side language) form data submissions. Here is an example that uses 2 files—form.php and process-form-data.php.
Page 1 of 2-page PHP form
<?php
// This is form.php
?>
<html>
<head>Simple PHP Form</head>
<body>
<form method="post" action="process-form-data.php">
Enter your favorite color: <input type="text" name="txtColor" /> <br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Page 2 of 2-page form using PHP
<?php
// This is process-form-data.php
if ($_POST) {
echo "The form was submitted. Your favorite color is " . $_POST[‘txtColor’];
}
else {
echo "The form was not submitted. Click <a href=\"form.php\">here</a> to try again.";
}
?>
Some things of note for beginners. First, the “if $_POST()” statement checks to see if there was any form data posted. This code is the foundation of just about any PHP form application. Within this check you can do form validation, send emails, or insert data into a database, among other actions. Secondly, you’ll notice that all form input values are accessible by referencing the array of posted values. Since we had a textbox called “txtColor,” we can reference its posted value in PHP with $_POST[‘txtColor’].
This 2-page method works perfectly fine, but why not shorten it into one file? Doing so creates a large amount of flexibility. You can print customer error messages directly in your form, change the formatting of text/tables/inputs to show where data was not completed, etc. and best of all, it’s simply easier to maintain one file.
Post data to the same page using PHP
There are actually 2 ways to post form data to the same page in PHP. First, you can change the “action” parameter in your form tag to the name of the current file. It will literally post the data to this URL, which is the same file.
<?php
// This is form.php
?>
<html>
<head>Simple PHP Form With Self Submit</head>
<body>
<form method="post" action="form.php">
Enter your favorite color: <input type="text" name="txtColor" /> <br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
The second, and better way, is to make use of PHP’s server variables:
<?php
// This is form.php
?>
<html>
<head>Simple PHP Form With Self Submit Using PHP Server Variables</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
Enter your favorite color: <input type="text" name="txtColor" /> <br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Both these methods work, but the second is slightly better because you won’t need to worry about updating the action parameter if the file name or path ever changes.
Putting it all together: a self-submitting PHP form
<?php
// This is form.php
if ($_POST) {
echo 'The form was submitted. Your favorite color is ' . $_POST[‘txtColor’];
}
else {
echo 'The form was not submitted. Click <a href="'. SERVER['PHP_SELF']).'">here</a> to try again.';
}
?>
<html>
<head>Simple 1-Page PHP Form with Processing</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']); ?>">
Enter your favorite color: <input type="text" name="txtColor" /> <br/>
<input type="submit" value="Submit" />
</form>
</body>
</html>
Expanding this into more complex PHP applications
The above code is the perfect foundation for email, feedback, guestbook, and other PHP form applications. Once you have this working, you’ll want to investigate PHP form validation, sending email using PHP, and preventing form SPAM using PHP.
Article Comments
Post a Comment
You must be logged in to post a comment.