-5

I want to get the value of textbox then transfer it in the file "prova.txt"

Below is my codes for PHP:

<html>
<head>
    <title>
        write
    </title>
</head>
<body>
<form action="prova.txt" method="POST"> 
<?php
echo" <input type=\"text\" name=\"name\">";
$A=['name'];
$riga = "";
$array = array();
$array=$A;
foreach ($array as $value) {
$riga .= $value . "|";
}

    $fp= fopen('prova.txt', 'a');
    fwrite($fp, $riga);
    fclose($fp);
?>
<input type="submit" value="scrivi sul file">
</form>
</body>

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
sipti
  • 11
  • 1
  • 5
  • http://stackoverflow.com/a/14999433/5586647 – Neobugu Oct 27 '16 at 14:39
  • You can't use a text file for the action in a form. – Aurous Oct 27 '16 at 14:39
  • 2
    @Aurous: you can use ANY url as the action. whether what you're posting makes sense to whatever is on the other end of that url is irrelevant - you can still get/post to anything you want. and of course, file extensions are just a SUGGUESTION of what the file type is. you could map .exe to be parsed as php scripts if you wanted, ditto for .txt, or .himom – Marc B Oct 27 '16 at 14:55
  • You have a history of off-topic or heavily downvoted questions and are at risk of losing your question-asking privileges. You should [read this before you post your next one](http://meta.stackoverflow.com/questions/254262/before-you-post-your-next-question). – Jay Blanchard Oct 27 '16 at 15:02

2 Answers2

2

If you want PHP to process the form, a PHP file must be invoked (not the text file you want to write to, that comes later). In the PHP file, POST and GET logic must be separated; a GET request will show the form, a POST request will invoke a handler and write to your text file.

<html><!-- Common header for GET and POST responses-->
<head>
    <title>Write a File</title>
</head>
<body>
<?php // enter PHP Parsing mode

if (!$_POST) {   //no POST has occurred, show the form

?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST"> 
<input type="text" name="name">
<input type="submit" value="scrivi sul file">
</form>

<?php
} else { // a form *has* been POSTed
?
   // sanitize the var $_POST['name'] with a basic filter
   $A  = filter_input(INPUT_POST,'name',FILTER_SANITIZE_STRING);

   // append the sanitized input to our text file
   $fp = file_put_contents('prova.txt', $A, FILE_APPEND);

   // give feedback to the user
   if ($fp) {
       echo "File written successfully";
   } else {
       echo "Problem writing file.";
   }
}
//escape from PHP mode
?>
<!-- this is a common footer for both GET and POST responses -->
</body>
</html>
Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
  • 1
    Why should the OP try this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Oct 27 '16 at 15:03
0
<html>
<head>
    <title>
        write
    </title>
</head>
<body>
<form action="" method="POST" name="fwrite"> 
<?php
if(isset($_POST['submit'])){
echo" <input type=\"text\" name=\"name\">";
$A=['name'];
$riga = "";
$array = array();
$array=$A;
    $fp= fopen('prova.txt', 'a');
    fwrite($fp, $riga);
    fclose($fp);
}
?>
<input name="submit" type="submit" value="scrivi sul file">
</form>
</body>

Try this

EDIT: You cannot use a .txt file for an action and you should always assign a name for a submit button.

Frank Helligberger
  • 46
  • 1
  • 1
  • 10
  • Why should the OP try this? A ***good answer*** will always have an explanation of what was done and why it was done in such a manner, not only for the OP but for future visitors to SO. – Jay Blanchard Oct 27 '16 at 15:03