Form to write in csv messed up

Hi,

I have a form kindly provided by Drummin, and it works fine as expected, I added to the code the following to write the data on a csv file as well, and it kinda works (it writes the data), but has a (big) problem:
Some of the fields overflow into the next field messing up the data, which I view in spreadsheet, so the data does not correspond to the header, i.e. email is displayed in the phone column, and so on.

this is a sample field from Drummin’s code

 //Address
}elseif(!preg_match("/^[a-zA-Z0-9' -]{2,}/", trim($_POST['address']))){
$error .= "Address does not pass validation\n";

and this is the code I added to write the csv

$fp = fopen('/pathto-the-file/data.csv', 'a');
fwrite($fp, $name . ',' . $lastname . ',' . $address . ',' . $city . ',' . $country . ',' . $birthday . ',' . $email . ',' . $phone . ',' . $room . ',' . $gender . ',' . $comment . ',' . PHP_EOL);
fclose($fp);

There are more fields, in the form.

How can I fix this?

Thank you

For a start you are not escaping the , after $fp nore do you have the .

Thank you.
I am not a coder, and got that code somewhere else.

Anyhow, do you mean it should be like this?

fwrite($fp . ',' $name . ','

Not quite:

fwrite($fp . ',' . $name . ','

In this case the the . ‘,’ . joins the variables with a , between the variable contents; but you need to escape using the , first and then go back to the variable with the second , - not explained very well!

Thank you.

In all sincerity, I can read, and maybe even understand the plain English of what you said, but translating it to correct code … no way! I just don’t understand/know what/which , to escape, and HOW to escape it.

$fp = fopen('/pathto-the-file/data.csv', 'a');
fwrite($fp . ',' . $name . ',' . $lastname . ',' . $address . ',' . $city . ',' . $country . ',' . $birthday . ',' . $email . ',' . $phone . ',' . $room . ',' . $gender . ',' . $comment . ',' . PHP_EOL);
fclose($fp);

Just thinking about it and I do not think you need the last comma:

$fp = fopen('/pathto-the-file/data.csv', 'a');
fwrite($fp . ',' . $name . ',' . $lastname . ',' . $address . ',' . $city . ',' . $country . ',' . $birthday . ',' . $email . ',' . $phone . ',' . $room . ',' . $gender . ',' . $comment . PHP_EOL);
fclose($fp);

Thank you for your patience.

You actually had said it in your comment #4, but I missed the . before $name.
For sure I’ll try, and see what happens.

I’m back again.

I noticed that it seems the output gets messed up because in some fileds people use the comma, i.e. to write the address they might use something like:
999, First blv. San Diego, CA
And I also suspect carriage returns, and colons.

Might that be?
In case how to clean it up before passing it to the file, the form is also sent to email, and of course there is no problem there.

You could change the user input comma to something else when saving to a file. If you get the data out of the file to use elsewhere you can change it back again.

You do not have to use a comma as a delimiter and can use for instance a | which is unlikely to be used by anyone. You just need to let the software know that the | is the delimiter.

Thank you.

You could change the user input comma to something else
when saving to a file. If you get the data out of the file to use
elsewhere you can change it back again.

You do not have to use a comma as a delimiter and can use for
instance a | which is unlikely to be used by anyone. You just need to
let the software know that the | is the delimiter.

I guess the above are TWO options to choose from right?
If the above assumption is correct, and I opt for the second option to change the delimiter, how would I do it, by changing

fwrite($fp . ',' . $name . ',' . $lastname . ','

to

fwrite($fp . '|' . $name . '|' . $lastname . '|'

Or is something else I am unaware of?
And what option would I choose to open it with openoffice calc if using | ?

Thanks again.

That is correct and this page tells you how to set the “separator” in open office: https://wiki.openoffice.org/wiki/Documentation/FAQ/Calc/Files/How_do_I_open_a_tab-delimited_file_in_OpenOffice.org_Spreadsheet%3F_What_if_I_have_a_different_type_of_delimiter%3F

Thank you.

Ok the delimiter change seems to have solved the comma typed in the fields, but there is still the carriage return issue.
If in the text area, I use “enter” key to go to next line it puts the new line in the next cell, thus messing up the output.
How can I avoid that?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.