当我尝试测试我的html表单时,它显示一个白屏。这是我的代码。
索引.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>
测试.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 文件中获取上面的代码。页面的标题显示测试.php页面。它应该这样做吗?PHP新手。提前谢谢。
试一试,经过测试。(无白屏)
使用编写的两个代码体。
我添加了一个条件,以防有人尝试直接访问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>
PHP hander (test.php)
<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>
我不认为你正在进入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,看看您是否看到"否"。
对于初学者来说,什么是 _REQUEST 美元["节省"]?它不是表单上的输入,所以它可能什么都没有。
请改为执行以下操作:
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!';
}
更改您的 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"
...
}