Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have a PHP file It is like this:

PHP
<?php
$myFile = "chat.txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = $_POST['uname']. ": ";
fwrite($fh, $stringData);
$stringData = $_POST['msg']. "<br>";
fwrite($fh, $stringData);
fclose($fh);
?>
<!DOCTYPE html>
<html>
<head>
<div id="php">
</div>
<link rel="stylesheet" type="text/css" href="style.css"> 
<title>Red Army | Chat Room</title>
</head>
<body bgcolor="darkred">
<div id="header">
<h1 align="center">
The Red Army
</h1><br>
<h2 align="center"><a href="mems.php">Members</a>    <a href="app.php">Applications</a>    <a href="news.php">News</a>    <a href="about.php">About</a>    <a href="other.php">Other</a>
</div>
<div id="main">
<p align="center">
<div id="chatbox" class="boxed">
<?php
$myFile = "chat.txt";
$fh = fopen($myFile, 'r');
$theData = fread($fh, filesize($myFile));
fclose($fh);
echo $theData;
?>
</div>
<form action="?" method="post">
Username: <input type="text" name="uname"><br>
Message: <input type="text" name="msg">
<input type="submit">
</form>
<br>
<br>
<!--Ending-->
<br>
<br>
<br>
<br>
<font size="1">Coded and Founded by Jayden S</font>
<!--Ending-->
</p>
</div>
</body>

I get the undefined variable error and I cant set a post variable or it just comes out whatever I set it to. So how do I get the error be hidden or solve the error? The contents of chat.txt are nothing but the file does exist. If you can help me that would be great.

Thanks for reading!
Posted
Comments
Sergey Alexandrovich Kryukov 6-Jul-13 13:24pm    
In what line?
Anyway, don't use undefined variables... :-)
—SA
G4mm4R4y 6-Jul-13 13:50pm    
Not sure what line I will run the page again but the undefined variables? That's what I am asking how can I NOT use undefined variables there It may be a dumb question but I am new to php. Maybe im jumping into things. But I am trying to basically make a chat box and the variables aren't defined how do I make it wait till they are defined to run that code?
G4mm4R4y 6-Jul-13 13:53pm    
( ! ) Notice: Undefined index: uname in C:\wamp\www\chat.php on line 4


Call Stack


#

Time

Memory

Function

Location

1 0.0015 249456 {main}( ) ..\chat.php:0



( ! ) Notice: Undefined index: msg in C:\wamp\www\chat.php on line 6


Call Stack


#

Time

Memory

Function

Location

1 0.0015 249456 {main}( ) ..\chat.php:0

This is the error report
Sergey Alexandrovich Kryukov 6-Jul-13 14:03pm    
There is no "wait". How? PHP works when something is already posted. Here is the thing: I cannot see with unarmed eye where the undefined variable is. You need to develop some debugging techniques. You really need to investigate which undefined variable actually used and where. In particular, you can use try-catch blocks just for investigation (http://php.net/manual/en/language.exceptions.php). I also used to use Phalanger to develop fragments of code under the debugger of Visual Studio...
—SA
G4mm4R4y 6-Jul-13 14:07pm    
The undefined variables are "$_POST['msg']" and "$_POST['uname'] I know there is a way no offense to your knowledge, because I found a tutorial that let me do this I believe you use javascript but I feel there is an easier way, but the chat box works it just displays the error message which is not very user friendly to my website. The action="?" uses the same page as the action. Can I hide the error message or fix it in any way? I don't want to have to make another page, as my hosting plan has limited space.

EDIT:
The undefined variable is the first script seen trying to append chat.txt

1 solution

Please see my comments to the question. You probably use the same page for showing the form and for showing the result of post. You just need to design your code to appropriately take these both possibilities into account.

In particular, you can use the very fact that $_POST["someKey"] is undefined by testing it: http://php.net/manual/en/function.isset.php[^].

—SA
 
Share this answer
 
v2
Comments
G4mm4R4y 6-Jul-13 14:46pm    
Ill accept this but still confused this is the best help I could get. But basically what I got was "Hey just make a new page!" So I will.
Sergey Alexandrovich Kryukov 6-Jul-13 15:27pm    
Great. You see, you just need to thoroughly design your code. Make a new page is the easiest approach. You can have one page with the form and another with the post results. However, reusing the same page for both form and post result is quite possible and is most usually used. On postback, you also need to create the same very form, but populate it with the values obtained from the latest post.
—SA

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