By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,242 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

How to send an entire html form by email

100+
P: 348
Hello eveyone

Is there a way to email an entire html page after the page is processed for data?

What I have is an entire html page that draws data from a db. I would like to email that form before the foreach loop goes back to create another form.

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. foreach()......
  4. if().....
  5. </body>
  6. </html>
  7.  
using php's email func, I need to email this entire form. Can someone please tell me how this can be done? I am using shorttags in my html.

Thanks
Aug 24 '08 #1
Share this Question
Share on Google+
4 Replies


Atli
Expert 5K+
P: 5,058
Hi.

Could you not simply store the data in a variable, mail it and then print it?

Like, say:
Expand|Select|Wrap|Line Numbers
  1. foreach($row = mysqli_fetch_assoc($result)) {
  2.   $form <<<FORM
  3. <span>Some DB value: {$_row['dbColumn']}</span>
  4. FORM;
  5.  
  6.   mail("joe@example.com", "Subject", $form, "From: me@example.com\r\n");
  7.  
  8.   echo $form;
  9. }
  10.  
Or am I not getting you?
If not, please explain in more detail.
Aug 25 '08 #2

100+
P: 348
fjm
Hi Atli,

Thanks for helping me on this. I have been stuck on this problem just going over the 1 week mark and I have no idea how to accomplish this task. I know the logic I need but have no idea how to do it. Please stay with me on this one if you can.

You are correct and you have the idea right on the money. Remember a week ago I posted that problem with the foreach loops? Well, this is the same problem. I have written a loop that will render exactly what I need. Its not pretty, but it is working.

What should happen is that when the first, outermost loop ends, there should be a function to mail those results. The loop should start over for the next customer, compile that report and then mail that it. This process should continue until there are no more results in the db.

Here is the loop I have written that works as I need.

Expand|Select|Wrap|Line Numbers
  1. require_once("db.php");
  2. $db = new Database('test');
  3. $report_type = array( 'Morning', 'Evening' );
  4. $rpthdr = $db->Query("SELECT t1.customer_id, t1.location_id ... WHERE ...");
  5. // $rpthdr sql shortened for brevity
  6. ?>
  7.  
  8. <?php foreach($rpthdr as $hdr): ?>
  9. <?php $customer_id = $hdr['customer_id']; $location_id = $hdr['location_id']; ?>
  10.  
  11. <?php foreach($report_type as $report): ?>
  12. <?php if($report == 'Morning'): ?>
  13.         <tr>
  14.           <td class="subhdrs">Report Overview</td>
  15.         </tr>
  16. <?php $results = $db->Query("SELECT... WHERE... AND t1.customer_id = '$customer_id' AND t1.location_id = '$location_id' AND t3.report_type = '$report'"); ?>
  17. <?php foreach($results as $rpt): ?>
  18.         <tr>
  19.           <td><?php echo $rpt['i_type']; ?>&nbsp;&nbsp;[<?php echo $rpt['add_info']; ?>]&nbsp;&nbsp;<?php echo $rpt['employee']; ?></td>
  20.         </tr>
  21. <?php endforeach; ?>
  22.         <tr>
  23.           <td class="subhdrs">Morning Report</td>
  24.         </tr>
  25. <?php $results = $db->Query("SELECT... WHERE... t1.customer_id = '$customer_id' AND t1.location_id = '$location_id' AND t3.report_type = '$report'"); ?>
  26. <?php foreach ($results as $rpt): ?>
  27.         <tr>
  28.           <td><?php echo $rpt['dtime']; ?><br><?php echo $rpt['i_type']; ?>&nbsp;[<?php echo $rpt['add_info']; ?>]<br><?php echo $rpt['disposition']; ?><br><?php echo $rpt['employee']; ?><br><br><?php echo $rpt['report']; ?></td>
  29.         </tr>
  30. <?php endforeach; ?>
  31. <?php elseif($report == 'Evening'): ?>
  32.         <tr>
  33.           <td class="subhdrs">Evening Report</td>
  34.         </tr>
  35. <?php $results = $db->Query("SELECT... WHERE... t1.customer_id = '$customer_id' AND t1.location_id = '$location_id' AND t3.report_type = '$report'"); ?>
  36. <?php foreach ($results as $rpt): ?>
  37.         <tr>
  38.           <td><?php echo $rpt['report']; ?></td>
  39.         </tr>
  40. <?php endforeach; ?>
  41. <?php endif; ?>
  42. <?php endforeach; ?>
  43.       </table>
  44.     </td>
  45.   </tr>
  46. </table>
  47. </body>
  48. </html>
  49. <?php endforeach; ?>
  50. ////////////////////////////////////////// MAIL() then loop back to beginning for next record
  51.  
This is pretty much the bulk of the report that has to be compiled. I can't get me fat brain around how to do this. I basically need to send everything from the <html> to the </html> tag and all of the processed information in between.

I am open to any suggestions you may have also about how to clean up this code. It looks terrible. Thanks Atli!
Aug 25 '08 #3

Atli
Expert 5K+
P: 5,058
Yes, I would have to agree. That's one ugly piece of code :)

