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>
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>
<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.
<html>
<head>
<title>
write
</title>
</head>
<body>
<form method="POST">
<input type="text" name="name">
<input type="submit" value="scrivi sul file">
</form>
</body>
<?php
$riga = "";
$array = $_POST;
foreach ($array as $value) {
$riga .= $value . "|";
}
file_put_contents('prova.txt', $riga);
?>