我被困在我有一个PHP任务的问题。我创建了一个PHP脚本来生成表单输入,并且我必须从输入中获取数据并将其插入到文本文档中。所以我编写了生成两个输入的代码和一个将数据写入文本文档的函数。但是,我的文本文档显示为空白。我如何正确定义一个从文本输入中获取多个值并将其插入到文本文档中的函数?这是我到目前为止的代码:从PHP生成的输入插入到文本文件
<form method='post'>
<table border='0' cellpadding='3' cellspacing='5'>
<tr>
<td><h2>Item</h2></td>
<td><h2>Amount</h2></td>
</tr>
<?php
for($row=1; $row<5;$row++)
{
$item_name='item_name'.$row;
$item_value = $_POST[$item_name];
$amount_name='amount_name'.$row;
$amount_value = $_POST[$amount_name];
echo
"<tr>
<td><input type='text' name='$item_name' value='$item_value' /></td>
<td><input type='text' name='$amount_name' value='$amount_value' /></td>
";
if (!empty($amount_value))
{
if(is_numeric($amount_value))
{
$total = $total + $amount_value;
}
else {
echo
"<td class='bad'>Amount Invalid: $amount_value</td>";
$error_count++;
}
}
echo"
</tr>
";
}
?>
<tr>
<td colspan='2'>
<input name='' type='submit' value='Submit' />
</td>
</table>
<?php
if ($error_count > 0)
{
echo "<br /><span class='bad' />Errors: $error_count</span>";
}
else
{
echo "<br />Total: $total";
}
if ($error_count == 0)
{
addData();
}
//Function
function addData()
{
$myFile = "bills.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $amount_value;
fwrite($fh, $stringData);
$stringData = $item_value;
fwrite($fh, $stringData);
fclose($fh);
}
?>
</form>
在脚本的顶部添加'error_reporting(E_ALL);'。然后考虑添加功能参数。 – mario