我想获取文本框的值,然后将其传输到文件“prova.txt”
以下是我的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>
答案 0 :(得分:2)
如果您希望PHP处理表单,则必须调用PHP文件(而不是您要写入的文本文件,稍后会出现)。在PHP文件中,POST和GET逻辑必须分开; GET请求将显示表单,POST请求将调用处理程序并写入您的文本文件。
<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>
答案 1 :(得分: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>
试试这个
编辑:您不能将.txt文件用于action
,并且应始终为name
按钮分配submit
。
答案 2 :(得分:-1)
<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);
?>