Export textarea contents to CSV or TXT

I have a textarea box that gets populated after processing lots of data. I would like an option to download each line of the textarea box contents as a CSV file or text file using two text links.
LVL 10
ray-solomonAsked:
Who is Participating?
 
ThaSmartUnoCommented:
in download.php you are missing
echo $contents;
at the bottom (should be the last line above the ?>)

Also I would change the top part to this:

<form id="form1" enctype="multipart/form-data" method="post" action="replace.php">
<input type="hidden" name="type" value="text" />
<input type="hidden" name="data" value="" />
</form>
<form name="form1">


Also ... I found out that it turns out with IE, you have to return false after the function in onclick for the submit to work ... so you could either add;return false to the onclick or ... follow this example:

<script type="text/javascript">
//<![CDATA[
function saveText(id,type) {
  if( !document.getElementById )return false;
  var f=document.getElementById('downFileForm'),
    n=document.getElementById(id);

  if( f && n ) {
    f.data.value = n.value;
    if(type){
      f.type.value = type;
    }
    f.submit();
  }
  return true;
}
//]]>
</script>

<form id="downFileForm" action="downfile.php" method="method">
<input type="hidden" name="type" value="text" />
<input type="hidden" name="data" value="" />
</form>
<textarea id="text1">This is the contents of textarea1</textarea>
<a href="javascript:void(0)" onclick="return !saveText('text1');">Text</a> |
<a href="javascript:void(0)" onclick="return !saveText('text1','csv');">CSV</a><br/>

<textarea id="text2">This is the contents of textarea2</textarea>
<a href="javascript:void(0)" onclick="return !saveText('text2');">Text</a> |
<a href="javascript:void(0)" onclick="return !saveText('text2','csv');">CSV</a><br/>
0
 
ThaSmartUnoCommented:
<form id="downFileForm" action="downfile.php" method="post">
<input type="hidden" name="type" value="Text" />
<textarea name="data">Blah blah blah</textarea>
<noscript><input type="submit" value="Text" /></noscript>
</form>
<a href="javascript:void(0)" onclick="var f=document.getElementById('downFileForm');if(f){f.submit();}">Text</a>

and in downfile.php
all you have to do is

$contents = array_key_exists( 'data', $_POST ) ? $_POST['data'] : '';
$filename = 'somefile.txt';

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/octet-stream"); // may also try 'application/force-download'
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Length: ".strlen($contents));
echo $contents;
exit;
0
 
ThaSmartUnoCommented:
after this line
$contents = array_key_exists( 'data', $_POST ) ? $_POST['data'] : '';

you may want to add:

if ( get_magic_quotes_gpc() ) {
  $contents = stripslashes( $contents );
}

this is in case magic_quotes is turned on in your config file ... otherwise quotes will show up as \' or \"
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
ray-solomonAuthor Commented:
How would the javascript link be changed if I wanted to download from more than one textarea box inside a form.
I have 16 textarea boxes. I would like two links for each of them. One for TXT and one for CSV.

Does somefile.txt need to exist and be writeable?

There will be multiple people using this form at the same time, can two or more sessions write to somefile.txt at the same time?

0
 
ThaSmartUnoCommented:
if thats the case (that there is 16 of them) then this may be better

<script type="text/javascript">
//<![CDATA[
function saveText(id,type) {
  if( !document.getElementById )return;
  var f=document.getElementById('downFileForm'),
    n=document.getElementById(id);

  if( f && n ) {
    f.data.value = n.value;
    if(type)
      f.type.value = type;
    f.submit();
  }
}
//]]>
</script>

<form id="downFileForm" action="downfile.php" method="get">
<input type="hidden" name="type" value="text" />
<input type="hidden" name="data" value="" />
</form>
<textarea id="text1">This is the contents of textarea1</textarea>
<a href="javascript:void(0)" onclick="saveText('text1');">Text</a> |
<a href="javascript:void(0)" onclick="saveText('text1','csv');">CSV</a><br/>

