0

I am new to php and trying just to append these two variables that I'm getting from a form on another page to a csv file.

<?php
$email = $_POST['Email_Address']; 
$full_name = $_POST['Full_Name']; 
$entry = $full_name;
$entry .= ",";
$entry .= $email;
$file = fopen("contacts.csv","w");
fputcsv($file,explode(',',$entry));
fclose($file);
?>

This script does run and the file does get written, but two problems:

  1. It seems to write over the existing file instead of appending.

  2. Clicking submit takes me to a blank page with Url = location of this php script.

The form is in a pop up window and I just want to close that window onclick instead of it taking me somewhere else.

Here's the form:

<form method="post" action="php-forms/subscribeform.php">
<input placeholder="Name" type="text" name="Full_Name" id="Full_Name" required />
<input placeholder="Email" type="email" name="Email_Address" id="Email_Address" required />
<input class="formBtn" type="submit" />
<input class="formBtn" type="reset" />
</form>

There isn't any error that I can see (I enabled errors to check).

How do I accomplish this?

cmckeeth
  • 85
  • 1
  • 8
  • Override the default behavior of the form with javascript and send via ajax. Or redirect back to the previous page after post. You can use `file_put_contents()` with the FILE_APPEND flag. – frz3993 Mar 19 '18 at 20:02

2 Answers2

2

You are using 'w' instead of 'a' in fopen(), that's why your file is always overridden. Then you could use an array to use fputcsv() instead of create a string and explode it, because if a string contains a comma, you will write three values, instead of two.

From fopen() documentation:

a
Open for writing only; place the file pointer at the end of the file. If the file does not exist, attempt to create it. In this mode, fseek() has no effect, writes are always appended.
w
Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.

// first, check the existance of your variables:
if (isset($_POST['Email_Address']) && isset($_POST['Full_Name'])) 
{
    $email = $_POST['Email_Address']; 
    $full_name = $_POST['Full_Name']; 

    // create an array with your values
    $entry = [$full_name, $email];

    // use 'a' to Append.
    $file = fopen('contacts.csv', 'a');
    fputcsv($file, $entry);
    fclose($file);
}

// then redirect
header("Location: /");
exit;

Note that you should check the content of the posted values before to write them, and use them.

Syscall
  • 16,959
  • 9
  • 22
  • 41
1

1) To append use the 'a' flag (http://php.net/manual/en/function.fopen.php)

$file = fopen("contacts.csv","a");

2) Yout get redirected to a blank page after submitting because when you post to that url, you execute the logic in the php file. As it probably doesn't output any html, it's a blank page. For this, You can post asynchronously with javascript. Look here jQuery AJAX submit form

monstercode
  • 917
  • 5
  • 22