Jump to content

[SOLVED] form -> CSV not working


gin

Recommended Posts

If I do the following it works as expected. The variable $foo is passed from the form correctly.

<?php
echo '"aaa","bbb"';
$out = "\n\"".$_POST['foo'].'"';
print $out;
?>

 

However, in the following the variable doesn't pass at all. The CSV file comes out okay, but where the variable should be there is nothing.

<?php
header("Content-type: application/csv");
header("Content-disposition: attachment; filename=file.csv");
echo '"aaa","bbb"';
$out = "\n\"".$_POST['foo'].'"';
print $out;
?>

 

Any advice much appreciated, for the sake of my remaining hair.

Link to comment
https://forums.phpfreaks.com/topic/40303-solved-form-csv-not-working/
Share on other sites

It seems to come through for me:

 

<?php
ob_start();
?>
<form action="" method="post">
<input type="text" name="foo">
<input type="submit">
</form>

<?php
if (!empty($_POST["foo"])) {
	header("Content-type: application/csv");
	header("Content-disposition: attachment; filename=file.csv");
	echo '"aaa","bbb"';
	$out = "\n\"".$_POST['foo'].'"';
	print $out;
}
?>

That doesn't work for me. The resultant file has the wrong filename (test.php instead of file.csv) and the contents are only the <form>...</form>, even when I placed the form in the else portion of that if statement.

 

I'm using Apache 2, PHP 5, MySQL 5, if it's relevant.

The following code works fine in both FF2 & MSIE7:

<?php
if (!empty($_POST["foo"])) {
	header("Content-type: application/csv");
	header("Content-disposition: attachment; filename=file.csv");
	echo '"aaa","bbb"';
	$out = "\n" . '"'.$_POST['foo'].'"';
	print $out;
	exit();
}
?>
<form action="" method="post">
<input type="text" name="foo">
<input type="submit">
</form>

 

Which browser are you using?

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.