phazelwood Posted May 31, 2007 Share Posted May 31, 2007 I'm new trying to use php forms. Trying to write what is typed into the text field to a file. But its not working. It makes the file, but the text field data is not written. Any help appreciated. Here is the code <html><head><title>Test</title></head> <body> <?php if(!$fields) { $form ="<form action=\"\" method=\"post\">"; $form.="Input Something<br>"; $form.="<input type=\"text\" name=\"fields\" size=\"5\">"; $form.="<input type=\"submit\" value=\"Submit\">"; echo($form); $handle = fopen("c:\\server\\htdocs\\phptesting\\iquiz\\testform.txt", "a"); fwrite($handle,$fields); fclose($handle); } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/53676-text-field-variable-problem/ Share on other sites More sharing options...
earnan Posted May 31, 2007 Share Posted May 31, 2007 The information entered in the form is not available until after the form is submitted. When the form is submitted, it goes to the webpage identified in the "action" option. Take the code to open and write the file, put it in a new PHP page along with $fields = $_POST['fields']; Put the name of that page in the form action setting (you COULD point the form to itself if you would like, in that case, put an "} else" after echo($form) and add the code above. Quote Link to comment https://forums.phpfreaks.com/topic/53676-text-field-variable-problem/#findComment-265409 Share on other sites More sharing options...
Daniel0 Posted May 31, 2007 Share Posted May 31, 2007 Is this what you are trying to do? <html><head><title>Test</title></head> <body> <?php if(empty($_POST['fields']) { echo <<<EOF <form action="" method="post"> Input something: <input type="text" name="fields" size="5"> <input type="submit" value="submit"> EOF; } else { $handle = fopen("c:\\server\\htdocs\\phptesting\\iquiz\\testform.txt","a"); fwrite($handle,$_POST['fields']); fclose($handle); echo "Data written"; } ?> </body></html> Quote Link to comment https://forums.phpfreaks.com/topic/53676-text-field-variable-problem/#findComment-265436 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.