Page 1 of 1

How can be echoed output page if there is no action on input

Posted: Mon Sep 07, 2009 5:06 pm
by phpnewbie3
Probably a very basic problem here, but looking over 1.5 hours by google search I have not find an answer... :banghead:
I have this very simple code of introducing data from input form into a flat file:

Code: Select all

<?php
 
if (isset($_POST['submit'])) {
 
$name1 = $_POST['name1'];
$name2 = $_POST['name2'];
 
$fp = fopen("data.txt","w");
 
if(!$fp) {
    echo 'Error, the file could not be opened or there was an error creating it.';
    exit;
}
 
fwrite($fp, "$name1"."<br/><br/>"."$name2"."\n");
 
fclose($fp);
}
 
?>
What needs to be introduced in the script above or in the output page script so that if we do not have action (no input made at all), so basically the flat file is not even created yet, a certain output php page to be shown (without outputting the error messages that there is no such data file...)?

Right now on the output page the data is shown using:

Code: Select all

<html>
<body>
 
...............
 
<?php
$file = fopen("data.txt", "r");
$data = fread($file, filesize("data.txt"));
fclose($file);
 
echo $data;
?>
 
...................
 
</body>
</html>

Re: How can be echoed output page if there is no action on input

Posted: Mon Sep 07, 2009 10:58 pm
by Ollie Saunders
Something like empty(trim($_POST['name1'])) yada yada ...