Hi all -
I'm new here and am hoping that someone might be able to help me with a problem I'm having. I've been coding PHP for a couple of months now, so I'm still trying to figure alot of things out, especially forms processing.
I have a script for a parts inventory app that I'm building. Basically, there are 3 screens -- Count Parts, Reconcile Count, Results -- the details for which follow.
The Count Parts form is built dynamically from an array that contains data retrieved from MS SQLServer. I loop through the data array and output "rows" of data -- qtyOnHand (hidden field, from DB query), qtyCounted (text input), itemNumber and itemDescription (both HTML text from DB). For each item, the user enters a physical count and then submits the form.
The Reconcile Count form is built dynamically from the form post variables array based on the items where qtyOnHand and qtyCounted do not match. The user will be able to then enter a reason for the discrepancy, something like backordered, lost, or returned, and specify whether this reason applies to all items in the discrepancy. The data in the form is the difference between qtyOnHand and qtyCounted (HTML text, calculated), reasonCode (HTML select with codes that will go into my DB insert query), and appliesToAll (HTML checkbox, to go in insert query). The user fills out and submits this form, and is then presented with the Results screen, which takes care of compiling the form results and creating and executing the DB query in the background.
The problem I'm having is how to best name my form elements so that the input data is easily accessed and updated from screen to screen. I wasn't really worried about it at first, and I was simply using an array for each form element and looping through the arrays, comparing values based on the array index. Then I ran into the checkboxes, and that's where my troubles stared. I totally forgot that checkboxes do not pass "false" or "no" or "off" when they are not checked -- they don't pass anything -- so my appliesToAll[] array indexes get messed up when a user unchecks an item on the Reconcile screen. And unfortunately I have to keep the checkbox since the project/management team has already signed off on the design/functionality of the application.
Is there a convenient way to keep up with the data from form to form? I thought of trying multidimensional arrays or associative arrays, but I haven't been able to make that work. I also thought of possibly using the part number as the index for my arrays and using foreach() as the looping structure rather than for(). I even thought of using session variables until the director of development here canned that idea for some reason.
So what about it? Any PHP gurus want to try to tackle this and help me out?
Thanks in advance,
-- phil --
Naming dynamic form elements
Moderator: General Moderators
Yes, I would say using Session's would be the easiest.Is there a convenient way to keep up with the data from form to form?
At each page, simply modify $_SESSION to contain the current sampling of what the user choose.
This also benifits your pages, because you can easily set it so that when I user goes back a page, that new page reflects whats in the $_SESSION data.
In fact, at the end of the forms, you can easily print out a "Are you sure" page with the current $_SESSION information.
Does this help? Or am I off the mark here?
Thanks, Jason, for the offline help.Yes, I would say using Session's would be the easiest.
... snip ...
Does this help? Or am I off the mark here?
To recap for anyone else who is interested, I don't believe I will be able to use sessions, but I did get word that I can change the checkbox to a select menu, so my problems are (seemingly) solved. I'll just pass things in arrays and match them up by index.
This seems to be a great community of PHP developers, so I'll definitely be keeping my eye on the forums.
Thanks again,
-- phil --