Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Javascript HTML PHP , +
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
$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:
<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 13-Jul-13 8:36am
G4mm4R4y1.1K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
<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.
  Permalink  
Comments
Jaydman2102 at 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

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

  Print Answers RSS
0 OriginalGriff 200
1 PIEBALDconsult 150
2 BillWoodruff 148
3 Jochen Arndt 135
4 DamithSL 130
0 OriginalGriff 5,695
1 DamithSL 4,506
2 Maciej Los 4,007
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web04 | 2.8.141216.1 | Last Updated 13 Jul 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100