Home>

Receiving string data entered in PHP with PHP and saving it to a new text file
(POST transmission&reception + text storage + conditional branching)
Please write the details of the question here.

The following error message occurred while implementing a function.

Error message

Post sending&receiving, text saving and conditional branching are not working
Cannot display in .txt

Notice: Undefined index: comment in /public_html/mission_2-1/mission_2-2.php on line 19
Applicable source code





mission_2-2




date_default_timezone_set ('Asia/Tokyo');
if ($_ POST ['comment'])
{echo 'Thanks for your input.
';
echo date ("Y/m/d H: i: s"). Accepted '. $_ POST [' comment '].' for "\ n". ' ';
}

$filename = 'mission_2-2.txt';
$fp = fopen ($filename, 'w');
if (! empty ($_ POST ['comment'] == 'comment')) {
fwrite ($fp, $_ POST ['comment']);
} elseif ($_ POST ['comment'] == 'Completed') {
echo 'Congratulations!';
}
fclose ($fp);

?>


Source code

I tried various ways of writing, but POST sending&receiving from the form + text saving + conditional branching is not working.
I do n’t know what ’s wrong.
Please state what you tried for the problem here.

Supplemental information (FW/tool version etc.)

Please provide more detailed information here.

php
  • Answer # 1

    This is the flow


    ';     echo date ("Y/m/d H: i: s"). Accepted '.htmlspecialchars ($comment).' for "\ n". ' ';   } } ?>

  • Answer # 2

    Is the "corresponding source code" displayed in the browser, input the input form, and is it a POST destination file?

    When this source code is first displayed in a browser, the $_POST variable is empty.
    However, since "if ($_ POST ['comment'])" is decided, the message "undefined index" is displayed.

    It is necessary to distinguish between the first display (GET) and the transmission of some data (POST).

    This is a process of carving out, but PHP has an array called "$_SERVER".

    Does $_SERVER ['REQUEST_METHOD'] exist

    Is $_SERVER ['REQUEST_METHOD'] value "POST"

    If you check

    , you will be able to determine whether the access from the browser is GET or POST.

    For other articles, see here for GET and POST.
    [Introduction to PHP] About HTTP (GET/POST)
    About the difference between GET and POST

Trends