Page 1 of 1

How to retain variables when "reseting" a form?

Posted: Fri Sep 20, 2013 1:26 am
by Fawkes
Hi,

Hopefully my title isn't too misleading, but I will explain my situation.

I am currently helping some acquaintances with managing their site as they don't officially have people to do it. I don't really know php and anything I know about it, I learned by trial and error in the last month. So my situation involves this:

I have a form involving various drop downs and text boxes for the user to fill information for a specific user such that the site url is something like "site.com/achievement.php?user_id=11user".

After the user is done inputting the data, they press a submit button which then pretty much takes all the data inputted and redisplays it and gives the user a confirm button. On this page, there is a modify button, which is supposed to let the user go back to the previous screen to modify anything, however whoever implemented this currently has it working like the following:

Code: Select all

	echo "<form name='achievement_modify' method='post' action='".$_SERVER['PHP_SELF']."'>";
What it did was it goes back to the previous page for the user to put in the info, however it does not retain any of the fields that were filled before and even forgets the specific user the data is for ("site.com/achievement.php").

What I want to know how to do is how can I create the button such that it can remember all these things?

Sorry if I sound like I have no idea what I am talking about. Because I really don't :s

Re: How to retain variables when "reseting" a form?

Posted: Fri Sep 20, 2013 6:20 am
by Celauran
That one line of code you shared shows that they're sending you 'back' (they're actually loading the same page) via post request, which means all the form data will be contained within the $_POST array. You can use this to prepopulate the form data. As for the lost user, check that their isn't a hidden field containing their user ID. If there is, they aren't lost. If not, consider adding one.

Re: How to retain variables when "reseting" a form?

Posted: Fri Sep 20, 2013 10:35 am
by Fawkes
I'm not quite sure on your second point. What do you mean by hidden variable?

site.com/achievement.php?user_id=11user

I know that the user id is contained within a variable called $user_id prior to them pressing the Modify button. Would I be able to use this to somehow get back to site.com/achievement.php?user_id=11user instead of site.com/achievement.php

I know when I try to reload the page, it goes through the following code:

Code: Select all

	if ($user_id == "") {

	$user_id = $_GET['user_id'];

	}
	echo "<input type=hidden id=user_id name=user_id value='$user_id'>";
However, when I check the page source, it seems like it is empty.

Code: Select all

<input type=hidden id=vuser_id name=vuser_id value=''>
EDIT: I was able to figure out everything. I am able to retain all information from the $_POST array including the userid. Thanks a lot!


Thanks.

Re: How to retain variables when "reseting" a form?

Posted: Tue Sep 24, 2013 5:18 am
by priyankagound
If you are looking to just repopulate the fields with the values that were posted in them, then just echo the post value back into the field, like so:

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


Hope this helps you.