<textarea id="text2">This is the contents of textarea2</textarea>
<a href="javascript:void(0)" onclick="saveText('text2');">Text</a> |
<a href="javascript:void(0)" onclick="saveText('text2','csv');">CSV</a><br/>



It doesn't actually create the file on the server, it just makes the browser think the file exists.  And I do not know what you mean by save for TXT or CSV.  If all you mean is that when its txt the extension of the file is txt and csv the extension is csv.  Otherwise, if some parsing needs to be done, I will have to see some dummy content.
0
 
ray-solomonAuthor Commented:
Okay, I used your suggestion and it look great, but I get this error message when I click on the javascript links.

Request-URI Too Large
The requested URL's length exceeds the capacity limit for this server.

request failed: URI too long


Any suggestions?
0
 
ThaSmartUnoCommented:
yea i apologize, change method="get" to method="post"
0
 
ray-solomonAuthor Commented:
I changed it from get to post and that seemed like it is trying to work, but when I view somefile.txt, it is empty.

I also noticed that it only post's in Firefox. It does not work with Internet Explorer.
In internet Explorer I don't get any javascript messages or anything else, I just will not work when i click on any link.

I am using Windows XP SP2 with latest versions of all those browsers.


Here is a copy of my PHP page to help you understand.
I use dreamweaver 8.

This is replace.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Test Page</title>


<style type="text/css">
<!--
.txtPlain {
      font-family: Arial, Helvetica, sans-serif;
      font-size: 12px;
}

.txtBold {
      font-family: Arial, Helvetica, sans-serif;
      font-weight: bold;
      font-size: 12px;
}
.txtGreen {
      font-family: Arial, Helvetica, sans-serif;
      font-weight: bold;
      font-size: 12px;
      color: #00CC66;
}
.txtBlue {
      font-family: Arial, Helvetica, sans-serif;
      font-weight: bold;
      font-size: 12px;
      color: #3333FF;
}
.style1 {
      font-family: "Trebuchet MS", Verdana, Arial, "MS Sans Serif";
      color: #006699;
}
-->
</style>
<SCRIPT LANGUAGE="JavaScript">
<!-- Begin
function copyit(theField) {
var tempval=eval("document."+theField)
tempval.focus()
tempval.select()
therange=tempval.createTextRange()
therange.execCommand("Copy")
}
//  End -->
</SCRIPT>
<script>
function checkBlock(theField){
  if(theField.value==""){
    var theForm = theField.form;
    var ta = arguments;
    for(var i=1;i<ta.length;i++){
       theForm[ta[i]].value="";
    }
  }
}
</script>
<script type="text/javascript">
//<![CDATA[
function saveText(id,type) {
  if( !document.getElementById )return;
  var f=document.getElementById('form1'),
    n=document.getElementById(id);

  if( f && n ) {
    f.data.value = n.value;
    if(type)
      f.type.value = type;
    f.submit();
  }
}
//]]>
</script>
</head>
<body>
<h1 align="center" class="style1">Test Page </h1>
<p align="center" class="txtBold"><?php echo $statement1.$statement2; ?></p>
<p align="center" class="txtBold"></p>
<form action="replace.php" method="post" enctype="multipart/form-data" name="import" id="import">
  <div align="center">
    <table width="755" border="0" cellpadding="2" cellspacing="0">
      <tr>
        <td colspan="5" align="center" class="txtPlain">&nbsp;</td>
      </tr>
      <tr>
        <td width="103" class="txtPlain">Import CSV File </td>
      <td width="221"><input type="file" name="csvfile"/></td>
      <td width="227"><input name="csvpost" type="submit" value="Go"/></td>
      <td width="66">&nbsp;</td>
      <td width="118">&nbsp;</td>
    </tr>
    </table>
  </div>
