Scott Dawson
Scott Dawson

Reputation: 791

HTML PHP form with multiple fields

I have a html form with 5 rows and 30 fields. Note the attached image. image

I have to capture all of these fields in an array to later convert to a csv file. Each field is labelled at follows: qt1, fa1, en1, et1, ba1, qt2, fa2, etc... Instead of taking each row and adding them to an array in php one at a time is there an easy(ier) way to accomplish this?

I can easily take each row and add them to an array the issue with this i feel would be the speed of the script and the fact that i will have to write 30 lines of array data in the php script.

Upvotes: 0

Views: 1201

Answers (2)

Lajos Veres
Lajos Veres

Reputation: 13725

Name the input fields like input[i][j] and you can access them later in php like: $_REQUEST['input'][i][j] as a multidimensional array.

Upvotes: 0

Prusprus
Prusprus

Reputation: 8065

Is this what you are looking for?

<?php
for($i = 0; $i < 30; $i++): ?>
    <input name="qt[<?php echo $i; ?>]"/>
    <input name="fa[<?php echo $i; ?>]"/>
    <input name="en[<?php echo $i; ?>]"/>
    <input name="et[<?php echo $i; ?>]"/>
    <input name="ba[<?php echo $i; ?>]"/>
<?php
endfor;
?>

On the server side, you will receive all this data in your $_POST variable anyways, so it will all be in an array regardless. This has an advantage of saving all your values of the same row with the same index and all the same data types under one array.

Not sure about performance, but code readability and maintenance is alot easier like this.

Upvotes: 2

Related Questions