I have this very simple code of introducing data from input form into a flat file: <?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); } ?> PHP: 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: <html> ............ <?php $file = fopen("data.txt", "r"); $data = fread($file, filesize("data.txt")); fclose($file); echo $data; ?> ............ </html> PHP:
instead of: if (isset($_POST['submit'])) { PHP: use if (!empty($_POST['name1']) && !empty($_POST['name2'])) { .. } PHP: ?
It does not work. I tried it without changing anything else, and also by making other changes after using this bit of code, but with no result...
Err then you're trying to do something else? If you changed the code correctly, it should only save the file if name1 and name2 have both been filled in. That is what you wanted, right?
May be you missed the concept of the solution given by premium. You have to check whether the user has introduced input in the fields you consider mandatory for creating the file. Your sample script has two fields (name1 and name2). The solution proposed won't create the file if both fields are empty. Of course you have to translate that concept to the actual fields in your form. And, not all kind of fields are checked the same way. You have to decide which fields are mandatory and which aren't.
Now this part works, I used this code: <?php if (isset($_POST['submit'])) { $name1 = $_POST['name1']; $name2 = $_POST['name2']; $fp = fopen("some.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"); } if(empty($_POST['name1'])) { fwrite($fp, "Something that will be output on the outputphp if there is no input on name1"."\n"); } fclose($fp); ?> PHP: (As I can have empty the second input field). But how I solve that on the output page even if the "some.txt" file does not exist yet, I will have echoed the same "Something that will be output on the outputphp if there is no input on name1"?
You have severe misconceptions about PHP. You have to start practising with less complex code. Then, start working on what you are trying to don now.
Yeah, go and read the sticky in this forum. You do not have enough knowledge of PHP. Ofcourse someone could type a solution here, but I think it's best if you learn this stuff yourself..
Don't take it personally. But you need to learn some basic concepts before trying to do this kind of code. You need to learn how PHP manages HTML output. You have misused the if control structure. It doesn't work as you want it to work because misplaced it. Check how if control construct works. You will need to know how HTML forms values are handle on submission. PHP documentation is translated to several other languages too. The official documentation root is at: http://www.php.net/docs.php