473,396 Members | 1,895 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to send an entire html form by email

348 100+
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
4 2529
Atli
5,058 Expert 4TB
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
fjm
348 100+
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
5,058 Expert 4TB
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
fjm
348 100+
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

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

Similar topics

5
by: Saiyan Vejita | last post by:
I've been working with ASP for 6+ years now, but in that time I've never had any solid advice on how to build a secure system. So I thought I'd ask here first. Anyway, here's the situation: I...
1
by: Kenshin | last post by:
Hey! I have another script where i pull all the information from the database and I want to send it to the person. What they do is they enter in their email, and if the email matches, than it will...
6
by: DigitalRick | last post by:
I have been running CDONTS in my ASPpages to send emails to me sent from my guestbook. It had been working fine untill I upgraded to Server 2003 (I am also running Exchange 2003) all locally. I...
1
by: Miguel Dias Moura | last post by:
Hello, Can you help me out in making this work? What I want is as simple as sending form values to an email. The code I am using is the following:
9
by: eswanson | last post by:
I have a web page I need to post a file plus some other fields to it. How can I do this from a asp.net page. I know I can send individual fields to the other page, but how do I send a file to the...
7
by: Martin | last post by:
Hi, I have a standard aspx page (form) that contains a few user controls. Upon form submission the page is validated. If validation passses then a text based email is sent. This is all working...
2
by: kennykenn | last post by:
Hi, Ive producd code to send an email after capturing info off a form,it works fine locally but when i put it live it doesnt work! the code is stopin at 'msg.send' any ideas, here the code! ...
4
by: chris | last post by:
I need to maintain a list of subscribers to an email list for a "newsletter" that will be sent via a web form probably once a month. I anticipate low numbers--tens to maybe one hundred subscribers...
1
by: Michael31277 | last post by:
I keep getting this error on my form code, but only when I keep the MailNewObj.Send in the code, so I know it is this... I get the 500 INTERNAL ERROR message.. I tried shutting off "friendly...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.