0

I'm trying to accept a form and write it to a CSV (invisible to the people submitting the form, but I can look at it as a compilation of everyone's entries on the server when I feel like it). Every time someone enters the form, it will become a new line on the CSV. To show that the people are actually submitting, a new tab will pop up with a little "thank you" like message and their submission so they can make sure it's theirs. Yes, I do have a JS form validation that works perfectly, but since that doesn't have a problem I left it out to save space.

Here is my current problem. In Firefox, I just get a blank new tab and nothing changes on my--blank--CSV, which is titled testForm.csv. In Chrome, a new tab opens that contains all the code on my php document, and my CSV stays blank.

Here's the snippet of my HTML:

<html>
 <body>
  <form name="Involved" method="post" action="postest.php" target="_blank" onsubmit="return validateForm();">
   Name: <br><input type="text" name="name" title="Your full name" style="color:#000" placeholder="Enter full name"/>
   <br><br>
   Email: <br><input type="text" name="email" title="Your email address" style="color:#000" placeholder="Enter email address"/>
   <br><br>
   How you can help: <br><textarea cols="18" rows="3" name="help" title="Service you want to provide" style="color:#000" placeholder="Please let us know of any ways you may be of assistance"></textarea>
   <br><br>
   <input type="submit" value="Submit" id=submitbox"/>
  </form>
 </body>
<html>

Here is postest.php:

<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $help = $_POST['help'];
 $csvData = $name . "," . $email . "," . $help . '\n';
 echo "Thank you for your submission! We'll get back to you as soon as we can!";
 echo "I'm " . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
 $filepointer = fopen('testForm.csv','a');
 if ($filepointer){
  fwrite($filepointer,$csvData);
  fclose($filepointer);
  exit();
 }
?>

I checked out this question about echoing to see if that was my problem. I asked this question before and nobody seemed to find anything wrong with my code other than the obvious $_POSTEST problem. This page looked like what I was going for, but wasn't. This question kind of had what I was going for but didn't actually have the POST code and the answer was one of the most useless things I've ever read (in a nutshell: "Just do it. It isn't that complicated." and some links to other SO questions, which I followed). They brought me here and here. I put exit(); after fclose() like it seemed to work for the first one (it did nothing). With the second, the user's code was too far removed from the codes I've been looking at for me to change my code over to what he/she was doing. I've been searching a lot, and doing extensive googling, but I'm going to cut my list of research here because nobody wants to read everything; this is just to prove I tried.

Let me know if there's anything else you need; I am a complete php novice and it's probably something very basic that I missed. On the other hand, I'm not seeing any major differences between my code and others' at this point.

Community
  • 1
  • 1
Bucephalus
  • 13
  • 7
  • You store the result of `open` in `$filepointer`, then use `$fp`. Change one or the other. – jcaron Aug 03 '14 at 17:56
  • Thank you! Unfortunately, that was just the more obvious of my problems; my code still just opens up a blank page and does nothing to my CSV. – Bucephalus Aug 03 '14 at 18:36

3 Answers3

0

Try something like this :

<?php
 $name = $_POST['name'];
 $email = $_POST['email'];
 $help = $_POST['help'];
 $filepointer = fopen('testForm.csv','a');
 fputcsv($filepointer, array($name,$email, $help));
 echo "Thank you for your submission! We'll get back to you as soon as we can!";
 echo "I'm " . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
?>
SpencerX
  • 5,453
  • 1
  • 14
  • 21
0

This is the error :-

--->   $filepointer = fopen('testForm.csv','a');
$fp = fopen('testForm.csv','a');
if ($fp){
  fwrite($fp,$csvData);
  fclose($fp);
  exit();
}

And the real issue is developing without

display_errors = On
log_errors = On

Look for these parameters in the php.ini file, and turn them on, unless you are developing on a live server, in which case, you really should set up a test environment.

and then not looking at the php error log

UPDATE

There was only one line to change actually, here is the complete code.

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $help = $_POST['help'];
    $csvData = $name . "," . $email . "," . $help . '\n';
    echo 'Thank you for your submission! We\'ll get back to you as soon as we can!';
    echo '\"I\'m \"' . $name . ", my email is " . $email . ", and I can help in that: \n" . $help;
    $fp = fopen('testForm.csv','a');    // only line changed
    if ($fp){
       fwrite($fp,$csvData);
       fclose($fp);
       exit();
    }
?>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
  • Would that go inside or outside the `` brackets, and before or after the code? In addition, is the log on the page that pops up? I think you're underestimating exactly how much of a novice I am--I had never even heard of POST before this project. :) Thank you for letting me know about the error display/log, that should make life a lot easier. – Bucephalus Aug 03 '14 at 18:18
  • And I am feeling unutterably thick for not realizing what you were trying to tell me with the arrow. Unfortunately, after referencing `filepointer` I still have a problem. – Bucephalus Aug 03 '14 at 18:29
  • What problem are you having – RiggsFolly Aug 03 '14 at 18:39
  • My problem remains that, rather than opening up a "thank you" tab and storing data in a CSV, the program does absolutely nothing to the CSV and opens a tab to a completely blank page. – Bucephalus Aug 03 '14 at 18:58
  • Well it works here, maybe you are looking for the file in the wrong directory. For me it replaces the page with the 'Thank you for....' line that you echo. Which looks pretty awful but it does show. – RiggsFolly Aug 03 '14 at 19:01
  • The reason I thought I had to put the backslashes in was because I was mixing up my `'`s and `"`s. I fixed it--thanks for pointing it out. I realized that I should try different browsers to see if they had different behaviors. In Firefox it opens a blank tab and the CSV doesn't change. In Chrome, it opens a new tab with the entire code printed (including the `` tags, and the CSV still doesn't change. I am still not sure where to put the `display` and `log` errors code, nor am I entirely sure what an ini file is (other than the basic googling I did when you mentioned it). – Bucephalus Aug 07 '14 at 16:47
  • Remember, I may be an idiot now, but this question means I'm trying not to be an idiot later. As I said before, this is my first time using php, so I probably don't know things that are basic to most people. I do not know what an ini file is, and the only testing environment I have is a sample snippet of a form in the same directory as my post test and test CSV. Also, sorry for how long it took me to get back to you; I haven't had internet access for a while. – Bucephalus Aug 07 '14 at 16:49
0

Your error is really basic and I am ashamed of you. Your problem is obviously that you have not been using a server, nor do you have a PHP package installed on your computer. When you told your computer target="_blank" and method="post", it knew what you wanted, being HTML. However, not having anything that parsed PHP, it had no idea how to read your code and came up as a blank page in Firefox and a block of code in Chrome.

You, indeed, have no idea what you are doing.

Bucephalus
  • 13
  • 7
  • Before anyone comments on this post saying how jerkish this person is, please take a look at how the poster relates to the OP. – Bucephalus Aug 07 '14 at 18:56