Solved

PHP echo $_POST results to email or CSV

Posted on 2011-09-09
12
630 Views
Last Modified: 2012-08-14
Im looking for a way to take the results (of the previous JavaScript & PHP script where the results POST'ed to a cell) and either email them or write them to a csv.  Im having some real issues with the variables as they exist when trying to insert them into a formail'esqu script.  

Refer to this question to see how I got the result below.  Thanks

<tr>
  <td>
    <?php echo $_POST['option1']; ?>
  </td>
  <td>
    <?php echo $_POST['option2']; ?>
  </td>
  <td>
    <?php echo $_POST['option3']; ?>
  </td>
  <td>
    <?php echo $_POST['option4']; ?>
  </td>
</tr>

Open in new window

0
Comment
Question by:mcarter1953
  • 5
  • 3
  • 2
  • +2
12 Comments
 
LVL 5

Expert Comment

by:wmadrid1
ID: 36510518
Hi
depends of what do you want to show,
the textbox
<?php echo $_POST['1value']; ?>

Open in new window

or the select
<?php echo $_POST['1type']; ?>

Open in new window



0
 
LVL 5

Expert Comment

by:wmadrid1
ID: 36510538
you finally have 6 elements in the POST array
$_POST['1value'];
$_POST['2value'];
$_POST['3value'];

$_POST['1type'];
$_POST['2type'];
$_POST['3type'];

Open in new window

referring to the other question, so play with them as you need
0
 

Author Comment

by:mcarter1953
ID: 36510597
The syntax is the same with both lines, only the variable is different and I don't understand how '1type' works as a variable for my code.

<?php echo $_POST['1type']; ?>

Open in new window


<?php echo $_POST['option1']; ?>

Open in new window

0
 
LVL 3

Expert Comment

by:woepwobin
ID: 36510868
I don't understand what you mean exactly by 'take the results and either email them or write them to a csv'. If for debugging purposes, this should work:

mail('you@domain.com', 'Form submit data', var_export($_POST, true));

Open in new window

0
 

Author Comment

by:mcarter1953
ID: 36511434
woepwobin, I tried and no luck.  Like I said Im having some real trouble passing the $POST data from the results page to the email page.  Essentially this what I am doing...

from 'index.html'
<form action="results.php" method="post">

<input name="option1" style="width: 160px" type="text" value="Line 1" />
<input name="option2" style="width: 160px" type="text" value="Line 1" />
<input name="option3" style="width: 160px" type="text" value="Line 1" />

<input name="Submit" type="submit" value="Update Results" />

Open in new window


from results.php
<form name "options" action="send.php" method="POST">

<?php echo $_POST['option1']; ?>
<?php echo $_POST['option2']; ?>
<?php echo $_POST['option3']; ?>

<input name="Submit" type="submit" value="Submit" />

Open in new window


from send.php
<?php
mail('email@whatever.com', 'Form submit data', var_export($_POST, true));
?>

<center>Thank you <b><?php echo $_POST['formname']; ?></b>. Your request has been sent.</center>

Open in new window


The send.php reports no errors, bu the $_POST['formname'] functions shows nothing and I never receive the email.
0
 
LVL 3

Expert Comment

by:woepwobin
ID: 36511872
Aah, you have some sort of 'confirm' page in between... try this for results.php:

<form name "options" action="send.php" method="POST">

<?php echo $_POST['option1']; ?>
<?php echo $_POST['option2']; ?>
<?php echo $_POST['option3']; ?>

<input type="hidden" name="option1" value="<?php echo $_POST['option1']; ?>" />
<input type="hidden" name="option2" value="<?php echo $_POST['option2']; ?>" />
<input type="hidden" name="option3" value="<?php echo $_POST['option3']; ?>" />

<input name="Submit" type="submit" value="Submit" />

Open in new window

0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 

Author Comment

by:mcarter1953
ID: 36513325
Ive apperantly got more problems that I though, I cannot email from PHP at all and the PHP.ini looks correct.  I'd rather pass the data to a CSV or TXT file anyways but I still cannot get the variables to pass via $_POST to the results.php file.  Can you try it locally to verify it works for you?
0
 

Author Comment

by:mcarter1953
ID: 36513707
OK, after a lot of fiddling around and research I found an issue with the PHP.ini file.  Got write working but still no email.  That is OK though, I can live with it for now.

Here's the code for the file write...
	
<?php 
$file = '5410buttons.txt'; 
$arr= $_POST; 
$fp = fopen($file, 'w') or die('Could not open file!');  
foreach ($arr as $key => $value) { 
$toFile = "Key: $key; Value: $value \n"; 
// write to file  
fwrite($fp, "$toFile") or die('Could not write to file');  
// close file  
} 
fclose($fp); 
?>

Open in new window

Here's the result from the text file...
Key: Submit; Value: Submit for Programming
Key: formname; Value: John Doe Key: formextn; Value: 9999 Key: button1type; Value: A Key: button2type; Value: B  Key: button3type; Value: C

2 questions - can I append instead of write and is there a way to have a line break <br /> inserted after each value statement or before the key statement?

thanks :)
0
 
