forms - reloading form and filling in last entered options

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

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

forms - reloading form and filling in last entered options

Post by robster »

Hi there,

I have a form, it has checkboxes, radio buttons, text fields, drop down lists and a submit button.

When the user hits that submit button it reloads the form, checks over the data to see if the user has filled in 'all the blanks' as it were and if they haven't displays in red text what they didn't fill in.

This is all good and works well, but it also displays the form with no options checked/filled in. So the user now has to go and re-fill in all their data.

I'm after a way to have the page re-load, do the error checking as it does, but re-fill in the values passed in the POST array.

I should note that the forms are built dynamically from a database, an example might be this radio group:

Code: Select all

<?
		  mysql_select_db($dbname);
				$sql = "SELECT * FROM table_name ORDER BY id ASC";
				$content = mysql_query($sql);
				$Xcontent = mysql_fetch_array($content);	
				$cShowMax = mysql_num_rows($content);
			
				for ($y=1; $y<=$cShowMax; $y++)
				{ 
					$id = $Xcontent["id"];
					$we_are = $Xcontent["we_are"];
					
				echo "<label><input type=\"radio\" name=\"we_are\" value=\"$we_are\">$we_are</label><br>";	
			
				$Xcontent = mysql_fetch_array($content);
				}
				mysql_free_result($content);	
			  
			?>
This means that the form still needs to draw dynamically as it does, but be intercepted by the previous post data (if it exists) and draw it with the users last posted variables.

I've had a look at this kind of solution: http://www.evolt.org/article/Clever_for ... /17/60143/ but in all honesty, it's not for me. It's a rather complicated little function whereas I'd like to do conditional checks with PHP only on each step of the form draw. Keeps it simple for a PHP simpleton like me and it keeps java out of it.


So I'm here asking for advice as I've searched and thought and scoured my brain and basically, could do with some pointers in the KISS theory :)

Thanks for your time, I hope someone out there has an idea!

Rob
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

OK, it seems on that theory I have it working a treat for radio buttons and for text boxes (single input types) but if I try it for a checkbox then all it ticks is the LAST checkbox of the lot selected.

IE: Let's say we have 4 checkbox items:

Code: Select all

pegs
towels
dog collars
ferrari
the user chooses 3 items, pegs, towels and of course, the ferrari (who needs a dog collar anyway right?! ;)).

When the page is reloaded and the code loops through, it then draws the LAST item as the one checked and leaves the others (ie: in this case it would check the ferrari but not check the pegs and towel).

Looking at my post data it has something like this:

Code: Select all

&amp;choose_a=pegs&amp;choose_a=towels&amp;choose_a=ferrari
The loop which draws the checkbox is obviously only choosing the last of the choose_a POST variables.

Is there a way to ... make it use them all?

Here's my code for drawing the checkbox now:

Code: Select all

<strong>wanting to choose a:</strong><br>
		  <?
		  	mysql_select_db($dbname);
				$sql = "SELECT * FROM table ORDER BY id ASC";
				$content = mysql_query($sql);
				$Xcontent = mysql_fetch_array($content);	
				$cShowMax = mysql_num_rows($content);
			
				for ($y=1; $y<=$cShowMax; $y++)
				{ 
					$id = $Xcontent["id"];
					$choose_a = $Xcontent["choose_a"];

					if ($get_choose_a == "$choose_a")  //if user is reloading page and has checked an item in this check box group then refill in their checked boxes
					{
						echo "<input name=\"choose_a\" type=\"checkbox\" value=\"$choose_a\" checked>$choose_a<br>";	
					}
					else  //just show normal empty options
					{
						echo "<input name=\"choose_a\" type=\"checkbox\" value=\"$choose_a\">$choose_a<br>";	
					}
					
				$Xcontent = mysql_fetch_array($content);
				}
				mysql_free_result($content);	
			  
			?>
$get_choose_a is gathered earlier in the script. Perhaps this is part of the problem? Looking at the code:

Code: Select all

$get_choose_a = $_GET['choose_a'];

It might only be only getting the last of the array, rather than all three... I think i've just found my problem.

Soooooo... how can I get all of the options that have been posted, then use them in my loop? I could do with some help if anyone has anything to offer. Thanks again!!

(talking it out often helps it seems :))
Post Reply