1 回答
TA贡献1829条经验 获得超7个赞
根据更新的问题,您有 2 个文件:j.php和questions.php。
j.php负责显示表单,也负责处理表单。
questions.php负责显示表单的内容。
由于您的表单发布到questions.php,您跳过了实际处理表单。只需将表单的操作更改为:action="j.php"然后在处理后重定向到questions.php。
<?php
if(isset($_POST['submit'])) {
$question1 = $_POST['name'];
$question2 = $_POST['age'];
$file = fopen( "question.txt", "w+" ) or die( "file not open" );
$s = $question1 . "," . $question2 . "\n";
fputs($file, $s)or die("Data not written");
// Here - redirect to questions.php to display the contents.
header('Location: questions.php');
die;
}
<!-- You want to post this form to the processing file. -->
<form action="j.php" method="post">
...
或者,只需在一个文件中完成所有操作。
<?php
if (isset($_POST['submit'])) {
$question1 = $_POST['name'];
$question2 = $_POST['age'];
$file = fopen("question.txt", "w+") or die("file not open");
$s = $question1 . "," . $question2 . "\n";
fputs($file, $s) or die("Data not written");
header('Location: '.$_SERVER['PHP_SELF']);
die;
}
?>
<h1>File Writer</h1>
<form method="post">
<label>
Question:
<input type="text" name="name">
</label><br>
<label>Answer:
<input type="text" name="age">
</label><br>
<input type="submit" name="submit" value="Make Question">
</form>
<hr>
The contents of the file are:
<!-- Suppressing a warning here with @. You shouldn't do this. -->
<?php echo @file_get_contents ("question.txt"); ?>
- 1 回答
- 0 关注
- 148 浏览
添加回答
举报