keeping form fields set

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

keeping form fields set

Post by 5leander5 »

How can I ensue that if a user does not fill in a certain form field, that when I redirect the user back to the form, that the fields which he had filled in originally are still filled in? I presume that you might have to send the variable back to the form????

For example,

$email = $_POST['Email'];
if (empty($email)) header( "Location: http://www.bertsparadise.com/form.htm" );

If any other fields were filled out by the user, that data would be lost by reloading the form.htm.

Any suggestions please????
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

okay, first rename form.htm into form.php

and change all your:

Code: Select all

<input type="text" name="NAME">
to:

Code: Select all

<input type="text" name="NAME" value="<?php echo $_GET["NAME"]; ?>">
(change NAME into the form element name)

That'll get the form text input boxes done. for option/select you will need to build your own function to select the correct item.

Then just mod your:

Code: Select all

header("Location: form.htm");
to be:

Code: Select all

header("Location: form.php?".$_SERVER['query_string'])[/p]

Correct me if im wrong, but that should work.
This is no use for one page php scripts, your form and program have to be seperate.

(DONT FLAME ME FOR POSTING THIS WAAAY AFTER AN ANSWER HAS PROBERLY BEEN GIVEN, i typed this before i left, and came back an hour later and forgot i had'nt posted it)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

Well, with forms handled by PHP I always have them on the same script.

Have if(isset($_POST['submit'])) at the top, if they submitted, validate the fields and what not.

Then in the HTML form field just put value="<?php if(isset($_POST['inputname'])) echo $_POST['inputname']; ?>".

There's loads of other ways to do it, that's just the way I do it.

By the way, it's referred to as a 'sticky form'.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

Don't forget to escape the data:

Code: Select all

<?php
if (isset($_POST['submit'])) {
// process stuff

}
<div><label for="email">Email:  (*)</label>
 <input type="text" class="text" name="email" id="email" size="25" 
 value="<?php echo (isset($_POST['email'])) ? htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8') : ""; ?>" />
</div>
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

What's the point in that?

The ternary operator is misused in a sense, you haven't escaped any data and if the user is entering some non-alphanumeric characters it's going to change them, forcing the user to change them back? Unless htmlentities() does sometihng I'm not aware of...?

I would presume he/she would escape the data where I previously said validate and what not.
Last edited by jayshields on Thu Mar 09, 2006 1:54 pm, edited 1 time in total.
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

The only problem i have found with checking:

Code: Select all

<input type="submit" name="submit" value="Submit!" />
with:

Code: Select all

if($_GET/POST["submit"]){
Is that if the user press enter on a form element, instead of clicking the button.
I have experianced where the form doesnt send the 'submit' data.

It might sound a bit wierd... but i've had it happen WAAAY to many times.
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Post by matthijs »

It's just something I'm used to: always escape any output. In this case output to html.. Of course when you're receiving the data you should do input validation.

R4000, that's an interesting point. I've not yet had any problems, but i'll certainly check it out.
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

This is the start of my php script:
<?
session_start() ;

foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;
}

In my form, I have the following test

<input name="Name" type="text" id="<?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>" tabindex="2" size="29">

However, it is not showing a name if I enter it in the form initially.

Can someone see where I'm going wrong here?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

you're putting it in the id attribute? why not the value attribute?
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

When I use value (instead of id) the actual text <?php if(isset($_SESSION['Name'])){
echo $_SESSION['Name']; }?>"
appears in the text field on the screen.

Any ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

isn't that what you wanted?
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

Take a quick look and you'll see what I mean:

http://www.bertsparadise.com/ExpressionOfInterest3.htm
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

you want it in the value...
i'm guessing that you want it to remember the users name if they have to go back.
if so, you want it in the value.


and you should make your page a .php not .htm then it might work :)
5leander5
Forum Commoner
Posts: 31
Joined: Thu Mar 09, 2006 3:45 am

Post by 5leander5 »

I changed it to a .php file and it clears up that anyway. At least the syntax is now recognised.

However, the field is now blank even if I fill it out the first time. Can you see if this is correct in the php script:

<?
session_start() ;

foreach($_POST as $key => $val)
{
$_SESSION[$key] = $val;

Will this set the $_SESSION['Name'] variable if there is a value in that variable
User avatar
R4000
Forum Contributor
Posts: 168
Joined: Wed Mar 08, 2006 12:50 pm
Location: Cambridge, United Kingdom

Post by R4000 »

it should.
but your code is the same as:

Code: Select all

$_SESSION = $_POST;
and you sure your using _POST not _GET?
Post Reply