Do not use the alternate syntax for the flow control statements. It's a sure way to cause all sort of mental and physical ailments (mostly caused by the next programmer that has to read the code).
(Always code like your code is to be handed of to the huge ex-con with an anger-management problem... in the office next to you!).

And try your best not to close the <?php ?> tags wherever you can. Besides making your code look ugly, it makes the PHP interpreter stop parsing until the next <?php ?> block, which is unnecessary overhead when there is nothing in between the two blocks.

The key thing if you want to write good code is to separate the HTML (or whatever the output is) from your PHP code. Don't fuse your PHP into the HTML or there will be no way to read that thing later.

The easiest way to do that would be to store the HTML in variables and use str_replace to replace keywords within the text.

Like, if you were creating tables from database values, you could do something like:
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // Set up the HTML
  3. $html_page = "<html>
  4. <head><title>{TITLE}</title>
  5. <body>{TABLES}</body>
  6. </html>";
  7.  
  8. $html_table ="<table><tr><th>{TITLE}</th></tr>{ROWS}</table>";
  9. $html_table_row = "<tr>{CELLS}</tr>";
  10. $html_table_cell = "<td>{DATA}</td>";
  11.  
  12. // Create some random data. Could be replaced by database values
  13. $pages = array(
  14.     array(
  15.         "Title" => "First page",
  16.         "Tables" => array(
  17.             array(
  18.                 "Title" => "First table",
  19.                 "Rows" => array(
  20.                     array(
  21.                         "Cells" => array("Col1", "Col2", "Col3")
  22.                     ),
  23.                 )
  24.             ),
  25.             array(
  26.                 "Title" => "Second table",
  27.                 "Rows" => array(
  28.                     array(
  29.                         "Cells" => array("Col1", "Col2", "Col3")
  30.                     )
  31.                 )
  32.             )
  33.         )
  34.     )
  35. );
  36.  
  37. // Loop through each page
  38. foreach($pages as $_page)
  39. {
  40.     // Create all tables for this page
  41.     $tables = "";
  42.     foreach($_page['Tables'] as $_table)
  43.     {
  44.         // Create all rows for this table
  45.         $rows = "";
  46.         foreach($_table['Rows'] as $_row)
  47.         {
  48.             // Create all cells for this row
  49.             $cells = "";
  50.             foreach($_row['Cells'] as $_cell) {
  51.                 $cells .= str_replace("{DATA}", $_cell, $html_table_cell);
  52.             }
  53.             $rows .= str_replace("{CELLS}", $cells, $html_table_row);
  54.         }
  55.  
  56.         // Create the table
  57.         $old = array("{TITLE}", "{ROWS}");
  58.         $new = array($_table['Title'], $rows);
  59.         $tables .= str_replace($old, $new, $html_table);
  60.     }
  61.  
  62.     // Create the page
  63.     $old = array("{TITLE}", "{TABLES}");
  64.     $new = array($_page['Title'], $tables);
  65.     $page = str_replace($old, $new, $html_page);
  66.  
  67.     // Print the page
  68.     echo $page;
  69.  
  70.     // Or even; mail the page
  71.     mail("joe@example.com", "Something", $page, "From: me@example.com");
  72. }
  73. ?>
  74.  
I know, that's kind of a redundant way to create tables, but you get the point.
Aug 25 '08 #4

100+
P: 348
fjm
Yes, I would have to agree. That's one ugly piece of code :)
lol. I told you. :P Its just one of those things where after you code it and stand back and look at your work, you say.. "man, I'd better not quit my day job".

I appriciate the advice on the alternate syntax and the closing of the PHP tag. I am not certain that I understand what you mean by not closing the PHP tag. If I don't close the tag when I am using PHPs alternate syntax, I will get a parse error, no?

Atli, seperating my html from my php code is exactly what I have been trying to do but I haven't been very successful at it. I would love to pick your brain about how to achieve this. Would you please comment on my next example and how you might handle this?

For example. I have a class I call HTML. Inside this class I have blocks of code that are reused over and over to reconstruct different forms A typical line in any given method may look like:
[PHP]echo "<input type=\"text\" name=\"somthin\">\n";[/PHP]
I have a couple of hundred lines that look like this. I know there is a better way to do this. Some of the lines of code I pass varables into to dynamically change the css, table width, etc. I thought about just including the html but I would lose the ability to change things on the fly.

Would you recommend having a bunch of html files in a common directory and using them as includes in a process script and using PHP alternate syntax in the html? I have an example that I will use from last night. I have a GetMap method where I query the db and return the db fields to my process script. My original thought was to put the html for the GetMap() into the method and said "no".. let me find a different way to do this. What I did was to totally seperate the html by placing that map file into an include directory, use PHP alternate syntax where I wanted to echo variables and then, in my process script, call the return values from the class and finally include the map html. Would this a better way of doing it?

I have to admit, I am a bit on the obsessive side and have tried to maintain a beautified html source so my php winds up looking terrible. I am starting to think it should be the other way around. Beautify the php, not the html.

The code you posted is a huge help. I haven't done anything with it yet, but I will. Thank you Atli for taking the time to sort through my spaghetti code to formulate a great solution. Pbmods gave me the array solution also but I couldn't picture the end result. With your code I was able to see HOW you implemented those arrays. Thank you again Atli!
Aug 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.