Forum Moderators: coopster

Message Too Old, No Replies

Saving values that checked OK in a form

Avoiding to reenter all fields if error in any field

         

henry0

6:01 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What is the best way to keep in a form the values that checked OK
In order to save those good one so the user do not need to redo it all?

It was not a problem until I needed to deal with a few too many fields!
Will writing to a temp table and feeding
<? echo $biz_name;?> with the temp value do?

While I type I imagine that for each fields where I use!preg_match() a preg_match() could match the input and save it

However the concept although doable sounds heavy

any other suggestion

Not sure that I would like adding a JS piece

henry0

7:53 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I found a way for every fields but UN and PW that I want to keep out of my solution:
Using a browser "life span" session (as long as the browser is not closed)
But I had a problem due to the number of sessions to be written until I found a neat function:
session_write_close(); that allows enough time for all sessions to be passed

Any other ideas

PS)
I do not like too much the idea of passing through session a user's details but I keep out of it the UN and PW
if a session is stolen only non financial and non personal details could be revealed

eelixduppy

7:59 pm on Apr 26, 2006 (gmt 0)



Hello...
I would try something like this:


print "
<form....>
<input type='text' size=40 name='name' value='$_POST[name]'>
......
</form>
";

That way the next time the form is shown it will show the previous values that they entered in. Then they also have the option of changing those values, but at least it will be there for them if they don't.

Hope this helps

eelix

henry0

9:01 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks eelix,
I tried that variation before
it does not carry back the value

hakre

11:38 pm on Apr 26, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



hi henry,

i know how you feel because it's quite a lot work to produce a useable web-form with error checking.

in a general approach you have the different input fields and their default values. these defaults are assigned to php variables - each field often has an according variable.

with these two things already done, the next step is to integrate these default values into the xhtml form. htmlspecialchars() does a great job here to put things like quotes as default (or later on submitted) values into the input elements:


<form method="post" action="">
<input name="field1" id="field1" value="<?=htmlspecialchars($form_field1)?>" size="32" />
</form>

as an example.

please check, if you're fine with this so far. modify $form_field1's value a bit and see if your page reflects the changes, because when you don't see $_POST values, you might not see even normal values.

orion_rus

3:28 am on Apr 27, 2006 (gmt 0)

10+ Year Member



You can use COOKIEs

henry0

12:57 pm on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi orion_rus,
Cookies or sessions carry the same result
As is it works with sessions
And I destroy them when the job is done

Hi Hackre,
Nothing short of writing to a db or creating a session seems to work
But even the DB solution is moot for I cannot refer to any field or ID that does not yet exist so I cannot do a WHERE etc...

I might have pushed a tad too far security check
But I am pretty happy with the results, it checks for omissions and give precise feedbacks
Same with miss typing or using wrong characters

Funny when you try to be highly safe and very thorough simple stuffs become to need lot of room!
My 10 fields form calls a script 600 lines long :)

And I cannot find a “light” way to pass back value to the form ..........

jamie

8:12 pm on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi henry,

i've just today been working on that very thing, lol

i process the form on the same script as i display it.

i have found the key is to use a separate template for the html form (see bottom of post) and ob_start() to insert the POST'ed values into before you display the errors.

so when you call your error checking routine, you can pass this filled-in template, to the display error function

if (!$submit)
{
// display form
// the entire form is in an html template as suggested above
ob_start(); include($_SERVER['DOCUMENT_ROOT'] . '/templates/forms/contact_us.htm');
$page_content = ob_get_contents(); ob_end_clean();
echo $page_content;
// see the quote at the bottom of this message for the html form template
}
else
{
// do error checking
if ($e->num_errors() > 0)
{
// if there are errors, include the same form template, now with the POST values filled in, and then pass that variable to the display error function.
ob_start(); include($_SERVER['DOCUMENT_ROOT'] . '/templates/forms/contact_us.htm');
$page_content = ob_get_contents(); ob_end_clean();
$e->display_errors($page_content);
}
}

HTML form template:

<form method="post">
Name <input type="text" size="30" name="name" value="<?php if (isset($_POST['name'])){echo $_POST['name'];}?>">
Email <input type="text" size="30" name="email" value="<?php if (isset($_POST['email'])){echo $_POST['email'];}?>">
<input name="submit" type="submit" value="Send">
</form>

clear as mud?

henry0

8:38 pm on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jamie, this looks like a bright solution :)
It should work, I will run a trial with that idea.
Strange I did not find many info about that problem
guess some coders do not think about the user.
In my case I know that they might click away if I do not help them by keeping alive a few values.
Probably JS would have done the job but I am not much of JS person.

jamie

8:59 pm on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



i had to do something like this because of an angry customer. he had just submitted a 600 word review and had a funny character in his name so lost the whole thing! yikes! ;)

henry0

11:32 am on Apr 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jamie, it's way better than my sessions solution
for mine exits the process where the error is
so it saved the above but not the error field.

I am thinking that with such a system
we can explode the error field
use preg and on the fly highlight where the error is in the field that contains the error

jamie

3:38 pm on Apr 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



that's a very elegant addition henry - i've seen that on order forms, where they putthe missing fields in bold red, but never thought to do it myself.

i'll have to play with that.

great idea!

added:

henry, i've just been thinking about how to do that, would you mind going into detail - would you read the entire template into an array (using file()) and then try preg_match on each line?

cheers

henry0

5:08 pm on Apr 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Jamie,
to tell you the truth I did not (yet!) Follow up on the idea
Why not read the whole thing in one shot?

1) format the input (if needed)
2) Then I was recently looking at preg_replace
which might be used to return the error for ex: In bold and red instead of highlighting

Cheers