Getting dynamically generated form variables

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
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Getting dynamically generated form variables

Post by siefkencp »

I have a problem that would lend it self to being able to do this.

1st:

A form that extends itself through a dynamically generated list.

Code: Select all

$table .= "
while(some condition = TRUE){
$form = "<td>$i<br>\n
<input name=\"r$i\" type=\"text\" id=\"r$i\" size=\"1\"><font size=\"-1\">Regular</font><br>\n";
	}

print $form;
2nd: a process to get these variables out (in a loop) and use them.

Code: Select all

while (not finnished working) {
$r = $_POST['r$1'];
dosomething_with($r);
}
Any help would be greatly apreciated!

Chris
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

You are on the right track, but line 3 of the first chunck of code should be :

Code: Select all

$form .= blah
(note the full stop).

Next, define the variable name in a variable:

Code: Select all

for ($x = 0; $x <= $i; $x++) 
{
	$variable = "r".$x;
	$r = $_POST[$$variable]
}
Not tested, but try that and see how you get on...
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

I'm getting:
PHP Notice: Undefined variable: r1 in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7 PHP Notice: Undefined index: in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7

For grin's I have included the whole thing on both ends:

Code: Select all

$t = 1;

$today = date("m/d/y");
$table = "
<form name=\"form1\" method=\"post\" action=\"i_proc.php\">
\n<table border=\"1\" cellpadding=\"4\" align=\"center\">\n";

$start_date = getdate(mktime(0,0,0,$month,1,$year));
$end_date = getdate(mktime(0,0,0,$month+1,0,$year));

$i = $start_date['mday'];
$end_mday = $end_date['mday'];
$long_month = $start_date['month'];
print $long_month . "<br>\n";
$table .= "
<tr>\n 
<td>Mon</td>\n
<td>Tue</td>\n
<td>Wed</td>\n
<td>Thur</td>\n
<td>Fri</td>\n
</tr>\n";

while($i != $end_mday){
	
	$work_date = getdate(mktime(0,0,0,$month,$i,$year));
	$w = $work_date['wday'];

	if($w == '0'){
		$table .= "<tr>\n";
	}

	if(($w != '0') and ($w != '6')){
	if(($w != 1)and ($i == 1)){
	while($w != $t){
	$table .= "<td></td>";
	$t++; }}
	
	$table .= "
<td>$i<br>\n
<input name=\"r$i\" type=\"text\" id=\"r$i\" value=\"0\" size=\"1\"><font size=\"-1\">Regular</font><br>\n
<input name=\"s$i\" type=\"text\" id=\"s$i\" value=\"0\" size=\"1\"><font size=\"-1\">Sick</font><br>\n
<input name=\"v$i\" type=\"text\" id=\"v$i\" value=\"0\" size=\"1\"><font size=\"-1\">Vacation</font></td>\n";
	}
	

	if($w == '6'){
		$table .= "</tr>\n";
	}
	$i++;
}

$table .= "</table><input name=\"Submit\" type=\"submit\"></form>\n";

print $table;
The processing file:

Code: Select all

$max = 32;
$int = 1;

while($int != $max){
$variable = "r".$int;
$r = $_POST[$$variable];

print $r;

$int++;
}
I was thinking maybe I need to add a self building array on the first form to drive the second... Thoughts?
Grim...
DevNet Resident
Posts: 1445
Joined: Tue May 18, 2004 5:32 am
Location: London, UK

Post by Grim... »

Add a hidden form value after the while loop with the value of $i, then get it again on the next page.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

I now get this:
PHP Notice: Undefined index: 5 in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

error_reporting(E_PARSE);

So that you don't get those unnecessary notices.
Last edited by foobar on Mon Nov 07, 2005 10:24 am, edited 1 time in total.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

OK, nevermind the last one... I have register global's on so i swapped out $_POST[$$variable] for just plain $$variable and it works. Thanks for your help! You saved me a bunch of experimentation.

Chris
foobar
Forum Regular
Posts: 613
Joined: Wed Sep 28, 2005 10:08 am

Post by foobar »

siefkencp wrote:OK, nevermind the last one... I have register global's on so i swapped out $_POST[$$variable] for just plain $$variable and it works. Thanks for your help! You saved me a bunch of experimentation.

Chris
You should use $_POST[] instead of just $<post array key>. It's more elegant.
siefkencp
Forum Commoner
Posts: 69
Joined: Thu Dec 16, 2004 8:50 am

Post by siefkencp »

Function over form man... I get paid to maximize resources including my time.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

do not ignore the notices as foobar suggested.. fix the problem. Add checking to see if the index exists using isset()
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

It's

Code: Select all

$_POST[$variable]
, strike the double dollarsigns. And you should never rely on register globals to propogate your user data.
Post Reply