简体   繁体   中英

PHP form displaying a white screen?

When I try and test my html form, it displays a white screen. Here's my code.

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>

I can 'view source' on the page, but I just get the code above in the php file. The title of the page is displaying the test.php page. Should it be doing this? PHP newbie. Thanks in advance.

I don't think you are getting into the if code

$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'.

For starters, what is $_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.

HTML form

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

Change your html:

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

Also change the php where you are accepting the param:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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