remember form values

General discussions related to php

Moderators: macek, egami, gesf

milily
New php-forum User
New php-forum User
Posts: 8
Joined: Sun Jun 29, 2003 12:39 pm
Location: Michigan, U.S.

remember form values

Postby milily » Thu Sep 04, 2003 12:57 pm

Hi,

I have several fields in a form. If the user leaves some of the required fields empty. I will forward the user back to the form and let them fill those fields. I want to remember the values the user already input. So they don't need to type it again. I don't want to use cookies or session variables. So what I did is:

<input type="text" name="university" value="<?php echo ((empty($_POST['university'])) ? "" : ($_POST['university']) ) ?>">

and

<input type="text" name="university" value="<?php echo ($_POST['university'] ? $_POST['university'] : "" ) ?>">

But both of them don't work. Did I miss anything? Could anyone tell me how to do this? Thank you.

Helen

User avatar
tryton
New php-forum User
New php-forum User
Posts: 49
Joined: Wed Jul 09, 2003 8:25 am
Location: Joze
Contact:

Postby tryton » Thu Sep 04, 2003 3:05 pm

Try this:

Code: Select all

// This should be at the beginning of the page
<? $email=$_REQUEST['emailAdd'];?>
...

// And when you re-display your form lower in the page,
// enter this
<input name="emailAdd" type="text" id="emailAdd" size="40" value="<? echo $email; ?>">

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Postby Joel » Thu Sep 04, 2003 11:22 pm

What tryton has said will do the same thing, except
a) It will remedy if the problem is with the form method
b) An unneccesary variable is created.

The only problem I can think of is with the forwarding back, does this mean you use the header function for a new location?

Try this:

Code: Select all

<input type="text" name="university" value="<?php echo @$_POST['university']; ?>">


This will post any contents of the value and suppress the error if it is not set. I think this maybe cheating a little, but its the simplest way of doing it.

User avatar
ruturajv
php-forum Super User
php-forum Super User
Posts: 1280
Joined: Sat Mar 22, 2003 9:42 am
Location: Mumbai, India
Contact:

session

Postby ruturajv » Thu Sep 04, 2003 11:40 pm

Why not use sessions to keep all the information ..?

sigix
php-forum Active User
php-forum Active User
Posts: 364
Joined: Mon Jul 14, 2003 9:39 pm
Location: /Earth/Xion

Postby sigix » Fri Sep 05, 2003 4:54 am

why don't you use javascript for the client side checks
so use don't leave any fields empty :idea:

milily
New php-forum User
New php-forum User
Posts: 8
Joined: Sun Jun 29, 2003 12:39 pm
Location: Michigan, U.S.

Postby milily » Fri Sep 05, 2003 10:35 am

Thank you all for your replies.

The reaseon I don't use session variable is I have too many fields and I don't want to have so many session variables. It will be confusing sometimes. Probably I will use javascript if I cannot find the solution from php.

I tried Tryton's method and tried and Joel's method. Neither of them work in my program.

Yes, I use header to reset the location. Is this a problem? If this is not good, what should I use instead of header? In my code, one file is the form html part, another file is the action php part.

Thank you.

Helen

User avatar
tryton
New php-forum User
New php-forum User
Posts: 49
Joined: Wed Jul 09, 2003 8:25 am
Location: Joze
Contact:

Postby tryton » Fri Sep 05, 2003 2:56 pm

If you can, show us all the code.

Joel
New php-forum User
New php-forum User
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand
Contact:

Postby Joel » Fri Sep 05, 2003 5:59 pm

I think maybe the the header redirection loses the POST variables. I am not too sure of this though, you could easily test

The way I generally do my errors on forms is this...

display page with form included
send form to php self and if no error from validator then process
if there is errors - display errors and include form again

The form has

Code: Select all

value="<?php echo @$_POST['var']; ?>"
[/code]


Return to “PHP General”

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest