0

当我尝试测试我的 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 文件中获取上面的代码。该页面的标题正在显示 test.php 页面。它应该这样做吗?PHP新手。提前致谢。

4

3 回答 3

1

试试这个,经过测试。(无白屏)

使用编写的两个代码体。

我添加了一个条件,以防有人尝试test.php直接访问。

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" name="submit" value="Save">
</form>

PHP 处理程序 (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>
于 2013-09-11T22:20:47.497 回答
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 并查看您是否看到“否”。

首先,$_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!';
}
于 2013-09-11T21:42:21.187 回答
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"
    ...
}
于 2013-09-11T21:44:06.947 回答