tuze
tuze

Reputation: 1978

Custom Number Of Inputs

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/

Upvotes: 1

Views: 95

Answers (2)

WeaklyTyped
WeaklyTyped

Reputation: 1341

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.

Upvotes: 1

Rorchackh
Rorchackh

Reputation: 2181

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.

Upvotes: 0

Related Questions