Home > Mobile >  Why my php form not working? HTTP ERROR: 500
Why my php form not working? HTTP ERROR: 500

Time:12-10

I'm making a simple website that uses php. The site shows form where you put text, then it appends it in a file and takes you to read that file. But it's throwing HTTP-ERROR: 500. I think that maybe the problem is that I'm referencing local files although the idea is to use site's file, in other words mysite.com/testfile.txt, but I don't know how.

There are the files:

index.html

<html>
<body>

<form action="welcome.php" method="post">
Text: <input type="text" name="text"><br>
<input type="submit">
</form>

</body>
</html>

welcome.php

<html>
<body>

<?php
$myfile = fopen("testfile.txt", "a")or die("Unable to open file!");

fwrite($myfile, $txt);
$txt = $_POST["text"]
fclose($myfile)
$lines = file('testfile.txt');

foreach ($lines as $line) {
  echo $line
}

?>
</body>
</html>

and empty testfile.txt

Thanks to anyone who can help.

CodePudding user response:

There are a couple issues with that code. First off, you're missing a few semi-colons.... and second, you're trying to use the $txt variable before you declare it. Try the following for your welcome page.

<html>
<body>

<?php
$myfile = fopen("testfile.txt", "a")or die("Unable to open file!");

$txt = $_POST["text"];
fwrite($myfile, $txt);
fclose($myfile);
$lines = file('testfile.txt');

foreach ($lines as $line) {
  echo $line;
}

?>
</body>
</html>

CodePudding user response:

You're missing semicolons at:

$txt = $_POST["text"]
fclose($myfile)

and

echo $line

Also, you're using $txt before definition.

CodePudding user response:

Add:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

at the start of your PHP file after the initial <?PHP tag.

And this should make life easier for you in the future if you make typos!

Please don't use the above in any public accessible websites - only to be used on private test server.

  • Related