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

Comments

2/28/2010 7:33:52 AM #

Rob

"array_key_exists() is slower and checks if the array key has the value null."

Did you actually test this?

1. Yes, array_key_exists() is slower, but micro-optimisations are the work of the devil and people writing "100 ways to make your PHP run faster" linkbait nonsense.

2. In PHP, array keys can't have the value NULL. Values can be NULL, but keys can't.

3. array_key_exists() <i>doesn't</i> check whether the value is NULL, but isset() does. Like you would expect from their respective names or, indeed, the PHP docs you linked to.

Also: The comments form doesn't work in Firefox.

Rob United Kingdom |

2/28/2010 8:16:33 AM #

Rob

<i>The comments form doesn't work in Firefox.</i>

Just found I had javascript turned off. This shouldn't break a simple blog comment form, but it does seem to.

Rob United Kingdom |

2/28/2010 9:59:35 AM #

m

I've had lots of bugs caused by using isset instead of array_key_exists when null is passed in.  I'm stopped using isset, it doesn't line up with most people's expectations.

m United States |

2/28/2010 1:59:56 PM #

SeanJA

I prefer isset to array_key_exists. array_key_exists is far too long to type, and most of the time I don't care if something is null. If it is a checkbox, there should be a default value. If it is a radio button... same thing. Pretty much every other field should not be null, or it can be null.

You should probably be putting an action in there... $_SERVER['PHP_SELF'] if there are not request variables that you want to pass in the action, or $_SERVER['REQUEST_URI'] if you want the stuff after the ? to be submitted too (in the case of a user page).

SeanJA Canada |

2/28/2010 2:00:34 PM #

SeanJA

Sorry... that second part was not directed at @m...

SeanJA Canada |

Comments are closed

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 2012, 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 2012 by Klaus Graefensteiner

Rendertime:
Page rendered at 2/5/2012 10:57:54 PM (PST Pacific Standard Time UTC DST -7)