Jump to content

text field variable problem


phazelwood

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.