我有一個 PHP 腳本,它允許最終用戶使用動態表單選擇他/她需要的條目數。1 組資料為 3 個條目。每個條目將包含自己的欄位。例如,如果他/她需要 4 組資料,最終用戶將在下拉串列中選擇 4,PHP 腳本將動態顯示 4 行空白表單,每行包含 3 個欄位(因此,一個 4x3 矩陣,有 12 個欄位)。
然后腳本必須將從 12 欄位收集的資料寫入文本檔案。我正在努力將表單中收集的資料傳遞給實際寫入 txt 檔案的 PHP 腳本。
這是主頁: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>';
?>
以及寫入 txt 檔案 WSCRIPT.php 的 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";
}
}
}
?>
我遇到的錯誤顯示為:
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...
其中第 9 行是來自 WSCRIPT.php 的行,其中:
$data = $_POST['.$Matrix['.$a.']['.$b.'].'] . "\r\n";
任何人都可以指出我如何前進的正確方向......我是一名 PHP 初學者,并且在旅途中自學......
非常感謝!
于連
uj5u.com熱心網友回復:
命名表單輸入元素$Matrix = array(array());不是正確的方法。元素應該有一個使用陣列語法的正確名稱,例如banana[]etc,以便名稱在 POSTed 資料中可用。
我整理了一個單頁演示來說明如何實作將從動態生成的表單收集的資料寫入文本檔案的目標,其中各個文本欄位的名稱使用具有陣列語法的通用名稱。由于上面的代碼沒有顯示select用于創建初始顯示的選單,我使用了一些 javascript 和一個內容模板,如下所示。
<?php
error_reporting( E_ALL );
if( $_SERVER['REQUEST_METHOD']=='POST' && isset( $_POST['matrix'] ) ){
/**************************
WSCRIPT.php emulator
*/
/* Split the matrix data into chunks of 3 ( 3 inputs per table row! ) */
$chunks=array_chunk( $_POST['matrix'], 3 );
$file='wscript.txt';
@unlink( $file );
foreach( $chunks as $trio ){
/*
Each row as shown in the HTML table will be written to it's own line in the text file.
The individual values are separated with the pipe character here - but could obviously be
formatted completely differently.
*/
file_put_contents( $file, implode( ' | ', $trio ) . PHP_EOL, FILE_APPEND | LOCK_EX );
}
exit( sprintf( '%s bytes written to file',filesize( $file ) ) );
}
?>
<!DOCTYPE html>
<html lang='en'>
<head>
<meta charset='utf-8' />
<title>write dynamically generated form element content to text file</title>
<style>
:root{ counter-reset:rows; }
tbody tr{ counter-increment:rows; }
tr td:first-of-type:before{ content:counter(rows) }
</style>
</head>
<body>
<!--
dropdown to select the number of rows the user
wishes to add to the table.
Data added by javascript.
-->
<select name='qty'>
<option selected hidden disabled>Please select
</select>
<!--
action='WSCRIPT.php'
~ removed for single page demo where
php acts as wscript.php
-->
<form method='POST'>
<table>
<colgroup>
<col style='width:5%'>
<col style='width:20%'>
<col style='width:10%'>
<col style='width:10%'>
</colgroup>
<thead>
<tr>
<th>#</th>
<th>Item / Description</th>
<th>Start date</th>
<th>End date</th>
</tr>
</thead>
<tbody>
<!-- dynamic content added here -->
</tbody>
</table>
<input type='submit' value='Save Data' />
</form>
<!--
a simple template holding the new table row and 3 input fields
to be added in whatever quantity is selected from the dropdown.
The name of the element uses the array syntax but importantly
it is a proper name rather than a PHP reference to an array!
The name `matrix` will appear in the POST array when the form
is submitted after the user has added whatever content they
need to.
-->
<template>
<tr>
<td> </td>
<td><input type='text' name='matrix[]' /></td>
<td><input type='text' name='matrix[]' /></td>
<td><input type='text' name='matrix[]' /></td>
</tr>
</template>
<script>
let oSel=document.querySelector('select[name="qty"]');
let tbl=document.querySelector('form > table > tbody');
let tmpl=document.querySelector('template');
/* add new options to the select menu */
for( let i=1; i <=25; i )oSel.append(new Option(i,i));
/* add the event listener that clones the template N times and adds to the table */
oSel.addEventListener('change',function(e){
tbl.innerHTML='';
for( let i=0; i < this.value; i ){
let tr=tmpl.content.cloneNode( true );
tbl.append( tr );
/**********************************************
add some junk data to each input element
- only because I'm lazy
*/
document.querySelectorAll('td input').forEach((n,j)=>{
n.value=[
`cell:${j 1}`,
window.URL.createObjectURL(new Blob([])).split('/').pop().substr(0,16)
].join( ' ' );
})
}
})
</script>
</body>
</html>
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/373509.html
下一篇:Pythonnmap不顯示主機
