2

I have a PHP form in which the user enters their data. On submit, it should open an existing CSV file and append some of the data. I have tried a number of things, but here is the most recent code I am currently working with:

HTML:

<html>
<head><title>Design Request</title></head>
<title>Design Request</title>     

  <form action="test2.php" method="POST">
    <fieldset align="left">
    <legend>Executive Sponsor Form</legend>
    <p><i>To be completed by the executive sponsor</i></p>
    <table>
      <tr>
        <td>Name:</td><td><input type="text" name="SPONSOR_NAME" size="40" /></td>
      </tr>
      <tr>
        <td>Position:</td><td><input type="text" name="SPONSOR_POSITION" size="40" /></td>
      </tr>
      <tr>
        <td>Telephone Number:</td><td><input type="text" name="SPONSOR_TELEPHONE_NUMBER" size="40" maxlength="11" /></td>
      </tr>
      <tr>
        <td>Email Address:</td><td><input type="email" name="SPONSOR_EMAIL" size="40" /></td>
      </tr>
      <tr>
        <td>Budget :</td><td><input type="checkbox" /><input type="text" name="BUDGET" size="37" maxlength="6" placeholder="budget code" /></td>
      </tr>
      <tr>
        <td>Aligned to which<br> priority:</td><td><input type="text" name="PRIORITIES" size="40" /></td>
      </tr>
      <tr>
        <td> Benefit to Business:</td><td><textarea cols="42" rows="10" name="BENEFIT"></textarea></td>
      </tr>
    </form>
    <input type="submit" value="Submit">

PHP:

<html>
  <head>
    <title>Your (Ref No: ["SPONSOR_NAME"]) has been submitted.</title>        
  </head>
  <?php
    $data = array("" . ',' . "SPONSOR_NAME" . ',' . "" . ',' . "" . ',' . "SPONSOR_EMAIL" . ',' . "PRIORITIES" . );
    $cr;

    $fp = fopen("/var/www/testdb.csv","w");
    foreach ($data as $fields){
      fputcsv($fp, $fields);
    }
    fclose($fp);
  ?>
</html>

Some of the fields are entered on one worksheet where as some are entered on another one thats why I have the array with some empty fields (I want data in column B E and F) the other columns will contain data from another form.

2
  • 1
    man up John and use a proper database! Commented Jan 16, 2015 at 12:31
  • its an existing one in excel Commented Jan 16, 2015 at 12:44

1 Answer 1

1

since you have already constructed your array for your data, you can just write the line to the file using PHP's implode() function

  $data = array("" . ',' . "SPONSOR_NAME" . ',' . "" . ',' . "" . ',' . "SPONSOR_EMAIL" . ',' . "PRIORITIES" . );

  $file = "/var/www/testdb.csv";          //file to append to
  $current = file_get_contents($file);    //open the file
  $current .= "\n" . implode(",",$data);  //add line-break then comma separated array data
  file_put_contents($file, $current);     //append new content to file

if you need to fill in those blanks in your data from another form, you will have to reopen the file, extract the last line of data, explode() it to an array, fill in the missing data and then overwrite the original line in the csv... I would not recommend this because a different user could submit the first part of a form and then append their new data to an incorrect line of the csv... in this case you should really take the advice of @symcbean and use a proper database.

Sign up to request clarification or add additional context in comments.

2 Comments

Couldn't I have something like: open file, form 1 go to column b (post data) go to e (post data) go to f (post data). Then form 2 go to column a (post data) go to column c (post data) etc?
PHP won't see data in a CSV file as having columns. You would have to parse out the CSV data each time you want to make a write into a multi-dimensional array, write that new data into your array and then resave the array to the CSV. That isnt bad for a small application, but there will be performance issues at scale along with the issues of concurrent processes overwriting eachother. -- a read database can gracefully and easily handle these issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.