将数组写入带有 id 的文件?
你能帮我将表单中选定的值及其 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
现在它只写入值。
Can you help me to write selected values from a form to a file with their id. I have html form:
<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>
And my file form_to_write.php
is:
$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!';
I need result in file like this:
1|value of 1
3|value of 3
2|value of 2
Now it write only values.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
而不是
日期,可能是因为
:
符号。设为
“ym-d_H-is”
格式。请注意 ymd 顺序。这更可靠,因为它使您的文件可以按日期排序
instead of
as for the date, it's probably because of
:
symbols.make it
"y-m-d_H-i-s"
format.note the y-m-d order. this is way more reliable as it makes your files sortable by date