</form>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="replace.php">
<input type="hidden" name="type" value="text" />
<input type="hidden" name="data" value="" />
  <div align="center">
    <table width="755" border="0" cellspacing="2" cellpadding="1">
      <tr>
        <td class="txtPlain">Original List </td>
        <td class="txtPlain">&nbsp;</td>
        <td class="txtPlain">&nbsp;</td>
        <td class="txtPlain">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td class="txtPlain"><span class="txtGreen"><?php echo $tbox_field1; ?></span></td>
        <td class="txtPlain"><span class="txtGreen">
          <input name="button17" type="button" onclick="copyit('form1.field1')" value="Copy" />
        </span></td>
        <td class="txtPlain">&nbsp;</td>
        <td class="txtPlain">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
        <td colspan="2">&nbsp;</td>
      </tr>
      <tr>
        <td height="325" colspan="2"><textarea name="field1" cols="20" rows="20" id="field1"><?php echo stripslashes($field1); ?></textarea></td>
        <td colspan="2">&nbsp;</td>
        <td colspan="4"><div align="center">
            <table width="270" border="0" cellspacing="2" cellpadding="1">
              <tr>
                <td colspan="2" class="txtPlain">&nbsp;</td>
              </tr>

              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td width="38" class="txtPlain">&nbsp;</td>
                <td width="246">&nbsp;</td>
              </tr>


              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td class="txtPlain">Replace this word in Groups 2, 3 or 4: </td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><input name="replacethis" type="text" id="replacethis" size="40" value="<?php echo stripslashes($replacethis); ?>" /></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><span class="txtPlain">Group 2 </span></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><input name="r2" type="text" id="r2" size="40" value="<?php echo stripslashes($r2); ?>" onChange="checkBlock(this,'t5','t6','t7','t8')" onBlur="this.onchange()" /></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><span class="txtPlain">Group 3</span></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><input name="r3" type="text" id="r3" size="40" value="<?php echo stripslashes($r3); ?>" onChange="checkBlock(this,'t9','t10','t11','t12')" onBlur="this.onchange()" /></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><span class="txtPlain">Group 4 </span></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><input name="r4" type="text" id="r4" size="40" value="<?php echo stripslashes($r4); ?>" onChange="checkBlock(this,'t13','t14','t15','t16')" onBlur="this.onchange()" /></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td><input type="submit" name="Submit" value="Generate" />
                <input name="Clear" type="reset" id="Clear" value="Clear" /></td>
              </tr>
              <tr>
                <td class="txtPlain">&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
            </table>
        </div>          </td>
      </tr>
      <tr>
        <td colspan="8" bgcolor="#CCCCCC" class="txtPlain"><div align="center" class="txtBold">Group 1</div></td>
      </tr>
      <tr>
        <td colspan="2" class="txtPlain">Plain Formatting ( <a href="javascript:void(0)" onclick="saveText('textarea1');">Text</a>, <a href="javascript:void(0)" onclick="saveText('textarea1','csv');">CSV</a> ) </td>
        <td colspan="2" class="txtPlain">Words with Brackets ( <a href="javascript:void(0)" onclick="saveText('t2');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t2','csv');">CSV</a> ) </td>
        <td colspan="2"><span class="txtPlain">Words with Quotes ( <a href="javascript:void(0)" onclick="saveText('t3');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t3','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Preformatted Words ( <a href="javascript:void(0)" onclick="saveText('t4');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t4','csv');">CSV</a> ) </span></td>
      </tr>
      <tr>
        <td width="138" class="txtGreen"><?php echo $tbox1; ?></td>
        <td width="50" class="txtGreen"><input name="button" type="button" onclick="copyit('form1.t1')" value="Copy" /></td>
        <td width="129" class="txtGreen"><?php echo $tbox2; ?></td>
        <td width="48" class="txtGreen"><input name="button2" type="button" onclick="copyit('form1.t2')" value="Copy" /></td>
        <td width="127"><span class="txtGreen"><?php echo $tbox3; ?></span></td>
        <td width="47"><span class="txtGreen">
          <input name="button3" type="button" onclick="copyit('form1.t3')" value="Copy" />
        </span></td>
        <td width="130"><span class="txtGreen"><?php echo $tbox4; ?></span></td>
        <td width="52"><span class="txtGreen">
          <input name="button4" type="button" onclick="copyit('form1.t4')" value="Copy" />
        </span></td>
      </tr>
      <tr>
        <td colspan="2"><textarea name="t1" cols="20" rows="20" id="t1"><?php echo stripslashes($textarea1); ?></textarea></td>
        <td colspan="2"><textarea name="t2" cols="20" rows="20" id="t2"><?php echo stripslashes($textarea2); ?></textarea></td>
        <td colspan="2"><textarea name="t3" cols="20" rows="20" id="t3"><?php echo stripslashes($textarea3); ?></textarea></td>
        <td colspan="2"><textarea name="t4" cols="20" rows="20" id="t4"><?php echo stripslashes($textarea4); ?></textarea></td>
      </tr>
      <tr>
        <td colspan="8" class="txtPlain">&nbsp;</td>
      </tr>
      <tr bgcolor="#CCCCCC">
        <td colspan="8" class="txtPlain"><div align="center" class="txtBold">Group 2</div></td>
      </tr>
      <tr>
        <td colspan="2" class="txtPlain">Plain Formatting ( <a href="javascript:void(0)" onclick="saveText('t5');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t5','csv');">CSV</a> ) </td>
        <td colspan="2" class="txtPlain">Words with Brackets ( <a href="javascript:void(0)" onclick="saveText('t6');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t6','csv');">CSV</a> ) </td>
        <td colspan="2"><span class="txtPlain">Words with Quotes ( <a href="javascript:void(0)" onclick="saveText('t7');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t7','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Preformatted Words ( <a href="javascript:void(0)" onclick="saveText('t8');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t8','csv');">CSV</a> ) </span></td>
      </tr>
      <tr>
        <td class="txtGreen"><?php echo $tbox5; ?></td>
        <td class="txtGreen"><input name="button5" type="button" onclick="copyit('form1.t5')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox6; ?></td>
        <td class="txtGreen"><input name="button6" type="button" onclick="copyit('form1.t6')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox7; ?></td>
        <td class="txtGreen"><input name="button7" type="button" onclick="copyit('form1.t7')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox8; ?></td>
        <td class="txtGreen"><input name="button8" type="button" onclick="copyit('form1.t8')" value="Copy" /></td>
      </tr>
      <tr>
        <td colspan="2"><textarea name="t5" cols="20" rows="20" id="t5"><?php echo stripslashes($textarea5); ?></textarea></td>
        <td colspan="2"><textarea name="t6" cols="20" rows="20" id="t6"><?php echo stripslashes($textarea6); ?></textarea></td>
        <td colspan="2"><textarea name="t7" cols="20" rows="20" id="t7"><?php echo stripslashes($textarea7); ?></textarea></td>
        <td colspan="2"><textarea name="t8" cols="20" rows="20" id="t8"><?php echo stripslashes($textarea8); ?></textarea></td>
      </tr>
      <tr>
        <td colspan="8" class="txtPlain">&nbsp;</td>
      </tr>
      <tr bgcolor="#CCCCCC">
        <td colspan="8" class="txtPlain"><div align="center" class="txtBold">Group 3 </div></td>
      </tr>
      <tr>
        <td colspan="2"><span class="txtPlain">Plain Formatting ( <a href="javascript:void(0)" onclick="saveText('t10');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t10','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Words with Brackets ( <a href="javascript:void(0)" onclick="saveText('t4');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t4','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Words with Quotes ( <a href="javascript:void(0)" onclick="saveText('t11');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t11','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Preformatted Words ( <a href="javascript:void(0)" onclick="saveText('t12');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t12','csv');">CSV</a> ) </span></td>
      </tr>
      <tr>
        <td class="txtGreen"><?php echo $tbox9; ?></td>
        <td class="txtGreen"><input name="button9" type="button" onclick="copyit('form1.t9')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox10; ?></td>
        <td class="txtGreen"><input name="button10" type="button" onclick="copyit('form1.t10')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox11; ?></td>
        <td class="txtGreen"><input name="button11" type="button" onclick="copyit('form1.t11')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox12; ?></td>
        <td class="txtGreen"><input name="button12" type="button" onclick="copyit('form1.t12')" value="Copy" /></td>
      </tr>
      <tr>
        <td colspan="2"><textarea name="t9" cols="20" rows="20" id="t9"><?php echo stripslashes($textarea9); ?></textarea></td>
        <td colspan="2"><textarea name="t10" cols="20" rows="20" id="t10"><?php echo stripslashes($textarea10); ?></textarea></td>
        <td colspan="2"><textarea name="t11" cols="20" rows="20" id="t11"><?php echo stripslashes($textarea11); ?></textarea></td>
        <td colspan="2"><textarea name="t12" cols="20" rows="20" id="t12"><?php echo stripslashes($textarea12); ?></textarea></td>
      </tr>
      <tr>
        <td colspan="8">&nbsp;</td>
      </tr>
      <tr bgcolor="#CCCCCC">
        <td colspan="8" class="txtPlain"><div align="center" class="txtBold">Group 4</div></td>
      </tr>
      <tr>
        <td colspan="2"><span class="txtPlain">Plain Formatting ( <a href="javascript:void(0)" onclick="saveText('t13');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t13','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Words with Brackets ( <a href="javascript:void(0)" onclick="saveText('t14');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t14','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Words with Quotes ( <a href="javascript:void(0)" onclick="saveText('t15');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t15','csv');">CSV</a> ) </span></td>
        <td colspan="2"><span class="txtPlain">Preformatted Words ( <a href="javascript:void(0)" onclick="saveText('t16');">Text</a>, <a href="javascript:void(0)" onclick="saveText('t16','csv');">CSV</a> ) </span></td>
      </tr>
      <tr>
        <td class="txtGreen"><?php echo $tbox13; ?></td>
        <td class="txtGreen"><input name="button13" type="button" onclick="copyit('form1.t13')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox14; ?></td>
        <td class="txtGreen"><input name="button14" type="button" onclick="copyit('form1.t14')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox15; ?></td>
        <td class="txtGreen"><input name="button15" type="button" onclick="copyit('form1.t15')" value="Copy" /></td>
        <td class="txtGreen"><?php echo $tbox16; ?></td>
        <td class="txtGreen"><input name="button16" type="button" onclick="copyit('form1.t16')" value="Copy" /></td>
      </tr>
      <tr>
        <td colspan="2"><textarea name="t13" cols="20" rows="20" id="t13"><?php echo stripslashes($textarea13); ?></textarea></td>
        <td colspan="2"><textarea name="t14" cols="20" rows="20" id="t14"><?php echo stripslashes($textarea14); ?></textarea></td>
        <td colspan="2"><textarea name="t15" cols="20" rows="20" id="t15"><?php echo stripslashes($textarea15); ?></textarea></td>
        <td colspan="2"><textarea name="t16" cols="20" rows="20" id="t16"><?php echo stripslashes($textarea16); ?></textarea></td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <p>&nbsp;</p>
  </div>
</form>
</body>
</html>


This is download.php
<?php
$contents = array_key_exists( 'data', $_POST ) ? $_POST['data'] : '';
$filename = 'somefile.txt';

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: application/octet-stream"); // may also try 'application/force-download'
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Length: ".strlen($contents));
?>
0
 
ThaSmartUnoCommented:
and there's a typo in my code ... method="method" should be method="post"
0
 
ray-solomonAuthor Commented:
Wow, thank you for helping me. It is much appriciated. It all works perfectly now! You deserve an A+
0
 
ThaSmartUnoCommented:
Thanks, you are welcome.
0
 
ray-solomonAuthor Commented:
if you are intersted in helping me more, I opened up another question related to this.

http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_21913243.html
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.