猿问

阅读时文本不会改变

我正在尝试创建一个交互式程序,用户可以在其中使用文本文件使用文本框提出问题。有两个 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 file2 应该读取并显示新写入的数据。


<?php



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

echo $myfile;


?>

预期输出它应该阅读新文本


浮云间
浏览 149回答 1
1回答

千巷猫影

根据更新的问题,您有 2 个文件:j.php和questions.php。j.php负责显示表单,也负责处理表单。questions.php负责显示表单的内容。由于您的表单发布到questions.php,您跳过了实际处理表单。只需将表单的操作更改为:action="j.php"然后在处理后重定向到questions.php。<?phpif(isset($_POST['submit'])) {&nbsp; &nbsp; $question1 = $_POST['name'];&nbsp; &nbsp; $question2 = $_POST['age'];&nbsp; &nbsp; $file = fopen( "question.txt", "w+" ) or die( "file not open" );&nbsp; &nbsp; $s = $question1 . "," . $question2 . "\n";&nbsp; &nbsp; fputs($file, $s)or die("Data not written");&nbsp; &nbsp; // Here - redirect to questions.php to display the contents.&nbsp; &nbsp; header('Location: questions.php');&nbsp; &nbsp; die;}<!-- You want to post this form to the processing file. --><form action="j.php" method="post">...或者,只需在一个文件中完成所有操作。<?phpif (isset($_POST['submit'])) {&nbsp; &nbsp; $question1 = $_POST['name'];&nbsp; &nbsp; $question2 = $_POST['age'];&nbsp; &nbsp; $file = fopen("question.txt", "w+") or die("file not open");&nbsp; &nbsp; $s = $question1 . "," . $question2 . "\n";&nbsp; &nbsp; fputs($file, $s) or die("Data not written");&nbsp; &nbsp; header('Location: '.$_SERVER['PHP_SELF']);&nbsp; &nbsp; die;}?><h1>File Writer</h1><form method="post">&nbsp; &nbsp; <label>&nbsp; &nbsp; &nbsp; &nbsp; Question:&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="name">&nbsp; &nbsp; </label><br>&nbsp; &nbsp; <label>Answer:&nbsp; &nbsp; &nbsp; &nbsp; <input type="text" name="age">&nbsp; &nbsp; </label><br>&nbsp; &nbsp; <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"); ?>
随时随地看视频慕课网APP
我要回答