I cannot save the form's content on file (on the local directory, where the HTML/PHP file is). Both the HTML and the PHP parts seem to work when "isolated". When I combine them (to be able to save what has been inserted), it does not work. Thank you for any suggestion, Pietro <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/> <title>Test</title> </head> <body> <form enctype="text/plain" method="POST" name="TestForm"/> <p /> Input anything: <input type="text" name="anything" value="Default"/> <br /> <input type="submit" value="OK" name="submit"/> </form> <?php $fh = fopen("form_output.txt", w); fwrite($fh, anything); fclose($fh); ?> </body> </html>
Well let's start with : you don't have a valid form (use the action ="form.php") second: use $anything not anything and third: use if ($submit=='OK') {then insert it} Code (markup): plus the $_POST['anything'] etc.
^^ Note that $submit only exists if register globals is enabled. $_POST['submit'] is the way to go. Oh, and there should be quotes around the W in the fopen() line. $fh = fopen("form_output.txt", 'w'); PHP:
maiahost wrote: Well let's start with : you don't have a valid form (use the action ="form.php") and third: use Code: if ($submit=='OK') {then insert it} PHP: Do I have to use action ="form.php" even if the php code is inside the html code in the same file? How? Where should I put the lines of code reported above? This is the modified code, which does not give me better results (i.e. I do not get any "form_output.txt" file): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/> <title>Test</title> </head> <body> <form enctype="text/plain" method="POST" name="TestForm"/> <p /> Input anything: <input type="text" name="anything" value="Default"/> <br /> <input type="submit" value="OK" name="submit"/> </form> <?php if ($submit=='OK') { $fh = fopen("form_output.txt", 'w'); fwrite($fh, $_POST['anything']); fclose($fh); } ?> </body> </html> HTML:
Can you make me an example on how to use the action attribute? It should be something like: action="filename.php" but what should I specify if the php code is in the html file itself?
The filename in the action attribute should be the file where you want to send the data to. It can be the page itself. If your page where the PHP code is called "filename.php", then set action to filename.php. You can also leave the attribute in blank, but it has to be there. <form action="" enctype="text/plain" method="POST" name="TestForm"/> HTML:
I tried this: <form action="" enctype="text/plain" method="POST" name="TestForm"/> HTML: and I still get nothing. And with this: <form action="targetFile.txt" enctype="text/plain" method="POST" name="TestForm"/> HTML: I get an error message if the file "targetFile.txt" does not exist, and nothing if it exist already (i.e. no error message, but the file is not touched either).
<form action="" enctype="text/plain" method="POST" name="TestForm"/> HTML: here is your problem you closed form tag in form start tag just remove slash
I removed the slash, but things do not work, yet. Can you try this code yourself, just to see what's wrong? <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/> <title>Test</title> </head> <body> <form enctype="text/plain" method="POST" name="TestForm" action="form_output.txt"> <p /> Input anything: <input type="text" name="anything" value="Default"/> <br /> <input type="submit" value="OK" name="submit"/> </form> <?php if ($_POST['submit']=='OK') { $fh = fopen("form_output.txt", 'w'); fwrite($fh, $_POST['anything']); fclose($fh); } ?> </body> </html> HTML: All I need is the form contents on a local file (in the same directory where the html/php file is)...
This works for me. I took out the encryption as well. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/> <title>Test</title> </head> <body> <form method="POST" name="TestForm" action=""> <p /> Input anything: <input type="text" name="anything" value="Default"/> <br /> <input type="submit" value="OK" name="submit"/> </form> <?php if ($_POST['submit']=='OK') { $fh = fopen("form_output.txt", 'w'); fwrite($fh, $_POST['anything']); fclose($fh); } ?> </body> </html> HTML:
Sorry about the short post yesterday but I was in a hurry. As nico_swd said it should work. Do you get any error messages ?
I get no error messages. It seems to work: I get the form, the buttons, ... The only missing thing is the result! I need the output on a file, but no file is produced. I have the suspect the PHP part of the code is not called at all... Could it depend on some browser setting? Could the PHP on my machine have some problems? (how can I check the version? If I give the "php" command at the prompt I get "command not found").
Did you try my code? It does work for me. Do you have a file called form_output.txt in the same directory? Is the file writable? What browser are you using? (It's almost impossible to be a browser problem) To check your PHP version do. <?php echo phpversion(); ?> PHP: But this has nothing to do with your version. IF you have an older version (< 4.1), try replacing $_POST with $HTTP_POST_VARS. You can also echo a test string in the if() condition to see if its executed.
Ahh well why don't you create the file form_output.txt and leave it empty? I guess you're running php on windows and that might cause the problem.
I am using Linux. If the file "form_output.txt" does not exist, nothing is done. If it exists (empty or not)... nothing is done. The browsers are SeaMonkey and Firefox (latest versions). nico_swd: I wrote your test and: From the browser I get an empty page. From the command line (I made the file executable): > ./test.php line 1: No such file or directory line 2: syntax error near unexpected token '(' line 2: 'echo phpversion();' > php ./test.php php: command not found
Okay, this is starting to annoy me. It's so simple and takes too long. Run this script and tell me everything it does. <?php error_reporting(E_ALL); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <head> <meta content="text/html; charset=ISO-8859-1" http-equiv="content-type"/> <title>Test</title> </head> <body> <form method="POST" name="TestForm" action="<?php echo $_SERVER['PHP_SELF']; ?>"> <p /> Input anything: <input type="text" name="anything" value="Default"/> <br /> <input type="submit" value="OK" name="submit"/> </form> <?php if (@$_POST['submit']=='OK') { echo '- Staring to write.<br />'; if (!file_exists('form_output.txt')) { echo '- form_output.txt does not exist.<br />'; echo '- Lets attempt to create it.<br />'; if (@touch('form_output.txt')) { echo '- File created<br />'; } else { exit('- File not crated. Script stopped<br />'); } } else if (!is_writable('form_output.txt') AND !chmod('form_output.txt', 0755)) { echo '- the file is not writable<br />'; } $fh = fopen("form_output.txt", 'w'); echo fwrite($fh, $_POST['anything']) ? '- Data written to file<br />' : '- Failed to write to file<br />'; fclose($fh); } ?> </body> </html> PHP:
If I call the script "test.php" and I load it in the browser, I get an empty screen, with no messages. If I call the script "test.php.html" and I load it in the browser, I get this: Input anything: [Default ] [OK] (this is the form, shown correctly) '; if (!file_exists('form_output.txt')) { echo '- form_output.txt does not exist. '; echo '- Lets attempt to create it. '; if (@touch('form_output.txt')) { echo '- File created '; } else { exit('- File not crated. Script stopped '); } } else if (!is_writable('form_output.txt') AND !chmod('form_output.txt', 0755)) { echo '- the file is not writable '; } $fh = fopen("form_output.txt", 'w'); echo fwrite($fh, $_POST['anything']) ? '- Data written to file ' : '- Failed to write to file '; fclose($fh); } ?> Sorry if I will reply you tomorrow, but I must go now. Thank's a lot for your help!