您能否帮我将表格中的选定值写入带有ID的文件中。我有html表格:
<form name="frm1" method="post" action="form_to_write.php">
<h4>q1</h4>
<input type="radio" name="answer[1]" value="someValue1" />someValue1<br />
<input type="radio" name="answer[1]" value="someValue2" />someValue2<br />
<input type="radio" name="answer[1]" value="someValue3" />someValue3
<h4>q2</h4>
<input type="radio" name="answer[3]" value="someValue4" />someValue4<br />
<input type="radio" name="answer[3]" value="someValue5" />someValue5<br />
<input type="radio" name="answer[3]" value="someValue6" />someValue6
<h4>q3</h4>
<input type="radio" name="answer[2]" value="someValue9" />someValue9<br />
<input type="radio" name="answer[2]" value="someValue7" />someValue7<br />
<input type="radio" name="answer[2]" value="someValue8" />someValue8
<input type="submit" value="submit" name="submit"/>
</form>
我的档案form_to_write.php
是:
$array = array_map("rtrim", $_POST['answer']);
$str = implode("\r\n", $array);
$date="date_not_work"; //date('m-d-y_H:i:s')
$fp = fopen($date."_out.txt", "w+");
$write=fwrite($fp, $str);
fclose($fp);
if ($write) echo 'Write is done!';
else echo 'Error!';
我需要像这样的文件结果:
1|value of 1
3|value of 3
2|value of 2
现在它只写了值。
答案 0 :(得分:3)
$str = '';
foreach ($_POST['answer'] as $num => $answer) {
$str .= "$num|$answer\r\n"; //i see no point in doing rtrim here
}
而不是
$array = array_map("rtrim", $_POST['answer']);
$str = implode("\r\n", $array);
至于日期,可能是因为:
符号。
将其设为"y-m-d_H-i-s"
格式
注意y-m-d顺序。这样更可靠,因为它使您的文件按日期排序