下面的代码是一个PHP页面,功能被我简化出来了,就是读取写入数据到TXT里,现在有一个问题,就是新打开页面时,这个写入的操作也会被执行一遍,于是TXT就被写入了空数据,我想要新打开页面时不要执行写入操作,只在提交时才执行写入操作。请问各路大神如何解决?
代码如下
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input name="AN" value="A"/>
<input name="AR" value="1" type="checkbox"/>
<input name="AM" class="RM"/>
<input type="submit" name="submit">
</form>
<?php
$myfile = fopen("2.txt", "w");
$n = $_POST["AN"];
$r = $_POST["AR"];if(!$r){$r=0;};
$m = $_POST["AM"];if(!$m){$m=0;};
fwrite($myfile, $n);
fwrite($myfile, ",");
fwrite($myfile, $r);
fwrite($myfile, ",");
fwrite($myfile, $m);
fwrite($myfile, ",\r\n");
fclose($myfile);
echo $n;
echo $r;
echo $m;
?>
</body>
</html>
代码如下
<!DOCTYPE html>
<html>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
<input name="AN" value="A"/>
<input name="AR" value="1" type="checkbox"/>
<input name="AM" class="RM"/>
<input type="submit" name="submit">
</form>
<?php
$myfile = fopen("2.txt", "w");
$n = $_POST["AN"];
$r = $_POST["AR"];if(!$r){$r=0;};
$m = $_POST["AM"];if(!$m){$m=0;};
fwrite($myfile, $n);
fwrite($myfile, ",");
fwrite($myfile, $r);
fwrite($myfile, ",");
fwrite($myfile, $m);
fwrite($myfile, ",\r\n");
fclose($myfile);
echo $n;
echo $r;
echo $m;
?>
</body>
</html>