/    Sign up×
Community /Pin to ProfileBookmark

Retain form field values issue :(

Hello,

I have the following problem. I edit and add new records to database through the same form. When adding new record and if there is an error on form, notice is displayed and already entered values are retained.

Now when i edit the record (action=edit), data is loaded from database, in this example ($data_group_id). But now, if user changes many values and there are errors in form, already entered values are again replaced with values from database. Here is the example code of one of the form fields:

<input type=’text’ name=’data_group_id’ value='”.($action == “edit” ? $data_group_id : $_POST[‘data_group_id’]).”‘>

I hope the explaination wasn’t too complicated.

Thank you,
Laura

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@xvszeroOct 01.2008 — Hmm.

I'm not exactly sure how your form is set up, but what I do for this is before I even display the form I have code that takes the values and immediately put them into session variables. Then the value for the form fields are provided by these session variables. This way when the user submits, code can update the session variables with the new information before trying to update the database. If there is an error and they have to go back and change anything the values for the form fields are using the new session data.

Of course, this brings up the question of how to make sure your code isn't overwriting the session variables every time you go back to that page, since it creates them that first time. My stuff is set up so that when you choose a report to edit, it sends the report id. So you have a variable that grabs that id... if that variable has something in it, it means you came from the report selection page, and you can create the session variables. If it is NULL, that means it came from another page (I have a "back" link) which means the session variables were already created (and have the most current data,) so you don't do anything to them.

Maybe not the best way, but it works for me.
Copy linkTweet thisAlerts:
@laura81authorOct 01.2008 — Thank you for quick answer. I will try to develop something from that.

Maybe someone has some other solution?

Laura
Copy linkTweet thisAlerts:
@ariellOct 01.2008 — Hi Laura,

as xvs, I, too, do not know the exact setup of your form. However, I am sure you have a certain way to retrieve your data from db. Doing it the "first" time at least ensures, that there's no $_POST stuff set so far.

Having said that, a simple destinction between data from db and data from the form might serve he purpose. Given you have firstName and lastName, you might introduce your script like this:

[code=php]
if (! ($firstName = @$_POST['firstName']))
$firstName = '';
if (! ($lastName = @$_POST['lastName']))
$lastName = '';
[/code]


Assuming, you name the form fields appropriately.

Then, in your form, you populate the form's values with the php vars, e.g.:

[code=php]
echo('
<input type="text" name="firstName" value="'.$firstName.'" />
');
[/code]


Now, when your "check-up" encounters an error, you have the most recent values in your form.

As for the usage of session-vars: This is certainly working, on the other hand, one must be careful. Storing TEMP values is definitely not what $_SESSION stuff is supposed to do, and depending on the size of an project, this technique lease easily to an "expensive overhead". I'd rather suggest to not going like this.

Best.

P.S. Do not forget the silecing operator "@", cos otherwise you catch a PHP warning in case a POST var is not (yet) existing...
Copy linkTweet thisAlerts:
@laura81authorOct 02.2008 — Thank you guys, It is now working as it should.

I used a little modified Ariell's approach.

Laura?
×

Success!

Help @laura81 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...