简单的 HTML/PHP 拒绝工作



这里是海报点 php

<?php
$FILENAME = "poster.php";
$txt = "data.txt"; 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both 
fields are set
$fh = fopen($txt, 'a'); 
$txt=$_POST['field1'].' - '.$_POST['field2']; 
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
}
?>

这是我的 HTML 表单

<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

当我把它放在调试器中时,我尽了一切努力来做到这一点,它说文件应该在第一行结束。当我运行文件时,我得到的只是一个白屏。我迫切需要帮助。

在海报.php文件中附加表单数据后,您没有编写任何内容。您可以通过以下方式获取文件内容...

网页形式:

<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="poster.php" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

并在poster.php

<?php
$FILENAME = "poster.php";
$txt = "data.txt"; 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both 
//fields are set
$fh = fopen($txt, 'a'); 
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL; 
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
echo file_get_contents("data.txt");
}
?>

根据要求,在单个文件中编写PHP代码和HTML代码,并在以下代码中删除表单的动作属性...

<?php
//$FILENAME = "poster.php";
$txt = "data.txt"; 
if (isset($_POST['field1']) && isset($_POST['field2'])) { // check if both 
//fields are set
$fh = fopen($txt, 'a'); 
$txt=$_POST['field1'].' - '.$_POST['field2'].PHP_EOL ; 
fwrite($fh,$txt); // Write information to the file
fclose($fh); // Close the file
//echo file_get_contents("data.txt");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Field 1 & 2</title>
</head>
<body>
<form action="" method="post">
What is your name?<br>
<input type="text" name="field1"><br>
<input type="text" name="field2"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>

表单的操作属性应该是您提交表单的文件名。

<form action="action.php" method="post"> 

这里是"动作.php",但你必须在海报点php上提交。

请检查一下:

<form action="poster.php" method="post">

表单操作将是您的主文件名(海报.php(,在表单发布后,您可以轻松获取海报.php文件上的所有表单字段数据。

你把这个文件名 ->海报点 php 的意思是海报.php ?

最新更新