LVL 3

Expert Comment

by:woepwobin
ID: 36514071
1. Append to file (see http://www.php.net/manual/en/function.fopen.php):
$fp = fopen($file, 'a') or die('Could not open file!');

Open in new window


2. If you're on windows you can try "\r\n" instead of "\n".
0
 
LVL 1

Accepted Solution

by:
romartyn earned 500 total points
ID: 36515920
Hello!
I would do like this
 
ob_start();
foreach($_POST as $key=>$post){
   $csv_head[]='"'.$key.'"';
   $csv_body[]='"'.$post.'"';
}
echo implode(';',$csv_head)."\n";
echo implode(';',$csv_body)."\n";
$out=ob_get_clean();

Open in new window

And then
 
mail('you@domain.com', 'Form submit data', $out);

Open in new window

or
 
file_put_contents('path_to_file.csv',$out);

Open in new window


So you can get a csv as a file or an email
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 36516085
When you are dealing with external data, like POST requests, data visualization is vitally important.  Want to see what is in the POST array?  Use something like this:

echo "<pre>";  // MAKE IT EAST TO READ
var_dump($_POST); // SEE MAN PAGE

http://php.net/var-dump

When you are creating a form, and you want to be able to ignore certain parts of the POST request array, a useful technique is to omit the name attribute from the input tag.  If you do this, the input control will show on the client screen, but will not appear in $_POST.  Example of a "submit" control that works, but does not clutter up the POST array.

<input type="submit" value="Submit this form" />

Another technique I sometimes use is to give the "special" form elements names that start with the underscore character.  You can use a simple substr() function to test the first character and handle things differently if you find an underscore in the leftmost position.  Something like this:

if (substr($_POST["element"],0,1) == '_') /* IGNORE THIS */
http://us2.php.net/manual/en/function.substr.php

Regarding this: have a line break <br /> inserted after each value -- If you are creating a CSV string from POST data, you do not actually want the HTML break tag.  You want a context-appropriate "new line" character.  PHP has a built-in constant that handles this.  Append PHP_EOL to your output string to start a new line in a CSV file.  Example of showing my name and email:

echo '"Ray Paseur", ray.paseur@gmail.com' . PHP_EOL;

You can write CSV files from arrays with fputcsv()
http://us2.php.net/fputcsv

A normal design pattern for using associative arrays and fputcsv() would include writing the keys into the first line of the CSV file and then writing the data elements into the next line or lines.  You can use the function array_keys() to get the keys.
http://php.net/manual/en/function.array-keys.php

You can append information to a CSV file, just like any other file.  See "mode a" in the man page here:
http://us2.php.net/manual/en/function.fopen.php

Things like this do not really do you any good - they just proliferate variables.  Use the $POST array instead of copying it.
$arr= $_POST;

PHP has a tutorial about working with forms.
http://us2.php.net/manual/en/tutorial.forms.php
0
 

Author Closing Comment

by:mcarter1953
ID: 36522684
That did the trick.  I appreciate the help.  The only thing I changed is added the 'FILE_APPEND' flag as in...
file_put_contents('filename.csv',$out, FILE_APPEND);

Open in new window

0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now