Reload menu selection in form

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

filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Reload menu selection in form

Post by filch »

Hello all,

I am new to this forum so salutations to all.

I searched for this answer and could not find the specific answer and I know I have seen this answer somewhere but cannot put my eyes on it. And for the life of me, I cannot get my brain to come up with the answer ... but I know it is simple. After an error check page of a form with a menu item in it, how does one reload the users selection (assuming they had made one) when the form is reloaded? I know how with the regular input fields but am brain dead on this one.

Thanks in advance.

Dave
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

use sessions
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Sessions?

Post by filch »

I thought of sessions but I could have sworn there was a simpler more direct way ... not that sessions would be that much work mind you. I could be wrong ;-)
User avatar
Fredix
Forum Contributor
Posts: 101
Joined: Fri Jul 18, 2003 2:16 pm
Location: Wehr (Eifel) Germany
Contact:

Post by Fredix »

you could find out what the user has selected and then add the check attribute to the selected option.
At least I think it was the checked attribute that makes makes an option appear inthe selection box.

you can look up on w3schools.com
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

Post by m3rajk »

self-processing forms.
when something is wrong when you check the post/get string just use the post/get string to populate the feilds
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Post by filch »

Yes ... but what if the item is a menu? How do you recall what the user had selection (option value)?

Dave
User avatar
devork
Forum Contributor
Posts: 213
Joined: Fri Aug 08, 2003 6:44 am
Location: p(h) developer's network

Post by devork »

one thing that I used to avoid sessions was
I made a hidden field and populated that field with the unique letter separater with Javascript when user clicks the value is added and then explode them and repopulate them in case anything is wrong.
if you have many category data then you have to make more hidden fields and process ....
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Post by filch »

That seems about as involved to me as using sessions. Sessions arn't that hard so it would probably be as useful to put the values there. As it turns out, I seem to be able to keep the value because it is only being kept over a single page and I have used the statement <? $_POST['fieldvalue']; ?> in the form. However, I could not find a way to place this statement in the menu code in the form but it seem to retain the value anyway after returning from the error page.

Not sure if any of this made sense but thanks all for your replys.

Dave
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Post by JAM »

If I understood you correct...

A javascript that goes page history-1 combined with this...

Code: Select all

header("Cache-control: private");
...to block naste IE bug perhaps?

The snippet above so that the form is not cleared upon returning to the former page.

Code: Select all

<?php header("Cache-control: private"); ? > // this board cant handle this correct, added space...
<form method="post">
 <select name="foo">
<?php
 for ($i=1;$i<=22;$i++) {
    echo '<option>'.$i.'</option>';
 }
? >  // this board cant handle this correct, added space...
 </select>
 <input type="submit">
</form>
Run this and press the back key once. I'm not using javascript in this example (you might not need it either) but it might give you some ideas.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

filch wrote:Yes ... but what if the item is a menu? How do you recall what the user had selection (option value)?
Take a look to a Code Snippet I post just right now: PHP/HTML: Print <select><options></select>. Hope it helps!

Cheers,
Sco.
filch
Forum Newbie
Posts: 22
Joined: Sat Oct 25, 2003 12:34 pm
Location: Toronto

Post by filch »

OK .. I understand what you are doing is creating a select form list with PHP. Again, my question is .. if the person makes a mistake and the script generates an error page, and then the user clicks on the back button or a javascript history-1 link, in a normal form element (non menu) I could use $_POST['fieldname'] or $_REQUEST['somefieldname'} to repopulate the form with the user's values so they would not have to start from scratch. BUT .. how would I do that with the menu element?

filch
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

I don't belive $_POST is available when the user press the back button. It seems to be a browser issue. Did you try JAM's solution already?
User avatar
gjb79
Forum Commoner
Posts: 96
Joined: Fri Jul 18, 2003 6:35 am
Location: x <-- (DC)
Contact:

tricky

Post by gjb79 »

If it is a selfproccessing form, you could call up the value of each form element as the initial value. ie

Code: Select all

<input type="text" value="<?php echo $_POST['fieldvalue']; ?>">
If somebody just arrives on the page, there will be nothing in the fields, after they enter stuff and submit the form, when it refreshes the entries should hold the values the user entered.

Hope this is what you where looking for.

tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post by tsg »

1) Best to post the form to itself. Example, at the top of your page, do a

Code: Select all

if($_POST['submit'] == "submit") {
   // do your form checking
   // if something is not right or empty , change the submit value
   // if correct, process form
} else {
If it is not correct and you want to populate the form with the current answers ... do as mentioned above ... in your input tags, use

Code: Select all

value="<? print "$input_name"; ?>
To rechoose an option / drop down box, do this

Code: Select all

<select name="name">
<option value="2" <? if($name == '2') { print "selected"; } ?>>
<option value="3" <? if($name == '3') { print "selected"; } ?>>
<option value="4" <? if($name == '4') { print "selected"; } ?>>
By it being selected, then it will rechoose that option. By pressing the back button, I don't know if it will still recall the choices .. or with any form .. php or not.

For some reasons the examples above are not showing the closing php tags \?> ..
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

unless its an extremely long form i'd use sessions

just save the variables to the session when you submit the form, then call on them for the values

shouldnt be too hard..
Post Reply