Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
My php form submit page keeps failing. it gives a undefined index error at every $_POST[''] variable. I put them in brackets where I should. What is wrong?

Code:

PHP
<?php
$ourFileName = $_POST['title'] .".html";
$ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
fclose($ourFileHandle);


$myFile = "servers.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = <<<EOD
<a href="{$_POST['title']}.html">{$_POST['title']}</a><br>
EOD;
fwrite($fh, $stringData);
fclose($fh);

$myFile = $_POST['title'] .".html";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = <<<EOF
<!DOCTYPE html>
<!--
Example Forum post/Topic
<div id="post">
   <a href="servers.php">Servers</a><br>
</div>
-->
<html> 
<head> 
<link rel="stylesheet" type="text/css" href="style.css">  
<title>Red Army</title> 
</head>
<body bgcolor="darkred">
<div id="header">
<h1 align="center">
The Red Army
</h1><br>
<h2 align="center"><a href="index.php">Forum</a>    <a href="http://www.theredarmymc.co.nf/">Main Site</a>
</div>
<div id="main">
<p align="center">
<br>
<br>
<div id="post">
   {$_POST['title']} Written by {$_POST['uname']}
   
</div><br>
<br>
<div id="post">
{$_POST['post']} 
<br>
</div>
<!--Ending-->
<br>
<br>
<br>
<br>
<font size="1">Coded and Founded by Jayden S</font>
<!--Ending-->
</p>
</div>
</body> 
</html>
EOF;
fwrite($fh, $stringData);
fclose($fh);

?>

The first undefined index error is at line2
Here is the html form:
HTML
<form action="spost.php">
Title: <input name="title" type="text"><br>
Username (ANY): <input type="text" name="uname"><br>
<br>
Posts support html. Please put <br>  at the end of each line.<br>
<br>
<textarea rows="10" cols="50" name="post"></textarea><br>
<br>
<input type="submit" value="Post">
</form>
Posted
Updated 6-Jul-18 0:33am

The default method for a form is "get", so the inputs will be in the $_GET array instead of $_POST. To change this, modify your form element:
PHP
<form action="spost.php" method="post">


You should check to see if the values are defined and valid too - it is possible to bypass your form and directly submit data to the processing page.
 
Share this answer
 
Comments
G4mm4R4y 14-Jul-13 2:08am    
If your talking about how its post and not get, Im not to worried about it but I fixed the issue before you answered I noticed my mistake. I fixed that BEFORE this question, but forgot to save. That's why I had trouble. But good news: My forum is up and running :D
use below code on top after php code
ini_set('display_errors',0);
 
Share this answer
 
Comments
Patrice T 6-Jul-18 9:53am    
6 years too late !
and hiding error message is not a solution.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900