0
<form action="" method="get">
<div id= "window"><center><input id="chatbox" type="text" name="chatbox">
<br></center></div> 

</form>

<?php

$myfile = fopen("chat.txt", "a+") 
$chattext = $_GET['chatbox'];
fwrite($myfile, $chattext);
fclose($myfile);
?>

I have looked at other solutions but for some reason I can't make my code work. My goal is to take the text in the text field and write it to the chat.txt file. I am very new to php so it is definitely possible that I just made a stupid syntax error. Thank You for your help.

  • Make sure the file has all the necessary permissions. – Pyromonk Apr 24 '17 at 00:58
  • What error do you have? Always be specific and make your best effort before asking. Stack Overflow is a question and answer site, not a code-writing service. Please [see here](https://stackoverflow.com/help/how-to-ask) to learn how to write effective questions. – Teocci Apr 24 '17 at 01:06
  • `$myfile = fopen('chat.txt', 'a+'); $chattext = $_GET['chatbox']==='' ? $_GET['chatbox'] : 'Hellow'; fwrite($myfile, $chattext); fclose($myfile);` fixed – Teocci Apr 24 '17 at 01:24
  • @Teocci sorry about that I tried to be as specific as possible in my question I am not getting any errors which is a major problem as I can't really see if my code is even running. Thanks for your support! – Ben Crawford Apr 24 '17 at 01:41
  • Did you tried the code that I post before? I corrected your syntax error. Please check if is so I will post it as an answer to close your question... – Teocci Apr 24 '17 at 03:26

1 Answers1

0
  • You forgot the ; after fopen()

  • I modified the code with an if statement

  • I added new line after each insert

Updated code:

<form action="" method="get">

<div id= "window"><center><input id="chatbox" type="text" name="chatbox">
<br></center></div> 

</form>

<?php

if(isset($_GET['chatbox'])){
    $myfile = fopen("chat.txt", "a+") ;
    $chattext = $_GET['chatbox'].PHP_EOL;
    fwrite($myfile, $chattext);
    fclose($myfile);
}
?>
Peter
  • 748
  • 6
  • 20
  • That is a very silly error! Hahaha but I still seem to have a problem is it possible that I am calling the php wrong as it still is not running. Is it a problem that I have it in an html file not a php because I tried it in both and in both cases it did not work. – Ben Crawford Apr 24 '17 at 01:31
  • the file with php content should be a php file of course not html.. this code is works, I've tested it. If you get another problem you should us what is the error – Peter Apr 24 '17 at 01:38
  • this is embarrassing but I have no idea how to call this code I think that may have been my original problem. I have tried some solutions online but I can't seem to understand what goes where when I put the code into a php file and then created a button to link to it when I clicked the button I was taken to a page that just displayed the code. So then I tried to put the whole thing in a php file and that just plum didn't work I'm sorry but how do I make this even run? – Ben Crawford Apr 24 '17 at 02:01
  • Do you use WAMP, LAMP? Or what kind of environment do you use? – Peter Apr 24 '17 at 02:03
  • I have just been using notepad++ to code in. – Ben Crawford Apr 24 '17 at 02:05
  • Not to code, to run the code? installed apache with php or? – Peter Apr 24 '17 at 02:06
  • Oh sorry XAMPP. – Ben Crawford Apr 24 '17 at 02:12
  • Thank you so much Peter realized my mistake and was able to make it work you were a godsend! – Ben Crawford Apr 24 '17 at 02:53
  • Colud you close the question or accept the answer? – Peter Apr 24 '17 at 10:24