27. February 2010 08:27
A few days ago I published a blog post with examples of self-submitting PHP forms on my blog. I digested the feedback and created a better version of the form.
All the form needs to do, is to print out the number that gets submitted in a text box.
Figure 1: Simple Self-Submitting Form - Mozilla Firefox
Here are the improvements (based on the feedback from “OIS”):
- You don’t have to count GET/POST, if you are going to check for a specific key afterwards.
- You should use isset($_POST['number']) instead of array_key_exists(). array_key_exists() is slower and checks if the array key has the value null.
- You should you do the logic first, then the presentation.
Better PHP form
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
$number = '';
$number = filter_input(INPUT_GET, 'number', FILTER_VALIDATE_INT);
if ($number === false)
$number = "Not a valid number!\n";
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Self-Submitting Form</title>
<form method="GET" action="">
<label>Enter a number :</label>
<input type="text" name="number"></input>
<input type="submit" value="Submit">
<?php echo $number; ?>
The script can be downloaded here: BetterSelfSubmittingPHPForm.zip
I hope this version of the self-submitting web form is better than the last attempt. But I am almost sure that even this script can be improved.