質問

I've got a PHP page that reads a text file from the server and loads some content on the page based on the contents of that file. Then, when the user clicks a submit button, content is rewritten to the text file and the page is reloaded.

However, now, the contents loaded to the page are not correct based on what I just selected. Though, if I hit F5 and reload the page, then the page loads with all correct content as I would have expected. It's like the page has to be refreshed a second time because the first time the text file is just being rewritten.

Is there anyway to correct this so that the page content loads correctly based on what the user selects and is then rewritten to the text file?

I'm writing the file using the following, at the very bottom of my php code:

echo "<form action='' method='post' enctype='multipart/form-data' name='saveChoices' id='saveChoices'>";
$userChoiceListWritable = fopen($userChoicesFile, 'w') or die("Couldn't open user's choice list file for writing.");

if (array_key_exists('save', $_POST)) 
{
    if(!empty($_POST['choiceCheckGroup'])) 
    {
        foreach($_POST['choiceCheckGroup'] as $check)
        {
            fwrite($userChoiceListWritable , $check."\n");
        }
    }
    fclose($userChoiceListWritable);
}
役に立ちましたか?

解決

you should write the file before the content of the file is output, not after. Put your file writing code at the top of your script since it is all in one file.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top