简体   繁体   中英

I don't get why this isn't creating a text file

I'm suppose to create a short quiz and when you hit submit it takes you to a results page that shows the person's name, score, correct answers to the questions, time, and date and it also saves that data and keeps building the history of people who took the test. Here's what I have and I just don't know why it isn't working. I'm really new to PHP so I know I'm probably doing something stupid.

<!DOCTYPE html>
<html lang="en">    
<head>
    <meta charset="utf-8">
    <title>Homework 1CD: Quiz</title>
    <link rel="stylesheet" type="text/css" href="homework1a.css" />
</head>
<body class="berry">
    <h1>Quiz!</h1>
    <form action = "homework1cd.php" method = "POST">
        <p>
        <label for="name">Your Name:</label>
        <input type="text" id="name" name="fname">
        </p>
        <p>
        <label for="dropdown">How many toes does the average cat have?
        </label>
        <br>
            <select id="dropdown" name="question1">
                <option value="A" name="question1">18</option>
                <option value="B" name="question1">24</option>
                <option value="C" name="question1">56</option>
                <option value="D" name="question1">20</option>
            </select>
        </p>
        
        <p>Cats are the only mammals who don't taste sweetness. True/False
        <br>
            <input type="radio" id="radio" name="question2" value="A">
                <label for="question2>A">True</label>
                <br>
            <input type="radio" id="radio" name="question2" value="B">
                <label for="question2>B">False</label>
        
        </p>
        
        <p>
        <label for="checkbox" name="question3">Check all that are breeds of cat:</label>
        <br>
            <input type="checkbox" id="checkbox" name="question3" value="A">
            <label for="question3>A">BooBoo</label>
            <br>
            <input type="checkbox" id="checkbox" name="question3" value="B">
            <label for="question3>B">Fluffy Bottom</label>
            <input type="checkbox" id="checkbox" name="question3" value="C">
            <label for="question3>C">Lil Cutie</label>
            <input type="checkbox" id="checkbox" name="question3" value="D">
            <label for="question3>D">Sphynx</label>
        </p>
        
        <p>Cats are farsighted. True/False
        <br>
            <input type="radio" id="radio" name="question4" value="A">
                <label for="question4>A">True</label>
                <br>
            <input type="radio" id="radio" name="question4" value="B">
                <label for="question4>B">False</label>
        </p>
        <p>
        <label for="dropdown">How many times their own body length can cats jump?</label>
        <br>
            <select id="dropdown" name="question5">
                <option value="A" name="question5">10x</option>
                <option value="B" name="question5">6x</option>
                <option value="C" name="question5">2x</option>
                <option value="D" name="question5">100x</option>
            </select>
        </p>
        <p class="submit">
            <input type="submit" value="submit" name="submit">
        </p>
    </form>
    
<?php
//date and time
echo "<p>Today is " . date("m/d/y. "), "</p>";
echo "<p>The time is " . date("h:i."), "</p>";

//get the person's answers
$answer1 = $_POST['question1'];
$answer2 = $_POST['question2'];
$answer3 = $_POST['question3'];
$answer4 = $_POST['question4'];
$answer5 = $_POST['question5'];

//set up for correct answers
$correct_answer = 0;

//count the correctly answered questions 
if ($answer1 == "A") { $correct_answer++; }
if ($answer2 == "A") { $correct_answer++; }
if ($answer3 == "D") { $correct_answer++; }
if ($answer4 == "A") { $correct_answer++; }
if ($answer5 == "B") { $correct_answer++; }

//create variable for name, date, and time
if ($_POST['fname']){
    $name = $_POST['fname'];
    $text = $name . ";";
    $date = $_POST['date'];
    $time = $_POST['time']; 
}

//write txt file for stored results
$myfile = fopen("results.txt", "a");

fwrite($myfile, $name);
fwrite($myfile, $correct_answer);
fwrite($myfile, $answer1);
fwrite($myfile, $answer2);
fwrite($myfile, $answer3);
fwrite($myfile, $answer4);
fwrite($myfile, $answer5);
fwrite($myfile, $time);
fwrite($myfile, $date);
fclose("results.txt");
?>

</body>
</html>
    
<!doctype html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>results</title>
</head>
<body>

<table border=1>
    <tr>
        <th name="fname">Name</th>
        <th name="correct_answer">Score</th>
        <th name="answer1">Question1</th>
        <th name="answer2">Question2</th>
        <th name="answer3">Question3</th>
        <th name="answer4">Question4</th>
        <th name="answer5">Question5</th>
        <th name="time">Time</th>
        <th name="date">Date</th>
    </tr>
</table>
<?php
//call to get file contents
$contents = file_get_contents ("results.txt");
$resultset = explode(";", $contents);
foreach($resultset as $name) {
    echo $name;
    echo $correct_answer;
    echo $answer1;
    echo $answer2;
    echo $answer3; 
    echo $answer4;
    echo $answer5;
    echo $time;
    echo $date;
}
?>

</body>
</html>

Looking at PHP's fopen function it should return false upon error, and produce a warning.

You can check the return value directly in the code:

$myfile = fopen("results.txt", "a");
if (!$myfile) {
  echo("file could not be created");
}

But to check the exact error message, you have to look at the webpage you created to check for a warning logs, or in your server's logs.

It is possible that the server does not allow files to be created by HTTP clients. Make sure to write the file to a safe directory, with the proper permissions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM