0

I'm sorry, I know this has been asked over and over, but I can't find a thread where people aren't arguing about the possibility of writing files with PHP or javascript. You get people saying "IMPOSSIBLE!" and others saying "POSSIBLE under these conditions". The answers on these threads that contain code don't work when I try to include them in my simple webpage, so I just want to post my snippet of code, my attempt at writing a text file, and see if anyone can tell me why it doesn't work and whether it is even possible.

My goal is to get the page to take each submission, and write all the text fields on one line in a text document, put in a line-return, and save/close the file. So each time something is submitted, a new single line is added for each submission.

<script>
  <?php
  if(isset($_POST['write'])) {
    $f = fopen("log.txt", "a");
    fwrite($f, $_POST["fName"]);
    fwrite($f, $_POST["lName"]);
    fwrite($f, $_POST["sessionDate"]);
    fwrite($f, $_POST["fName"]);
    fwrite($f, "\n");
    fclose($f);
    die():
  }
  ?>
</script>

<form method="post" action="?write">
  <input name="fName" type="text" placeholder="FIRST NAME"/>
  <input name="lName" type="text" placeholder="LAST NAME"/>
  <input name="sessionDate" type="date"/>
  <input name="workoutPerformed" type="text" placeholder="WORKOUT PERFORMED"/>
  <button name="submitButton" class="checkinButton" type="submit">SUBMIT</button>
</form>

I'm running these files in a local directory on my computer, and when I can determine that everyone works as intended to put them on a webpage to keep track of my business' member check

Tony White
  • 197
  • 5
  • 18
  • Do you have the proper permissions to write to the file? – robere2 May 06 '17 at 22:30
  • I'm testing this on a local directory, I'd assume I have the permission to write the file unless it's a setting I need to explicitly set @bugfroggy – Tony White May 07 '17 at 00:51

1 Answers1

0

I don't see any reason that your code wouldn't work, apart from:

  • "write" should be a form field - probably a hidden one.

  • I'd use an absolute path for the log file name, to make sure that it really does go where you think it does.

  • Put the name of your script in the form's action attribute, leaving out the question mark.

Ray O'Donnell
  • 759
  • 4
  • 11
  • Care to explain the down-vote? – Ray O'Donnell May 06 '17 at 22:38
  • Not sure :c I'm just getting to try your suggestions, quick question though. What's a form field, and how do I make it hidden? – Tony White May 07 '17 at 00:46
  • Removing the question mark from the action just tries to redirect my browser to a file called "write" instead of attempting to run the php script that's in-line with index.html code – Tony White May 07 '17 at 00:50
  • In the action attribute, replace "?write" with the name of the script - so if the script is called `my_script.php` then you should have `action="my_script.php"`. – Ray O'Donnell May 07 '17 at 06:59
  • A hidden form field looks like this: `` – Ray O'Donnell May 07 '17 at 07:01
  • Got it, but now when it's run, the page just redirects to a webpage that displays the php code text. I think that's because there isn't a web server for it to actually run the script. So my question is if it's possible to write that file without having a web server – Tony White May 07 '17 at 12:13