A better simple self-submitting PHP form

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.

Simple Self-Submitting Form - Mozilla Firefox

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.

Tags: , ,

Php

About Klaus Graefensteiner

I like the programming of machines.

Add to Google Reader or Homepage

LinkedIn FacebookTwitter View Klaus Graefensteiner's profile on Technorati
Klaus Graefensteiner

Klaus Graefensteiner
works as Developer In Test and is founder of the PowerShell Unit Testing Framework PSUnit. More...

Open Source Projects

PSUnit is a Unit Testing framwork for PowerShell. It is designed for simplicity and hosted by Codeplex.
BlogShell is The tool for lazy developers who like to automate the composition of blog content during the writing of a blog post. It is hosted by CodePlex.

Administration

About

Powered by:
BlogEngine.Net
Version: 1.6.1.0

License:
Creative Commons License

Copyright:
© Copyright 2013, Klaus Graefensteiner.

Disclaimer:
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

Theme design:
This blog theme was designed and is copyrighted 2013 by Klaus Graefensteiner

Rendertime:
Page rendered at 5/21/2013 4:24:17 AM (PST Pacific Standard Time UTC DST -7)