Jump to content

Recommended Posts

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> 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/53676-text-field-variable-problem/
Share on other sites

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.

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>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.