Forum Moderators: coopster

Message Too Old, No Replies

How To Process Dynamic Form Field Names

How can I process a form in php when the form field names change?

         

bbouch

2:43 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Let me start by saying I don't have a lot of PHP experience, and am just starting to fumble my way around some of this stuff... I'm sure this has been asked a number of times, but after 2 days of searching the internet, I just haven't found a solution that makes sense, so I am hoping someone here can help me.

I have a pretty simple project schedule sort of web page that lists a number of tasks & 4 dates associated with each task. (planned start, planned complete, actual start, actual complete.

The page is built dynamically based on what is in a mysql database, and the dates listed are form fields, with the intent that a person with access can change any number of these dates.

Each task has an id associated with it, so when i build the form, I name the form field <task_id>-<date type>. so, for example, for task id = 10 I have 4 related form fields:

10-planned_start
10-planned_complete
10-actual_start
10-actual_complete

This basic pattern is repeated for all the tasks (up to 100 right now, but it could grow slightly).

Once all the form fields have been updated, and the user clicks submit I want to update the data in the database with the appropriate data.

So, my questions is:

1) How can I process this form, when I don't know what the names of the fields are going to be, and/or how many fields there are?

2) Is there a good way to identify only those fields that have changed and do updates on only those fields, or am I "stuck" updating all the records, even those where the dates have not changed from when the form was built?

- Brian

Dilly

2:49 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Welcome to WebmasterWorld bbouch!

So are you saying that you currently have a working script that adds the 4 dates for a task to the database but dont know how to edit existing tasks with dates in the database.

eelixduppy

2:50 pm on Sep 18, 2007 (gmt 0)



Hello Brian, and Welcome to WebmasterWorld!

When you post the form, you can get the names of all the fields by grabbing the keys from the POST array. Try something like this and you will be able to see what I mean:


echo '<pre>'; print_r([url=http://us2.php.net/manual/en/function.array-keys.php]array_keys[/url]($_POST)); echo '</pre>';

Then you'd have to loop through the 'keys' and create your query from it.

bbouch

3:12 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanks for the Welcome Dilly.

I have 1 form where a person enters a project, and an end date. Based on that end date, the database is populated with a "first stab" at when each task should be started and completed. But these dates can change, and I need a way to allow the person in charge of managing this page the ability to edit the actuals as well.

Currently, the page is one large form, but I am certainly open to ideas about a better way to set things up, if it makes more sense to do so.

I actually mispoke slightly in my original post. The form is organized by a High Level ID, and Task ID (which is basically a subcomponenet of the High Level Id) so there are 3 parts to the dynmically generated form name. Here's an Example, if it helps:


<table>
<tr>
<td align="right"> Hardware Equipment Plan Completed&nbsp; &nbsp;</td>
<td > <input type="text" name="40-21-planned_start" size="10" VALUE="2007-01-01"></td>
<td ><input type="text" name="40-21-planned_finish" size="10" VALUE="2007-01-01"> </td>
<td ><input type="text" name="40-21-actual_start" size="10" VALUE="2007-03-03"> </td>
<td ><input type="text" name="40-21-actual_finish" size="10" VALUE=""> </td>

<tr><td align="right"> Audio Visual Plan Complated&nbsp; &nbsp;<br></td>
<td > <input type="text" name="40-22-planned_start" size="10" VALUE="2007-02-01"></td>
<td ><input type="text" name="40-22-planned_finish" size="10" VALUE="2007-02-28"> </td>
<td ><input type="text" name="40-22-actual_start" size="10" VALUE=""> </td>
<td ><input type="text" name="40-22-actual_finish" size="10" VALUE=""> </td>

<tr><td align="right"> Tell Comm Plan Completed&nbsp;&nbsp;</td>
<td > <input type="text" name="40-23-planned_start" size="10" VALUE="2007-02-01"></td>
<td ><input type="text" name="40-23-planned_finish" size="10" VALUE="2007-02-28"> </td>
<td ><input type="text" name="40-23-actual_start" size="10" VALUE=""> </td>
<td ><input type="text" name="40-23-actual_finish" size="10" VALUE=""> </td>
</tr>
</table>

The actual number of tasks (i.e. table rows) can change at any given time, based on the number of tasks related to a particular effort.

I'd like to be able to determine what has changed since the page is loaded, although I'm not sure if that's possible or not, and then update the values in the database for those changed fields.

I guess my initial, basic question is how can I process any of the form fields if I don't know their name until the form is built. DO I need to create some sort of array of form field names and then do a loop through that array when the submit button is pressed, or is there a better way?

I only ask because I am sure this scenario has been talked about a million times, and there are probably some good examples I could work from, but I haven't had much luck finding them based on the searches I'e been doing.

Not sure if that clarifies what I am looking for, but I hope it helps.

bbouch

3:15 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



Thanks eelixduppy,

I thought there had to be something like that available.

Now, If I wanted to see what had changed, I am guessing I need to populate a similar array with the initial values when I am building the page?

- Brian

eelixduppy

3:35 pm on Sep 18, 2007 (gmt 0)



That will probably work. I'm not sure I understand why you need to know which ones change, though. If you are going to update the database, why not just update every field? It will make the programming a little easier and it shouldn't make any other difference.

bbouch

3:42 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



capturing the differences isn't vital, but I was hoping to capture who updated the data so we could go back and ask if we have questions around why and/or when something was updated a specific way.

I'll try it without to start with and if I can get that working, I'll worry about the fluff later :)

snowman304

5:16 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



You could record the IP or even the user's ID for each record. I am assuming that the user will log in to do the editing, so why not just create another field in your DB table to put the ID of the user that edited that record.

borntobeweb

11:51 pm on Sep 18, 2007 (gmt 0)

10+ Year Member



blouch, off the top of my head, you can do this:

For each plan you add the text fields as you already have, then another set of hidden fields with names like "orig-40-21-planned_start" "orig-40-21-planned_finish" etc, and set them to the current values in db, and add one more hidden field called "plan[]" set to the unique plan ID, e.g. "40-21".

Now when you process the form:

foreach($_POST['plan'] as $planID) {
if($_POST[$planID.'-planned_start']!= $_POST['orig-'.$planID.'-planned_start']
¦¦ $_POST[$planID.'-planned_end']!= $_POST['orig-'.$planID.'-planned_end']
¦¦ ...) {
update db;
}

Note this doesn't take into account concurrent changes to the DB, ie when the DB is changed between the time the form is initially loaded and submitted. For that, instead of using the "orig-" hidden fields, compare the posted values to whatever's in the db.

borntobeweb

3:07 am on Sep 19, 2007 (gmt 0)

10+ Year Member



Oops sorry i mistyped your name, bbouch with a b not l.

bbouch

10:38 am on Sep 19, 2007 (gmt 0)

10+ Year Member



No problem :) I've been called worse.