我有一个包含一些数据的文本文件,我将其传递给表格中的一些输入
<!doctype html>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<form action="edit_subjects.php" method="post">
<?php
$content = file('../db/test.txt');
foreach($content as $eachLine) {
list($subject, $type, $description) = explode('|', $eachLine);
echo '<label for="subject">Subject:</label>';
echo '<input type="text" name="subject" id="subject" value="'.$subject.'"><br>';
echo '<label for="type">Type:</label>';
echo '<input type="text" name="type" id="type" value="'.$type.'"><br>';
echo '<label for="description">Description:</label><br>';
echo '<textarea name="description" id="description">'.$description.'</textarea><br>';
}
?>
<input type="submit">
</form>
</body>
</html>
运行代码会输出:
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="Subject 1"><br>
<label for="type">Type:</label>
<input type="text" name="type" id="type" value="Type1"><br>
<label for="description">Description:</label><br>
<textarea name="description" id="description">Description1
</textarea><br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="Subject 2"><br>
<label for="type">Type:</label>
<input type="text" name="type" id="type" value="Type2"><br>
<label for="description">Description:</label><br>
<textarea name="description" id="description">Description2
</textarea><br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="Subject 3"><br>
<label for="type">Type:</label>
<input type="text" name="type" id="type" value="Type3"><br>
<label for="description">Description:</label><br>
<textarea name="description" id="description">Description3
</textarea><br>
正如您所看到的那样,名称会一遍又一遍地重复出现,那么我将如何单独访问每组输入并将其写入文件
答案 0 :(得分:0)
在文件edit_subjects.php中,您必须使用$ _POST ['subject']读取传递的参数,然后以正确格式($ result)的字符串连接变量并将其写回以下代码:
$fp = fopen("../db/test.txt", "r+");
if (flock($fp, LOCK_EX)) {
ftruncate($fp, 0);
fwrite($fp, $result);
fflush($fp);
flock($fp, LOCK_UN);
} else {
echo "Couldn't get the lock!";
}
fclose($fp);
答案 1 :(得分:0)
以下是edit_subjects.php的一个解决方案:
如果要在这样的循环中打印表单项,然后在提交表单时发送它们,则需要更改每个循环上的name=
值。你可以像这样使用和增量数字:
$i = 1;
foreach($content as $eachLine) {
list($subject, $type, $description) = explode('|', $eachLine);
echo '<label for="subject">Subject:</label>';
echo '<input type="text" name="subject' . $i .'" id="subject" value="'.$subject.'">br>';
echo '<label for="type">Type:</label>';
echo '<input type="text" name="type' . $i .'" id="type" value="'.$type.'"><br>';
echo '<label for="description">Description:</label><br>';
echo '<textarea name="description' . $i .'" id="description">'.$description.'</textarea><br>';
$i++
}
或您可以将表单name
转换为数组:
echo '<input type="text" name="subject[]" id="subject" value="'.$subject.'">
这将要求您在edit_subjects.php中循环它们以获取值。
然后,从$ _POST数组中访问您的表单值(请记住,您应该转义所有值):
if (isset($_POST['subject'])
{
$subject = $_POST['subject'];
$type = $_POST['type'];
//your other form values here...
$result = $subject . "," . $type //or however you want to structure it
$file = fopen("../db/test.txt", "w");
fwrite($file, $result);
} else {
echo "POST variables not set!";
die;
}
如果使用数组,则访问POST值的示例是:
if (isset($_POST['subject'])
{
$file = fopen("../db/test.txt", "w");
$i = 0;
foreach($_POST['subject'] as $array)
{
$result = $_POST['subject'][$i] . "," . $_POST['type'][$i] ."\r\n";
fwrite($file, $result);
$i++
}
} else {
echo "POST variables not set!";
die;
}