0

I have an decent sized HTML form, on top of this is Javascript which can create many more 'records' to certain parts of the form. In total we could be talking 50+ INPUT elements.

I'm wondering of the best way to process all of this data by PHP:

Pass it as a normal INPUT elements via POST and let the PHP decode it all.

Or lose the names of the form input elements so they are not presented in the POST and then use Javascript on the submit event to encode all the required input elements into an object and then encode that object into JSON and pass via a single hidden input element.

I'm thinking the later would allow me to write clearer code on the PHP side, and it would basically be passed as an object once run through json_decode(), and would be more abstract from html changes. Whilst the former requires no javascript, but would need to be kept in sync with the html.

Or another method I've not thought of.

Which do you think would be the best way?

Moose
  • 109
  • 1
  • 10

3 Answers3

0

You do not need an abstraction layer between html and php. Nobody does. Use normal html behavior and retrieve if from php the normal way. If for some reason you really need an object instead the normal $_POST array, call

$POSTobject = json_decode(json_encode($_POST), FALSE);
ericH
  • 1
0

In my experience I found that for large to very large forms the easiest was to keep the input fields in their own form, with tags, but rather to give the input fields a pattern name, like somefunctionalname_01, somefunctionalname_02, etc, and have the server-side processor to look for this class of variables. Your business code can then sort the parameters according to the category they belong to, and you can use them at leisure in a structured way later.

e.g.

$MyBroadCategories = array('somefunctionalname', 'someotherfunctionalname');
foreach($MyBroadCategories as $Cat) {
  foreach($_POST as $key => $val) {
    if (preg_match('/^' . $Cat . '_(\d+)$/', $key, $bits)) {
      // Please business code here with this particular class of variables
      // Or add them to an specific array of candidates for later use.
    }
  }
}

} }

Attention, in all cases, if you exceed the maximum number of parameters handled by your server (defaults to 1000), think of adjusting the max_input_vars parameter of your PHP configuration.

tropicalm
  • 51
  • 1
  • 3
  • I like your answer, and I would vote it up if I was able. I think Joseph's answer translates better to the php 'world' however. – Moose Sep 23 '13 at 12:00
0

You don't even need JS to write "clearer code". You can pass arrays of data to PHP from forms in this manner. This is even clear enough. Taken from my other answer, you can do this (the previous question was about checkboxes, but should work with any input type):

<input type="checkbox" name="answers[set1][]" value="apple" />   //imagine checked
<input type="checkbox" name="answers[set1][]" value="orange" />  //imagine checked
<input type="checkbox" name="answers[set1][]" value="grape" />
<input type="checkbox" name="answers[set2][]" value="airplane" />   //imagine checked
<input type="checkbox" name="answers[set2][]" value="train" />  //imagine checked
<input type="checkbox" name="answers[set2][]" value="boat" />
<input type="checkbox" name="answers[solo]" value="boar" /> //single type value. note that there is no [] in the end

end up like this in the request array (like say POST):

$_POST[] = array(
    'answers' => array(
        'set1' => array('apple','orange'),   //unchecked items won't be included
        'set2' => array('airplane','train'), //unchecked items won't be included
        'solo' => 'boar'
    )
);
Community
  • 1
  • 1
Joseph
  • 117,725
  • 30
  • 181
  • 234
  • Perfect, I didn't even know you could do that! Heck I just tried it and it even works for GET requests. – Moose Sep 23 '13 at 11:57
  • @user1304073 [URLs have a length limit](http://stackoverflow.com/q/417142/575527), you can't pack that much data into a url. – Joseph Sep 23 '13 at 12:06
  • Thanks, I'm aware of that, I just thought it was a nice alternative should I need/use it sometime. Typically I only use GET for things like searches, and POST for forms. Otherwise your address bar looks all horrible. – Moose Sep 23 '13 at 15:38