Simple self submitting PHP forms

by Klaus Graefensteiner 19. February 2010 15:35

This is the first real thing I struggled with while learning PHP. I couldn’t make the form post to itself. Finally I managed to get it right. Here are two scripts that take a number as a string and post it back to the page. The examples that I searched and found on the web weren’t this helpful. So I decided to post my own scripts.

image

Self submitting form with GET

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php
	$number= $_GET['number'];
?>
<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 
	if(count($_GET) > 0 && array_key_exists('number', $_GET))
	{
		echo $number;
	}
?>
</body>
</html>

Self submitting form with POST

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?php
	$number= $_POST['number'];
?>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Simple Self-Submitting Form</title>
</head>
<body>
<form method="POST" action="">
<label>Enter a number :</label>
<input type="text" name="number"></input>
<br />
<input type="submit" value="Submit">
</form>
<?php 
	if(count($_POST) > 0 && array_key_exists('number', $_POST))
	{
		echo $number;
	}
?>
</body>
</html>

Download

You can download the two scripts here: SelfSubmittingPHPForms.zip

Ausblick

That’s it.

Tags: , , ,

Php

Comments

2/22/2010 6:12:09 PM #

OIS

You could simply ask the user to do a search and replace GET with POST, no need for 2 examples...
You dont have to count GET/POST if you are gonna check for a specific key afterwards.
You should use isset($_POST['number']) instead. array_key_exists is slower and checks if the array key has the value null which you cant print.
You should you do the logic first, then the presentation.
<?php $number = ''; if (isset($_POST['number'])) { $number = filter_input(INPUT_POST, 'number', FILTER_VALIDATE_INT); if ($number === false) { $number = "Not a valid number!\n"; } } ?><html>...<?php echo $number; ?>

OIS Norway |

2/23/2010 1:48:17 AM #

Klaus Graefensteiner

Thanks for your insights OIS.
I will have a look at your suggestions and re-write my php forms.

I just started with PHP and it seems like there is always much more to learn.

Klaus

Klaus Graefensteiner United States |

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:56:50 PM (PST Pacific Standard Time UTC DST -7)