This is a discussion on Newbie - Problem keeping form values within the PHP Language forums, part of the PHP Programming Forums category; I have a form on a webpage, that users use to send me orders (it's a private page, selling ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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 return. 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" rows="6" cols="67" tabindex="1"> </textarea></p> <p class="clear"><label>Navn:</label> <input type="text" size="75" maxlength="200" name="user_name" tabindex="2" /></p> <p class="clear"><label>Gateadresse:</label> <input type="text" size="75" maxlength="200" name="adresse" tabindex="3" /></p> <p class="clear"><label>Postnummer:</label> <input type="text" size="4" maxlength="4" name="p_nummer" tabindex="4" /></p> <p class="clear"><label>Poststed:</label> <input type="text" size="24" maxlength="74" name="p_sted" tabindex="5" /></p> <p class="clear"><label><span class="star">* </span>E-mail adresse:</label> <input type="text" size="75" maxlength="200" name="mail_adresse" tabindex="6" /></p> <p class="button"><label></label> <input type="submit" name="send" value="Send bestilling" tabindex="7" /></p> </form> The form is contained in a separate file that's included into the main page. The PHP-file that's used when the "Submit"-button is pressed: <?php $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: http://home.no.net/onl78/feil.php"); exit(); } else { mail("pubblicc@start.no", "Bestilling", "$user$adrs$gate$num$sted$kom"); } ?> <?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. -- ØNL http://www.langbakk.no |
|
|||
|
Hi,
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, Cyrus |
|
|||
|
"Ørjan Langbakk" <news@langbakk.no> wrote in message news:<2tj91jF1v4tagU1@uni-berlin.de>...
> 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 > return. > > 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" > rows="6" > cols="67" > tabindex="1"> > </textarea></p> > <p class="clear"><label>Navn:</label> <input type="text" > size="75" > maxlength="200" > name="user_name" > tabindex="2" /></p> > <p class="clear"><label>Gateadresse:</label> <input type="text" > size="75" > maxlength="200" > name="adresse" > tabindex="3" /></p> > <p class="clear"><label>Postnummer:</label> <input type="text" > size="4" > maxlength="4" > name="p_nummer" > tabindex="4" /></p> > <p class="clear"><label>Poststed:</label> <input type="text" > size="24" > maxlength="74" > name="p_sted" > tabindex="5" /></p> > <p class="clear"><label><span class="star">* </span>E-mail > adresse:</label> <input type="text" > size="75" > maxlength="200" > name="mail_adresse" > tabindex="6" /></p> > <p class="button"><label></label> <input type="submit" name="send" > value="Send bestilling" tabindex="7" /></p> > </form> > > The form is contained in a separate file that's included into the main > page. > > The PHP-file that's used when the "Submit"-button is pressed: > > <?php > $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: http://home.no.net/onl78/feil.php"); > exit(); > } else { > mail("pubblicc@start.no", "Bestilling", > "$user$adrs$gate$num$sted$kom"); > } > ?> > > <?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. 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. micha micha |
|
|||
|
I denne meldingen:uA1dd.9994$YM4.3731882@news4.srv.hcvlny. cv.net,
skrev: Cyrus D. <satan@invalid.org> følgende: > Hi, > > 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. 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... -- ØNL http://www.langbakk.no |
|
|||
|
I noticed that Message-ID:
<782d6cb.0410182213.3f194d4d@posting.google.com> from chotiwallah contained the following: >you could use javascript to check the form. Which will need to be re-checked in PHP. You can't rely on JS for validation. 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 http://www.ckdog.co.uk/rfdmaker/ |
|
|||
|
"Ørjan Langbakk" <news@langbakk.no> wrote in message news:<2tj91jF1v4tagU1@uni-berlin.de>...
> 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. <snip> May help you <http://groups.google.com/groups?selm=abc4d8b8.0402130547.2f09d2af%40posting .google.com> -- | Just another PHP saint | Email: rrjanbiah-at-Y!com |