• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 721
  • Last Modified:

PHP echo $_POST results to email or CSV

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
mcarter1953
Asked:
mcarter1953
  • 5
  • 3
  • 2
  • +2
1 Solution
 
wmadrid1Commented:
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
 
wmadrid1Commented:
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
 
mcarter1953Author Commented:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
woepwobinCommented:
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
 
mcarter1953Author Commented:
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
 
woepwobinCommented:
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
 
mcarter1953Author Commented:
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
 
mcarter1953Author Commented:
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
 
woepwobinCommented:
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
 
romartynCommented:
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
 
Ray PaseurCommented:
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
 
mcarter1953Author Commented:
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

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now