0

I'm having trouble creating a form that exports to a .CSV file in PHP. I created a fiddle for the HTML which is here:

http://jsfiddle.net/tqs6g/

I'm coding in PHP so I can't really show the full code on JSFiddle since it can't support the PHP but here's my PHP code:

<?php
if($_POST['formSubmit'] == "Submit")
{
    $errorMessage = "";

    if(empty($_POST['brandname']))
    {
        $errorMessage .= "<li>Please enter a business/brand name.</li>";
    }
    if(empty($_POST['firstname']))
    {
        $errorMessage .= "<li>Please enter your first name.</li>";
    }

    $varBrand = $_POST['brandname'];
    $varFName = $_POST['firstname'];
    $varLName = $_POST['lastname'];
    $varEmail = $_POST['email'];
    $varSite = $_POST['website'];

    if(empty($errorMessage)) 
    {
        $fs = fopen("mydata.csv","a");
        fwrite($fs,$varBrand . ", " . $varFName . ", " . $varLName . ", " . $varEmail . ", " . $varSite . "\n");
        fclose($fs);
        exit;
    }
}
?>

When I click Submit it successfully goes to 'thankyou.php' (which is set in the form action) but I can't figure out why it's not posting the correct error messages or filling in my 'mydata.csv' file upon click. Possibly it's a sight syntax error? Let me know if you need any more info, I know this is kind of confusing seeing as the PHP is separated from the Fiddle.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
MillerMedia
  • 3,651
  • 17
  • 71
  • 150

1 Answers1

0
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') { // better method to check for a POSt
    ... validation stuff ...
    $data = array();
    $data[] = $_POST['brandname'];
    $data[] = $_POST['firstname'];
    etc...
    if (empty($errrorMessage)) {
        $fs = fopen('mydata.csv', 'a') or die("Unable to open file for output");
        fputcsv($fs, $data) or die("Unable to write to file");
        fclose($fs);
        exit();
    } else {
       echo $errormessage;
    }
}

A few things of note:

1) using $_SERVER['REQUEST_METHOD'] to check for submit type is absolutely reliable - that value is always set, and will always be POST if a post is being performed. Checking for a particular form field (e.g. the submit button) is hacky and unreliable.
2) Using fputcsv() to write out to a csv file. PHP will do all the heavy work for you and you jus tprovide the function an array of data to write
3) Note the or die(...) constructs, which check for failures to open/write to the file. Assuming that a file is available/writeable is unreliable and will bite you at some point in the future. When dealing with "external" resources, always have error handling.

Marc B
  • 356,200
  • 43
  • 426
  • 500
  • How this answer finds the problem of the questioner? – Shiplu Mokaddim Jan 07 '12 at 17:28
  • 1
    How can we find a problem when the OP provides nothing of any use in the way of diagnostics? This shows the OP how to do that. The code OP posted otherwise looks fine, so this is the only way to proceed. – Marc B Jan 07 '12 at 17:31
  • I agree, I was having trouble diagnosing the problem, so I will run this and see what it results in. Thanks. – MillerMedia Jan 07 '12 at 17:39
  • I've tried this but it's doing the same thing. It's just going to thankyou.php and not responding even if I don't fill in any of the inputs on the form. My PHP code is in my tags. I would like to debug more but I wouldn't know how to figure out what code is even being run (if any). Is there an NSLog type feature in PHP (similar to Objective-C)? – MillerMedia Jan 07 '12 at 18:04
  • If you're redirecting via a `header(Location: ...` type thing, then make it conditional upon $errorMessage being empty. – Marc B Jan 07 '12 at 20:44