views:

446

answers:

5

I am trying to figure out the best way to name my HTML form fields so that they can be easily parsed into a set of PHP opbjects.

For example I have the following table of elements: (let's assume it is inside a form)

<table>
  <tr id='payment0'>
    <td><input type='text' name='paymentType0'></td>
    <td><input type='text' name='paymentAmount0'></td>
    <td><input type='text' name='paymentAccount0'></td>
  </tr>
  <tr id='payment1'>
    <td><input type='text' name='paymentType1'></td>
    <td><input type='text' name='paymentAmount1'></td>
    <td><input type='text' name='paymentAccount1'></td>
  </tr>
</table>

Obviously I can write a bunch of code to search through the $_POST array to find all the paymentType/Amount/Account etc. values and put them into objects. However this feels a bit smelly and I am wondering if there is a better way to name these form elements?

On a side note, I had attempted to name the fields as control arrays (at least that's what I call them) so instead of paymentType0 it would be paymentType[0] and PHP would see them as an array that I could iterate though. The problem with this approach is that referencing these fields with jQuery becomes very difficult due to the square brackets.

What's the 'best of both' worlds solution for this?

A: 

You could consider preprocessing the submitted values with jQuery. Instead of submitting the raw form data you could structure them on the client-side and pass them to the server using JSON, which can easily be transformed into a PHP object.

n3rd
A: 

I agree with n3rd, if you're having trouble with the variables once passed through, pre-format your data on your end.

A litte extrapolation on this idea just means to store your data in a JSON object which you construct as the user enters values and then on submit send the JSON object to the next page instead of worrying about what each variable name is.

If that's not it, there is another possible solution although I'm not sure if it addresses your particular problem:

http://www.configure-all.com/arrayoffields.php

(grabbed from a google search on "php form names jquery array")

Organiccat
A: 

Use the names with square brackets so that PHP's form processing library will convert them to arrays.

Then, if you need to reference them using JavaScript - use square bracket notation.

David Dorward
A: 

You can always try following code:

foreach($_POST as $key => $val)
{
    if(isset($$key)) $$key = $val;
}

However this approarch is bad because you can POST variables like PHP_SELF etc, so you would like to check those.

Pavels
+3  A: 

You can use array without any problems, but don't call the fields paymentType[0], paymentAmount[0], ...

Instead, call them payment[0][type], payment[0][amount], etc.

Then the records will be conveniently grouped into associative arrays, which are much easier to manipulate:

foreach ($_POST["payment"] as $payment) {
   echo "The type was $payment[type] and the amount was $payment[amount]";
}

As for the jQuery: follow David's suggestion. Objects in JavaScript are really just associative arrays with syntactic sugar and you can access their members as follows:

myForm["payment[0][amount]"].value = 300
rix0rrr