PHP: I cannot save the form's content on file...

Discussion in 'PHP' started by pietrom, Nov 7, 2006.

  1. #1
    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>
     
    pietrom, Nov 7, 2006 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    Replace anything with $_POST['anything'].

    And please use
     or [code] wrappers when posting code.
    PHP:
     
    nico_swd, Nov 7, 2006 IP
  3. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    maiahost, Nov 7, 2006 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    ^^ 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:
     
    nico_swd, Nov 7, 2006 IP
  5. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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:
     
    pietrom, Nov 8, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Try using $_POST['submit'] instead of $submit. And yes, the action attribute is needed.
     
    nico_swd, Nov 8, 2006 IP
  7. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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?
     
    pietrom, Nov 8, 2006 IP
  8. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #8
    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:
     
    nico_swd, Nov 8, 2006 IP
  9. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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).
     
    pietrom, Nov 8, 2006 IP
  10. kreoton

    kreoton Peon

    Messages:
    229
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #10
    <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 ;)
     
    kreoton, Nov 8, 2006 IP
  11. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    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)...
    :eek:
     
    pietrom, Nov 8, 2006 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    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:
     
    nico_swd, Nov 8, 2006 IP
  13. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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 ?
     
    maiahost, Nov 8, 2006 IP
  14. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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").
     
    pietrom, Nov 8, 2006 IP
  15. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #15
    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.
     
    nico_swd, Nov 8, 2006 IP
  16. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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.
     
    maiahost, Nov 8, 2006 IP
  17. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    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
     
    pietrom, Nov 8, 2006 IP
  18. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #18
    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:
     
    nico_swd, Nov 8, 2006 IP
  19. pietrom

    pietrom Peon

    Messages:
    8
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    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! ;)
     
    pietrom, Nov 8, 2006 IP
  20. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #20
    If you call test.php, does not even the HTML show up?
     
    nico_swd, Nov 8, 2006 IP