Newbie - Problem keeping form values

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a form on a webpage, that users use to send me orders (it's a
private page, selling comics) - this form is pretty basic, and I've
encountered a problem.

A couple of the fields are mandatory, and if the user haven't filled
them, he's returned to the form, and told that he needs to fill out the
missing fields. All good.

But, whatever the user typed in earlier, is gone, and I want it to still
be there - it's annoying for the user to have to fill out the entire
form again.

The way I have it now, the form itself sits on one page, then when the
user hits the "Submit" button, a page is displayed, either "takk.php"
(if the mail was sent successfully) or "feil.php" if there was something
wrong. Both of the pages return the user to the page with the form after
a few seconds (with JS) or the user can click a link on those pages to

I want to keep whatever is written in the form if the user has forgotten
something, and return the user to a blank form if the user has done
everything right.

I've tried to look around the web for a solution, but I've only gotten
more confused - can anyone here help me?

I can, if it's easier, ditch the "feil.php" page that shows when there
is an error, and just go directly back to (or refresh)  the page with
the form / errors - then I do need to change the text preceeding the
form, so to describe to the user what went wrong and how to fix it.

Here are som snippets of HTML and PHP code I use today:

The form:
<form method="post" action="bestilling.php">
<p class="clear"><label><span class="star">* </span>Bestilling:</label>
<textarea name="bestilling"
<p class="clear"><label>Navn:</label> <input type="text"
  tabindex="2" /></p>
<p class="clear"><label>Gateadresse:</label> <input type="text"
  tabindex="3" /></p>
<p class="clear"><label>Postnummer:</label> <input type="text"
  tabindex="4" /></p>
<p class="clear"><label>Poststed:</label> <input type="text"
  tabindex="5" /></p>
<p class="clear"><label><span class="star">* </span>E-mail
adresse:</label> <input type="text"
  tabindex="6" /></p>
<p class="button"><label></label> <input type="submit" name="send"
value="Send bestilling" tabindex="7" /></p>

The form is contained in a separate file that's included into the main

The PHP-file that's used when the "Submit"-button is pressed:

 $user = "Navn: " . $_POST["user_name"] . "\n";
 $adrs .= "Mailadresse: " . $_POST["mail_adresse"] . "\n\n";
 $gate .= "Gateadresse: " . $_POST["adresse"] . "\n\n";
 $num .= "Postnummer: " . $_POST["p_nummer"] . "\n";
 $sted .= "Poststed: " . $_POST["p_sted"] . "\n\n";
 $kom .= "" . $_POST["bestilling"] . "\n\n";
 if (empty( $_POST["mail_adresse"])
  || empty($_POST["bestilling"])) {
 header("Location: ");
 } else {
 mail("", "Bestilling",

<?php include 'takk.php' ?>

As said earlier, I can easily loose the "feil.php" if it's easier to
just go back to the form-page, and keep the values.


Re: Newbie - Problem keeping form values

Quoted text here. Click to load it

you could use javascript to check the form.

<form action="xxx" method=POST onSubmit = 'return check_form();'>

all you need now is a javascript function 'check_form' that returns
false in case the fields are not filled in correctly. in that case the
form is not submitted at all.



Re: Newbie - Problem keeping form values

I noticed that Message-ID:
contained the following:

Quoted text here. Click to load it

Which will need to be re-checked in PHP.  You can't rely on JS for

Let's not go here people, this is a PHP group.

The solution, in your case, is to either use sessions to temporarily
store the form data or to write it to hidden fields in your second page.

Personally, I do everything on one page and do something like

<input type="textbox" name="something" value="
<?php if (isset($_POST['something'])){ echo $_POST['something']} ?>

You could use something similar to check for session data or data
retrieved from hidden inputs.

Drop down boxes, checkboxes and radio buttons are a bit more difficult,
but are essentially done in the same way.

Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs /

Re: Newbie - Problem keeping form values


Well, a very simple solution would be to send the user back to the form page
as if he pressed the back button in his browser, that will retain the form
items he filled out.

Like so:

<a href='javascript:window.history.back()'>Please correct your form</a>

Of course, you can have your JavaScript function do it like so:

setTimeout("window.history.back()",5000); // 5 second delay

Otherwise you would have to send data back to the original form page and
fill in the form items. You can send hidden form items from the validation
page back to the original form page, on the original page you would check if
those values are empty or zero for when the user goes there the first time.

See if the back method works for you, if it doesn't and you need more
detailed information (and code) on the other method I somewhat described
just ask.

Take care,

Re: Newbie - Problem keeping form values

I denne meldingen:uA1dd.9994$,
Quoted text here. Click to load it

I'm sure it would work, but I'd like to have a method that works
_without_ being dependent on JS - basically because I'd like my page to
work no matter what kind of browser or setup the user has on his box.

If I were to send data back to the form, I guess I'd have to do it
directly from the "bestilling.php" page - that is, I would have to skip
the "feil.php" page that shows now, when the user has done something
wrong? Or can I take the data from the form, via the "bestilling.php" to
the "feil.php" and back to the original form? (I guess it can be done -
most things can, but I'm assuming that the approach of going right back
from the "bestilling.php" file is a lot simpler).

What I then need, is some form of explanation to the user about what
went wrong...


Re: Newbie - Problem keeping form values

Quoted text here. Click to load it

   May help you

|  Just another PHP saint  |
Email: rrjanbiah-at-Y!com

Site Timeline