Jump to content

Processing PHP Forms with variables set as Arrays


msledge

Recommended Posts

I have a logistics (trucking / freight) operation.  I have a form online that allows customers to set their delivery appts.  This form is built after i run a query that says "Go get all the active shipments for customer and display them in the table".

 

The table lists the freight equipment number, the date it is released, a drop down to select the date, a drop down to select the delivery location, and a checkbox used to select the record.

 

The checkbox is assigned a variable name of "checkArray[]" and the value is set to the unique ID of that record.  The two drop down combo boxes are also assigned to array variables i.e. dateArray[] and locationArray[].

 

My problem is this:  I need to run through the form and process it so that I essentially have a list of everything that was checked and the associated date and location values that were input.  I first run through the checkArray to get a list of everything that has been checked.  This is no problem.  The problem arises when I try to associate that record ID with the selections in the other combo boxes.

 

Let's say the result table is 25 total records.  I check 3 records to submit for update.  Well..the array in the post variable for the checkArray is 3.  However, the arrays for the date and location variables are 35.  The reason is because I load the form and set the value for the combo boxes upon loading.

 

I know that I can traverse the table with Javascript and the DOM.  However, I need to submit this via a php processing form to the server.  I'd prefer to do it all with PHP.  I am using Javascript to validate form input on the front end.  Maybe there is a reliable way to pass the data from Javascript to PHP, however, if someone has javascript turned off i still need them to be able to process the form.

 

I guess my question is this:  Does anyone have a good way to manage this?  I've uploaded a screenshot for assistance.   

 

[attachment deleted by admin]

Link to comment
Share on other sites

to make sure i understand you...

 

if i create a variable called $tr and apply it to the <tr> element, where $tr is something like tr1,tr2,tr3, etc.

 

I will add that name as a prefix on all 3 form elements:  my 2 select elements and my 1 checkbox element.

 

So instead of having the elements stored in a declared array i.e. name=checkboxArray[], each element is stored in a separate variable i.e. tr1_dateArray, tr1_locationArray, tr1_checkboxArray

                tr2_dateArray, tr2_locationArray, tr2_checkboxArray

etc.

 

Is this what you're suggesting?

 

Link to comment
Share on other sites

Well obviously i'm not reading your original question (it's late) and just thinking like I do. I'd personally not use js, i'd treat it all as  row's, as I generate each row i'd add a prefix with 'id', e.g. the name would look like so:

01_id 01_container 01_day_released 01_last_free_day 01_delivery_date 01_destination 01_tmp 01_select

You can still validate with js, just update where they are and then actually submit the form. However when php receive's the data, everything has a tag, you could (explode into 2 parts) or (check prefix and check strstr() / strpos() )... Then if 01_select == checked then update using 01 elements. Sound's complex but it's simpler than it sounds with array's...

 

I've just re-read what you've written, i've not used a js array and returned it so i'm not sure of the result, are you saying you get all 35 (say) elements, and your not sure which have changed, if so you could laboriously update them all.... I've re-read again... and i'm lost... 25 / 35 ??? When do you set the combo's (when gen with php or after loaded with js?)

 

I do prefer your version, only returning what you need and activating combo when required, but as I say js is a secondary luxury to me, so I approach from a totally different angle.

 

I need a cup of tea and a think... tell me what your thinking... hmmm

 

Link to comment
Share on other sites

I made a typo when I typed 35, meant to type 25.  When you assign a combo box a value: <select name=dateArray><option value="1">$var</option><option value="2">$var</option></select>, the PHP Array $_POST['dateArray'] will automatically have the same number of elements in it as the number of rows in the table.  In contrast, $_POST['checkboxArray'] will have an index only equal to the number of checkbox elements that were checked.

 

so if i wanted to say, show me checkboxarray[$i] and datearray[$i] and locationarray[$i] in order to build an update statement to be sent back to the db, it won't be accurate because the index for checkboxarry will be (for example) 5, when the indexes for the other two arrays will be 25 (as in the first example).

 

To reiterate:  I'm not using any JS for form processing...only to disable / enable the form elements.

 

I think I understand what you're suggesting with the prefix.  I'll give it a try and see how well it works.

 

Just to share...I already have a working solution but i think it is less than optimal.  For my drop down boxes I made the first selection a hard coded value ( <option value = ''> ) before I create the rest from script.

 

Then I use the array_filter function to filter both arrays down to all values that do NOT EQUAL ''.  This re-sets the arrays and now all the indexes are in line with each other.

 

But like I said...I don't think it is optimal or efficient because you are forced to program a setting for the element unecessarily.

 

Thanks for you're suggestion.  I'll let you know how it works.

 

Michael

Link to comment
Share on other sites

phat_hip_prog: 

 

I was able to successfully implement your suggestions and it worked.  Now I have 2 solutions and I think I'll implement yours.  I now have one more problem that I want solved and wondered if you could help.

 

With the same image that I uploaded...is there any way that I can read the data in the 'CONTAINER' column in the HTML table without having it load in a form element?  Right now, it's just the value between the <td> tags.  I could insert an <input> element and load the data as the value of that element, but I would prefer not to.

 

Michael

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.