// Please take a look at these codes
<form action="<? $PHP_SELF; ?>" method="post">
Name<input type="text" name="name">
Message<textarea name="entry"></textarea>
<input type="submit" value="Submit">
</form>
<?
$data_file = "data.txt";
$name = $POST['name'];
$entry = $POST["entry"];
$fp = fopen($data_file,"a") or die ("Error opening");
fwrite($fp,$name.$entry) or die ("Error writing"); // Here is where the error come out due to $name or $entry is blank (no input) on page load
fclose($fp);
include($data_file);
?>
There's something wrong with the above codes:
Whenever I use $PHP_SELF as action in a html form, I get error with the above codes because when the page is first loaded, the $name & $entry fields are empty, so it automatically "die" at >> fwrite($fp,$name.$entry) or die ("Error writing");
So far I can only think of putting:
if ($name == "") || ($entry == "")) {
// do nothing
} else {
// then execute codes
}
which I don't think this is the correct way to do this... are there any other ways to fix this?
I don't want to create a html page to hold the forms, and another php page to hold the php codes.