【发布时间】:2015-11-26 22:55:04
【问题描述】:
我在写入文本文件时遇到问题。用户在表单中的一个或多个复选框中单击的值。完成后,它应该将其打印到文本文件中。因此,如果用户标记肉类和苹果,它应该将其打印到文本文件 shoppingcart.txt 中。
我对 html、css 和 php 非常陌生。该代码低于我目前所取得的成果。
<article class="article">
<p>What in the store and their prices</p>
<?php
foreach($file as $line) {
echo $line;
}
?>
<p> Below you can order some things!</p>
<form method="post" action="">
<input type="checkbox" name="cb" value="meat"<br>
<input type="checkbox" name="cb" value="apple"<br>
<input type="checkbox" name="cb" value="drinks"<br>
<input type="submit" value="Submit">
<?php
$myfile = fopen("shoppingcart.txt", "w") or die("Unable to open file");
fwrite($myfile
?>
</form>
我应该在表格下方的 php 部分写什么?
【问题讨论】:
-
看php.net/manual/en/function.file-put-contents.php。还要注意标志
FILE_APPEND | LOCK_EX。 -
在进一步使用脚本之前,您可能想了解一些关于 php 中的表单处理的知识。
-
您可能也想为此使用
SESSIONS,而不是文本文件。如果 2 位用户同时在您的网站上,而您写信给shoppingcart.txt,您将无法分辨谁想要什么。 -
类似问题:也许有趣? :Store clicks inside .txt/.php file on the server