1

I need to enter UTM coordinates of certain polygons to a HTML form. I will handle the form values with php to insert into mysql db. But number of polygons and points of the polygons can be various. So text input boxes can be adjusted on the form.

+---------+------------+-----------+-------------+
| polygon | point      | y         | x           |
+---------+------------+-----------+-------------+
| 1       | 1          | 0         | 0           |
+---------+------------+-----------+-------------+
| 1       | 2          | 0         | 50          |
+---------+------------+-----------+-------------+
| 1       | 3          | 50        | 50          |
+---------+------------+-----------+-------------+
| 1       | 4          | 50        | 0           |
+---------+------------+-----------+-------------+
| 2       | 1          | 50        | 75          |
+---------+------------+-----------+-------------+
| 2       | 2          | 50        | 100         |
+---------+------------+-----------+-------------+
| 2       | 3          | 75        | 75          |
+---------+------------+-----------+-------------+

Letting the user to enter number of points to create input boxes seems a good practice to me. However I'm not sure is the best practice to serialize values and handling the values with php ? Should I use JSON or just input names with indexes. Or something else?

Here is a simple example: http://jsfiddle.net/UVZNq/

tuze
  • 1,978
  • 2
  • 15
  • 19

2 Answers2

1

You can try something like this:

+---------+------------+-----------+-------------+
| polygon | point      | y         | x           |
+---------+------------+-----------+-------------+

                                ***************
                                * Add Another *
                                ***************

When the user presses 'Add Another` Submit the current point using Ajax and convert the existing (already submitted) point as simple text (rather than textbox) and add new set of text boxes.

This way the php script is handling a single point at a time and the UI is not cluttered either.

WeaklyTyped
  • 1,331
  • 4
  • 16
  • 31
0

if you just name your inputs a common name like (with brackets)

<td><input type="text" name="ply[]" /></td>
<td><input type="text" name="pnt[]" /></td>
<td><input type="text" name="y[]"/></td>
<td><input type="text" name="x[]" /></td>

you should be able to retrieve arrays in $_POST or $_GET (depending on your form's method) that contains all the values. For instance:

print_r($_GET['ply']);

would print all your ply input values.

Rorchackh
  • 2,113
  • 5
  • 22
  • 38
  • If you looked at to my example you can see this is what I'm doing currently. Also can you explain how many input box should be present in that form? – tuze Sep 24 '12 at 11:04
  • I'm not sure I fully understand your question, but if you're asking about the *number* of input boxes, you can have as many as you like. Then you would handle them with a foreach loop in your PHP file. The accepted answer here may give you an idea: http://stackoverflow.com/questions/2433727/submitting-a-multidimensional-array-via-post-with-php – Rorchackh Sep 24 '12 at 11:22
  • Trying to find a more handy way. – tuze Sep 24 '12 at 11:54