msledge Posted September 14, 2007 Share Posted September 14, 2007 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] Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/ Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 I design everything as if no javascript allowed and that someone isn't using the page and send it all directly. Do all checks with php. Once satisfied go and play with js. Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348609 Share on other sites More sharing options...
msledge Posted September 14, 2007 Author Share Posted September 14, 2007 I agree...actually, I do form validation using php also. I didn't mention that. I use the javascript to enable / disable the combo boxes upon clicking the select statement. Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348612 Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 Sorry missed point of question. As you generate the table from db, say prefix each form element with a row name or similar. Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348620 Share on other sites More sharing options...
msledge Posted September 14, 2007 Author Share Posted September 14, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348624 Share on other sites More sharing options...
phat_hip_prog Posted September 14, 2007 Share Posted September 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348646 Share on other sites More sharing options...
msledge Posted September 14, 2007 Author Share Posted September 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348659 Share on other sites More sharing options...
msledge Posted September 14, 2007 Author Share Posted September 14, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/69380-processing-php-forms-with-variables-set-as-arrays/#findComment-348670 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.