Page 1 of 1

Need help connecting variables

Posted: Sun Mar 25, 2007 9:18 pm
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]

Posted: Sun Mar 25, 2007 9:32 pm
by iknownothing
Try putting this in your PHP file...

Code: Select all

$displayname = $_POST['displayname'];
$chatinput = $_POST['chatinput'];

Posted: Sun Mar 25, 2007 9:48 pm
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]

Posted: Sun Mar 25, 2007 10:34 pm
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]