Php 文本在被读取时不会改变

Php 文本在被读取时不会改变,php,Php,我正在尝试创建一个交互式程序,其中用户使用文本文件通过文本框提问。但我还处于非常早期的阶段,我可以创建两行文本文件,其中一行是问题和答案。我有两个php文件。其中一个创建文本文件。另一个应该读取并显示点击时它会显示在屏幕上,因为form action=“questions.php”。但由于某些原因,文本显示但不更改 j、 php文件1它正在尝试创建文本文件 <?php if(isset($_POST['submit'])) { $question1 = $_POST['name']

我正在尝试创建一个交互式程序,其中用户使用文本文件通过文本框提问。但我还处于非常早期的阶段,我可以创建两行文本文件,其中一行是问题和答案。我有两个php文件。其中一个创建文本文件。另一个应该读取并显示点击时它会显示在屏幕上,因为form action=“questions.php”。但由于某些原因,文本显示但不更改

j、 php文件1它正在尝试创建文本文件

<?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");

      }

else{
echo
'<center>
 <form action = "questions.php"  method = "post">
  Question you want ask the person <input  type  = "text" name="name"> <<br>
    Answer<input type = "text" name = "age"><br>
    <input type = "submit" name = "submit" value = "Make Question">
    </form>
    </center>';}
?>

预期输出它生成文件:question.txt

questions.php文件2,用于读取和显示新写入的数据

<?php


$myfile = file_get_contents ("question.txt");
echo $myfile;

?>


预期输出应该读取新文本

根据更新的问题,您有2个文件:j.php和questions.php

  • j、 php负责显示表单,也负责处理表单
  • 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">
...

文件编写器
问题:

答复:

该文件的内容包括:
根据更新的问题,您有两个文件:j.php和questions.php

  • j、 php负责显示表单,也负责处理表单
  • 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">
...

文件编写器
问题:

答复:

该文件的内容包括:
这没有什么区别,但是为什么要使用
w+
模式而不是
w
?我看到很多人在打开文件时使用
+
,但似乎没有理由这样做。这仅仅是因为用户不明白它的意思而养成的一个坏习惯吗?为什么不在第一个脚本中使用
file\u put\u contents()
?浏览器可能正在缓存第二个脚本的结果,请尝试在URL中使用cachebuster。@Bamar我不知道我在使用chrome浏览器cachebuster与浏览器无关,它是您添加到URL的随机参数。谷歌搜索它。这没什么区别,但是为什么你用
w+
模式而不是
w
?我看到很多人在打开文件时使用
+
,但似乎没有理由这样做。这仅仅是因为用户不明白它的意思而养成的一个坏习惯吗?为什么不在第一个脚本中使用
file\u put\u contents()
?浏览器可能正在缓存第二个脚本的结果,请尝试在URL中使用cachebuster。@Bamar我不知道我在使用chrome浏览器cachebuster与浏览器无关,它是您添加到URL的随机参数。谷歌。