by Klaus Graefensteiner
27. February 2010 08:27
Improvements
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">
<html>
<head>
<?php
$number = '';
if (isset($_GET['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>
</head>
<body>
<form method="GET" action="">
<label>Enter a number :</label>
<input type="text" name="number"></input>
<br />
<input type="submit" value="Submit">
</form>
<?php echo $number; ?>
</body>
</html>
Download
The script can be downloaded here: BetterSelfSubmittingPHPForm.zip
Ausblick
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.
9f5fecb3-8ee9-4e7c-b207-6f9c34b7d78e|0|.0
Tags: php, web, form
Php