[SOLVED] Dynamic 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

Post Reply
efortis
Forum Newbie
Posts: 7
Joined: Mon Oct 13, 2008 7:48 pm

[SOLVED] Dynamic Form

Post by efortis »

Hi Guys!

I'm building a form in which the quantity of rows is the number of tuples on tblTask.
Each row contains various INPUTs and a SUBMIT. Every row contains the same INPUTS.

I create the form in this manner:

Code: Select all

 
$iTask=1;
do { 
    // Other INPUTs... e.g. name="jobStatus_APPEND_TASK_NAME"
    <INPUT type="submit" name="<?php echo $row_rsTask['taskName'];  ?>" value="Save">
    $iTask++;
} while ($row_rsTask = mysql_fetch_assoc($rsTask)); 
//I use $iTask to compare the tuples for initial values.
 
Every click on the Submit button (Save) must insert a tuple into tblJob only with the values in the same row.

The query part:

Code: Select all

 
$btnClicked = "none";  // I want to set this variable to the button name
$txtStatus = 'jobStatus_'.$btnClicked;  // e.g. Taskname appended to each field prefix
 
if ($btnClicked != "none" ){ $insertSQL ="... $_POST[$txtStatus] ...$jTask ..." }  
 
What would be a good approach to tackle this situation?
Thanks in advance for any suggestion
Last edited by efortis on Tue Oct 14, 2008 9:30 am, edited 2 times in total.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: Dynamic Form

Post by Stryks »

I'm not entirely sure what it is you're asking, so let me just clarify.

You have a query returning tasks. For each task you want a row on inputs and a submit button.

When data is filled into any one row and that rows submit button is pressed, you want that rows information added (updated) to the database.

Assuming that each $rstask row has an autonumber index, what I'd be doing is doing something like ...

Code: Select all

 
<?php
while ($row = mysql_fetch_assoc($rstask)) {
?>
   <input type="text" name="data1[<?php echo $row['task_id']; ?>]" /> 
   <input type="text" name="data2[<?php echo $row['task_id']; ?>]" />
   <input type="submit" name="submit[<?php echo $row['task_id']; ?>]" value="" />
<?php
}
?>
 
... if you want to use the textual name for the task instead of the id, I guess that's up to you. It should work either way.

Then when you check the submitted from data, you can collect the specific row with ...

Code: Select all

 
if($_SERVER['REQUEST_METHOD'] == 'POST') {
   $task_id = key($_POST['submit']));
   // example to show access to the proper rows values
   echo "The values for task number $task_id are, data1: " . $_POST['data1'][$task_id] . ' and data2: ' . $_POST['data2'][$task_id]; 
}
 
Of course, data1 and data2 are just sample names I gave to the fields. They could be anything you want that would describe the data in that textbox.

Hope that makes sense.
efortis
Forum Newbie
Posts: 7
Joined: Mon Oct 13, 2008 7:48 pm

Re: Dynamic Form

Post by efortis »

Stryks,

Thanks a lot, I really appreciate your time. Now the code works great and looks beautiful!!
Thanks again
Post Reply