Page 1 of 1
Getting dynamically generated form variables
Posted: Mon Nov 07, 2005 9:20 am
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
Posted: Mon Nov 07, 2005 9:31 am
by Grim...
You are on the right track, but line 3 of the first chunck of code should be :
(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...
Posted: Mon Nov 07, 2005 9:48 am
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?
Posted: Mon Nov 07, 2005 10:03 am
by Grim...
Add a hidden form value after the while loop with the value of $i, then get it again on the next page.
Posted: Mon Nov 07, 2005 10:18 am
by siefkencp
I now get this:
PHP Notice: Undefined index: 5 in c:\Intranet\facstaffstuff\timereport\i_proc.php on line 7
Posted: Mon Nov 07, 2005 10:21 am
by foobar
error_reporting(E_PARSE);
So that you don't get those unnecessary notices.
Posted: Mon Nov 07, 2005 10:23 am
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
Posted: Mon Nov 07, 2005 10:25 am
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.
Posted: Mon Nov 07, 2005 10:46 am
by siefkencp
Function over form man... I get paid to maximize resources including my time.
Posted: Mon Nov 07, 2005 7:29 pm
by feyd
do not ignore the notices as foobar suggested.. fix the problem. Add checking to see if the index exists using
isset()
Posted: Mon Nov 07, 2005 8:03 pm
by Ambush Commander
It's
, strike the double dollarsigns. And you should never rely on register globals to propogate your user data.