当我尝试测试我的html表单时,它会显示一个白色屏幕。这是我的代码。
的index.html
<form name="form1" method="post" action="test.php">
<textarea name="data" cols="100" rows="10">
Facebook:
Twitter:
Instagram:
Website:
Comments:
---------------------------------------------
</textarea>
<br>
<input type="submit" value="Save">
</form>
test.php的
<html>
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On'); //On or Off
$saving = $_REQUEST['saving'];
if ($saving == 1){
$data = $_POST['data'];
$file = "data.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Saved to $file successfully!";
}
?>
</html>
我可以在页面上“查看源代码”,但我只是在php文件中获取上面的代码。页面标题显示test.php页面。它应该这样做吗? PHP新手。提前谢谢。
答案 0 :(得分:1)
我认为你没有进入if代码
$saving = $_REQUEST['saving'];
if ($saving == 1) {
$data = $_POST['data'];
$file = "data.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Saved to $file successfully!";
} else {
echo 'Nope!';
}
尝试添加此ELSE,看看你是否看到'Nope'。
对于初学者来说,$ _REQUEST ['saving']是什么?它不是表单上的输入所以它可能不是什么。
请改为:
if ($_POST) {
$data = $_POST['data'];
$file = "data.txt";
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Saved to $file successfully!";
} else {
echo 'Nope!';
}
答案 1 :(得分:1)
使用所写的两个代码体。
我添加了一个条件,以防有人试图直接访问test.php
。
<form name="form1" method="post" action="test.php">
<textarea name="data" cols="100" rows="10">
Facebook:
Twitter:
Instagram:
Website:
Comments:
---------------------------------------------
</textarea>
<br>
<input type="submit" name="submit" value="Save">
</form>
<html>
<?php
ini_set('error_reporting', E_ALL);
ini_set('display_errors', 'On'); //On or Off
if(!isset($_REQUEST['data'])) {
echo "You cannot do that from here.";
exit;
}
else {
$data = $_REQUEST['data'];
}
if(isset($_REQUEST['submit'])) {
$file = "data.txt";
chmod($file, 0777);
// chmod($file, 0644); // or use 644 which is safer
$fp = fopen($file, "a") or die("Couldn't open $file for writing!");
fwrite($fp, $data) or die("Couldn't write values to file!");
fclose($fp);
echo "Saved to $file successfully!";
}
else {
echo "Submit not set.";
}
?>
</html>
答案 2 :(得分:0)
更改你的html:
<input type="submit" value="Save" name="saving"/>
同时更改你接受参数的php:
$saving = $_REQUEST['saving'];
if ($saving) { // it is enough to just check if there is a value, the actual value is "Save"
...
}