1

I have a form in which I want to capture the name and email of a user and store it in a text file. I'm not sure if it's my code or if I have to save my script in a specific place. When the submit button is clicked I get an error that says "404 file or directory not found". My client is uploading them through ftp so I have no way to check if it works locally. The PHP script(process-form-data.php) and text file (formdata.txt) are saved within the public HTML folder. I'm very new to PHP so sorry if this is a dumb question. Any help on what I'm doing wrong and the process of uploading the files would be greatly appreciated, thanks!

   <form name=”web_form” id=”web_form” method=”post” action=”process- 
    form-data.php”>

     <label for="exampleInputEmail1"><h3 style="color: #eee; margin: 0; 
     font-weight: 400;">Request Information</h3></label>

     <input type="text" name=”name” class="form-control" id="name" 
      placeholder="Your Name">

     <input type="text" class="form-control" name=”email” id=”email” 
      placeholder="youremail@domain.com">

     <button type="submit" name=”s1″ id=”s1″ value="submit" class="btn 
     btn-primary btn2">Submit</button>
  </form>


<?php
 $name = $_POST[‘name’];
 $email = $_POST[’email’];
 $fp = fopen(”formdata.txt”, “a”);
 $savestring = $name . “,” . $email . “n”;
 fwrite($fp, $savestring);
 fclose($fp);
 echo “<h1>You data has been saved in a text file!</h1>”;
?>
Alex
  • 113
  • 1
  • 7
  • 1
    Start by using an Ascii text editor so you dont get Odd double quotes and single quotes in a PHP script. Try Notepad++ its very good and free. You cannot use a Document editor like WORD to edit code – RiggsFolly Apr 30 '18 at 14:43
  • 1
    Also add [error reporting](http://stackoverflow.com/questions/845021/how-to-get-useful-error-messages-in-php/845025#845025) to the top of your file(s) _while testing_ right after your opening PHP tag for example ` – RiggsFolly Apr 30 '18 at 14:44
  • It would be wise to ensure that the text file you're writing to is outside the document root; generally speaking it's a bad idea to have a file that's both web server writeable and readable. – CD001 Apr 30 '18 at 14:47
  • I use Sublime text editor. Where would you recommend I save the text file and how would I call out the path to the text file? What will adding the error report do? Sorry, I'm just trying to learn as much as I can so I will know what to do in the future. Thanks for all your help! – Alex Apr 30 '18 at 14:53
  • _What will adding the error report do?_ Show you any errors you make while coding – RiggsFolly Apr 30 '18 at 15:07
  • So do you have a `process-form-data.php` and did you really code it with a line break in the middle of the file name? – RiggsFolly Apr 30 '18 at 15:09
  • I used code from an article that I read online. I have very little PHP knowledge, unfortunately. Here's the link for a reference http://www.howtoplaza.com/save-web-form-data-text-file Yes, I have a file named process-form-data.php – Alex Apr 30 '18 at 15:17

1 Answers1

1

You should mind about your double quotes ("") written in your html code

<form name='web_form' id='web_form' method='post' action=''>

     <label for="exampleInputEmail1"><h3 style="color: #eee; margin: 0; 
     font-weight: 400;">Request Information</h3></label>

     <input type='text' name='name' class="form-control" id="name" 
     placeholder="Your Name">

     <input type="text" class="form-control" name='email' id='email' 
     placeholder="youremail@domain.com">

     <button type="submit" name='s1' id='s1' value="submit" class="btn btn- 
     primary btn2">Submit</button>
  </form>

this code writes email and name in a text file

<?php
    if (isset($_POST['s1'])) {
      echo $_POST['name'];
      echo $_POST['email'];

      $myfile = fopen("data.txt", "w") or die("Unable to open file");
      $name = $_POST['name']."\n";
      fwrite($myfile, $name);

      $email = $_POST['email']."\n";
      fwrite($myfile, $email);
      fclose($myfile);
    }
  ?>
Bagzie
  • 482
  • 5
  • 7
  • Ok, so if I change my PHP code and HTML to what you have provided the script should essentially work? Also, do I still need my PHP file name in (action='') attribute? – Alex Apr 30 '18 at 17:34
  • you can use your action linking to the php file you want, but that php file should contain tha php code i wrote – Bagzie May 02 '18 at 14:47
  • It worked out perfectly! Thank you so much for the time you have put in to help me out! I was actually going to post another question about having the data sent to an email address as well. Is there any way you can help me with that? Btw, I'm also a graphic designer and would love to do some pro bono work for you if you're in need of anything in the design realm. Just my way of saying thanks for your time and solution. – Alex May 02 '18 at 17:54