Making your forms remember their values

One of the things that all of the forms we’ve built so far have been missing is something that virtually every browsergame worth its salt has: inputs that remember their old values.

You can see this in action on virtually any website you visit, whether it’s a browsergame or not. You type some text into a box, hit the ’submit’ button, and then when the new page has loaded, if that input area is still there, it will still have the values that you entered earlier. This is a really nice feature, that’s really easy to implement.

All you need to do is set the value attribute of your inputs to the variable that stores the information sent to your page.

Let’s say, for example, that in your PHP page $_POST[‘username’] has the value that the user typed into the ‘username’ box. To set the value to that, all we’d need to do is put value=’$_POST[‘username’]‘ inside our HTML code that we are outputting, and make sure that it’s in a location where the value of the variable will be interpolated into our output.

If you’re using a templating system, this (usually) becomes even easier – under Smarty for PHP, you can use something like this:

<input type='text' name='username' value='{$smarty.post.username}' />
If you’re using Perl’s HTML::Template, you can use the associate argument in your constructor, with a CGI object:

my $query = new CGI;
my $template = HTML::Template->new(
		associate	=>	$query,
	);

And because the CGI object has a param() method, HTML::Template will just automagically store the values into any area where you’ve defined that they should be displayed, like so:

<input type=’text’ name=’username’ value=”<!–tmpl_var name=’username’–>” />
And that’s all there is to making your forms remember their values – a simple change that will help keep your users from getting frustrated with using any of the forms in your game.