John Davidson

PHP: how to pass a variable as a multidimensional array with method=POST

0 comments
Message:


I have a PHP script that allows end-users, using a dynamic form, to select how many entry he/she needs.
1 set of data is 3 entries. Each entry will contain its own field. For example, if he/she needs 4 set of data, the end-user will select 4 in a drop down list and the PHP script will dynamically display a blank form with 4 lines, each line containing 3 fields (so, a 4x3 matrix, with 12 fields).


Then the script has to write the data collected from the 12 field into a text file. I am struggling to pass the data collected in the form, to the PHP script that will actually write into the txt file.


Here is the main page: MAIN.php:


<?php
$Matrix = array(array());
$data = $name*3; //1 line entry contains 3 columns of data; total $name line entry
$block=null;
$entry=null;
for ($n = 0; $n <= $data-1; $n+=1) {
$block=fmod($n,$name);
$entry=($n-$block)/$name;
$block=$block+1; //line (horizontal)
$entry=$entry+1; //column (vertical); 3 in this case
}
for ($a = 0; $a <= $block-1; $a+=1) { //line (horizontal)
for ($b = 0; $b <= $entry-1; $b+=1) { //column (vertical)
$Matrix[$a][$b]=null;
}
}
echo '
<form action="WSCRIPT.php" method="POST">
<table style="width:40%">
<col style="width:5%">
<col style="width:20%">
<col style="width:10%">
<col style="width:10%">
<tr>
<td><center>#</center></td>
<td><center>Item / Description</center></td>
<td><center>Start date</center></td>
<td><center>End date</center></td>
</tr>';
echo '<tr>';
for ($a = 0; $a <= $block-1; $a+=1) { //line (horizontal)
$line=$a+1;
echo '<td><center>'.$line.'</center></td>';
for ($b = 0; $b <= $entry-1; $b+=1) { //column (vertical)
echo '<td><center><input name="'.$Matrix[$a][$b].'" type="text" /></center></td>';
}
echo '</tr>';
}
echo '
</table>
<br>
<input type="submit" name="submit" value="Save Data">
</form>';
?>

And the PHP script writing to the txt file, WSCRIPT.php:


<?php
$block = 10; //max
$entry = 3; //constant
for ($a = 0; $a <= $block-1; $a+=1) { //line (horizontal)
for ($b = 0; $b <= $entry-1; $b+=1) { //column (vertical)
$data = $_POST['.$Matrix['.$a.']['.$b.'].'] . "\r\n";
$ret = file_put_contents('WSCRIPT.txt', $data, FILE_APPEND | LOCK_EX);
if($ret === false) {
die('Error writing this file');
}
else {
echo "$ret bytes written to file";
}
}
}
?>

The error I'm having is showing up as:


Notice: Undefined index: .$Matrix[0][0]. in C:\TEST\WSCRIPT.php on line 9
Notice: Undefined index: .$Matrix[0][1]. in C:\TEST\WSCRIPT.php on line 9
Notice: Undefined index: .$Matrix[0][2]. in C:\TEST\WSCRIPT.php on line 9
Notice: Undefined index: .$Matrix[1][0]. in C:\TEST\WSCRIPT.php on line 9
etc...

where line 9 is the line from the WSCRIPT.php, with:


$data = $_POST['.$Matrix['.$a.']['.$b.'].'] . "\r\n";

Can anybody point me in the right direction on how to move forward... I'm a PHP beginner and self-learning on the go...


Thank you very much!


Julien



My Answer:





And here is the process.php file:
$_POST['field1'][$i],
'field2' => $_POST['field2'][$i],
'field3' => $_POST['field3'][$i]
);
$data[] = $entry;
}
$file = fopen("data.txt", "w");
foreach ($data as $entry) {
fwrite($file, implode(",", $entry) . "\n");
}
fclose($file);
}
?>
Make sure to adjust the field names in the process.php file to match the names of the fields in your form. This code assumes that the fields in the form are named 'field1', 'field2', and 'field3'.

Rate this post

3 of 5 based on 9707 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us