简体   繁体   English

PHP表单显示白屏?

[英]PHP form displaying a white screen?

When I try and test my html form, it displays a white screen. 当我尝试测试我的html表单时,它会显示一个白色屏幕。 Here's my code. 这是我的代码。

index.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 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>

I can 'view source' on the page, but I just get the code above in the php file. 我可以在页面上“查看源代码”,但我只是在php文件中获取上面的代码。 The title of the page is displaying the test.php page. 页面标题显示test.php页面。 Should it be doing this? 它应该这样做吗? PHP newbie. PHP新手。 Thanks in advance. 提前致谢。

I don't think you are getting into the if code 我认为你没有进入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!';
}

Try adding this ELSE and see if you see 'Nope'. 尝试添加此ELSE,看看你是否看到'Nope'。

For starters, what is $_REQUEST['saving']? 对于初学者来说,$ _REQUEST ['saving']是什么? It isn't an input on the form so it is probably a nothing. 它不是表单上的输入,所以它可能一无所获。

Do this instead: 改为:

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!';
}

Give this a try, tested. 尝试一下,测试一下。 (no white screen) (没有白屏)

Use both bodies of code as written. 使用编写的两个代码体。

I added a condition in case someone tries to access test.php directly. 我添加了一个条件,以防有人试图直接访问test.php

HTML form 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 hander (test.php) 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>

Change your html: 改变你的HTML:

<input type="submit" value="Save" name="saving"/>

Also change the php where you are accepting the param: 同时更改你接受param的php:

$saving = $_REQUEST['saving'];
if ($saving) { // it is enough to just check if there is a value, the actual value is "Save"
    ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM