I am stuck on a problem for a PHP assignment I have. I created a PHP script to generate form inputs, and I must take the data from the input and insert it into a text document. So I wrote the code that generates two inputs and a function that will write the data to the text document. However, my text document appears blank. How would I properly define a function that takes multiple values from text inputs and inserts them into a text document? Here is my code so far:
<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);on top of your script. Then consider adding function parameters.