Need help connecting variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Need help connecting variables

Post by ChessclubFriend »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


This is my index.php

Code: Select all

<html><Body>
<form action="ChatFile.php" method="POST">Display Name <input type="text" name="displayname" />Your message <input type="text" name="chatinput"  /><br /><input type="submit" value="Submit Message" /></form></center>
<?php include("chatFile.txt"); ?>
</body></html>

This is my ChatFile.php
<?php $myFile = "chatFile.txt";
$whitespace = ": ";
$whitespaces = "\n";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$displayname $whitespace $chatinput $whitespaces";
fwrite($fh, $stringData);
fclose($fh);
?>
When my html file includes chatfile.txt, displaying it, it doesn't display the Displayname and the Chatinput. I'm pretty sure the only reason this doesn't work is because of the way I connected the variables. Can anyone help me?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

Try putting this in your PHP file...

Code: Select all

$displayname = $_POST['displayname'];
$chatinput = $_POST['chatinput'];
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok! It works great now, this is what chatfile.php looks like to fix it:

Code: Select all

<?php
$myFile = "chatFile.txt";
$displayname = $_POST['displayname'];
$chatinput = $_POST['chatinput'];
$whitespace = ": ";
$whitespaces = "\n";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "$displayname$whitespace $chatinput $whitespaces";
fwrite($fh, $stringData);
fclose($fh);
?>
The POST imported the variables from the form perfectly. Thanks!

If you still want to help...

chatfile.txt has displayname:chatinput on a new line every time a new message is received like I want. However, when I actually display chatfile.txt everything is all on one line, how do I display each received message on a new line, like the text file actually is?

By the way thanks a lot for your help, I'm very greatfull
:)


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
ChessclubFriend
Forum Newbie
Posts: 21
Joined: Sun Mar 25, 2007 9:13 pm

Post by ChessclubFriend »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Ok I did it again! I delete the 'include' function and used this

Code: Select all

<?php
$file = fopen("chatFile.txt","r");while(! feof($file))
  {
  echo fgets($file). "<br />";
  }fclose($file);?>

Thanks for your time and help people!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Post Reply