Page not remembering the forms contents?

is there any way, that when a form is incorrectly completed and you click send, the next page says that some of the forms questions haven’t been answered properly, and then you click the back button to go back to the form, is there any way that the parts that you did complete, would still be in the form ? This is in formmail.cgi Is there any way of doing this, without anything to do with PHP please ?

It all depends on which browser is being used. Some keep the form content and redisplay it when you go back and some don’t.

Thanks Stephen, is there an easy way to do it, without any php, where it would, in any browser, remember the details please ?

Not if they press the back button to go back since then their browser has full control and if the browser doesn’t retain what they typed then there is nothing you can do to put it back into the form.

It is quite easy to do with PHP (and you don’t need to know much about PHP, either). Is there any reason you don’t want to use PHP?

Thanks Ralph and Stephen. I have

<?php
session_start();

at the very top of the page for something else, and guess there might be a problem trying to get another piece of php code at the very top of the page for the form completion ?

No, there shouldn’t be any problem at all. You just close off one PHP instruction and start another. E.g.

<?php
one set of instructions
?>

<?php
another set of instructions
?>

Aaaah, thanks Ralph, I just thought that the php code for the form completion, had to go at the very top of the page ? ? If not, then, yep, you’ve convinced me :slight_smile: What would be an example of a complete php code, for a form completion, for a first name and email address please ?

There can be other PHP code before it. From memory, I don’t think you’re allowed any whitespace between tags, so no whitespace before the first <?php, and no gaps between the PHP entries. E.g.

<?php
one set of instructions
?>
<?php
another set of instructions
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html lang=“en”>

What would be an example of a complete php code, for a form completion, for a first name and email address please ?

There are plenty of form-to-email scripts available online, such as FormToEmail.com. I have a few of my own that include form validation to filter out spam. Perhaps PM me though, as there are lots of behaviors available.

(PS I’m a PHP noob, so you could also head over to the PHP forum. :wink: )

Re the original question
The only way to do this would be to use AJAX to detect that the user has returned to the previous page, and then re-populate the form fields based on what was submitted. This can not be guaranteed to work every time, though, as not everyone will have Javascript enabled. I’m not an AJAX programmer, so I couldn’t tell you how to do it, but it should be possible (albeit complicated).

A (in my oppinion better) solution, which will work in every browser, would be to place a button which, when clicked, will go to the form and populate the form fields using PHP.

Re the PHP
There is no reason to close the PHP block and start another, if you have no litteral HTML in between the two sets of instructions. There is no output difference between these two examples:

<?php
$array = array(1, 2, 3, 4, 5);

foreach($array as &$value) {
 $value++;
}
?>
<?php
foreach($array as $key => $value) {
 printf("The value of item %s is %s<br>", $key, $value);
}
?>
<?php
$array = array(1, 2, 3, 4, 5);

foreach($array as &$value) {
 $value++;
}

foreach($array as $key => $value) {
 printf("The value of item %s is %s<br>", $key, $value);
}
?>

You can even do this:

<?php
$array = array(1, 2, 3, 4, 5);

foreach($array as $key => $value) {
?>
The value of item <?php $key; ?> is <?php $value++; ?><br>
<?php
}
?>

The last example can’t be recommended, though, as it will be a true pain to debug later.

What type of form fields are we talking about? Normal text boxes are easy.


<input type="text" name="firstName" value="
<?php if(!isset($_POST['firstName']){
echo "Enter your first name";
}else{ echo $_POST['firstName'];}?>"
> // closes the input tag

Use javascript to detect if the form has been completed correctly BEFORE letting the user leave the page.

I can’t remember how I ended up on this page

but it has a simple script to validate form fields.

You could also try googling on form validation javascript. It brings up lots of examples.

Many thanks all - much appreciated. Problems all solved now. :slight_smile: