Email the contents of a dynamic webpage generated by php

That worked! Here is the code I ended up with for the form:


echo ("<form action=\\"reports-email2.php\\" method=\\"post\\" name=\\"Email-report\\" id=\\"Email-report\\">");
									
echo ("<table width=\\"350\\" border=\\"0\\" cellspacing=\\"1\\" cellpadding=\\"2\\">");
	echo ("<tr>");
$time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
$time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : '';
echo ("<input type=\\"hidden\\" name=\\"time1\\" value=\\"$time1\\">");
echo ("<input type=\\"hidden\\" name=\\"time2\\" value=\\"$time2\\">");
echo ("<input type=\\"hidden\\" name=\\"send_mail\\" value=\\"1\\">");
		echo ("<td width=\\"100\\"><input type=\\"submit\\" name=\\"send_data1\\" value=\\"Email Report\\"></td>");

	echo ("</tr>");
echo ("</table>");
echo ("</form>");

Can you explain what these lines of code are doing? I haven’t seen something like this in the php code I have written so far and would like to understand it better.


$time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
$time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : '';

Thanks for all of your help!

This is the ternal operator (if i’m not mistaken)
( condition(s) ) ? (if its true) : ( if its false)


$time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
$time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : '';

Which is equivalent with an IF and else comdition:

if(isset($_POST["time1"]))
{
$time1=htmlspecialchars($_POST["time1"]);
}
else
{
$time1='';  // You need to initialize the $time1 value
}

And imagine the $time2 version based on the first example.
In a nutshell: if the time1 has set lets add its value into $time in other case add an empty string.

And htmlspecialchars is to convert the inputted values as its html entities, to protect your html from XSS injections.

Thank you so much for all of the help and the great explanation. That makes alot more sense now. Do you know if there is a way to keep the css styles that are applied to the table when it is emailed? I can always just use the <b> tag and align=‘center’ if I have to but it would be great if the stylesheet would still apply to the formatting in the email. Thanks again!!

I think its allowed to attach a link to a CSS stylesheet file which is on your server.

<link href="http://yourserver.com/mailcss.css" rel="stylesheet" type="text/css" />

Have you considered intercepting the report HTML from the output buffer?

No. I am not sure exactly how you do that either. Would you care to fill me in on that process and how to do it? Is that a better way?

How about take a checkbox in the main form, and say: check this if you want to the report would be emailed. And you can solve with only one form.

I like your idea. So basically if the box is checked when the user clicks the “Get Report” button (“send_data1”) the report would populate the screen and automatically be emailed without the need of clicking another button to send it.

Another idea I would like to implement is if the box to email the form is checked then the below it a list of possible email addresses would show up with check boxes beside each email address. I want to allow the user to select from a list of email addresses who to send the report to. So if a box is checked that email address would be added to the $to variable in the mail function and the email would be sent to only the email addresses selected.

multiple select box then would be a good idea. I often make contact lists with array or by database. Where you visible the names, and their email address stored in an array.

I had the email functionality of this report working great and then the client decided they wanted to change how the emailing of the report worked. This link shows how the page was working: http://www.mccawphotographics.com/qc/reports-final-div-forum.php The user could check the box that said “Check if you want to email this report.” and then when they hit the “Get Report” button the report would show and would email to the selected emails at the same time.

Now the client wants to have the user select the dates and hit “Get Report” When the report shows up they also want the “Email Report” button and the “Check if you want to email this report.” checkbox to show up below the report.

So I moved the checkbox and added a button to send the email (the button is named “send_report”). When testing this new page the email that is sent is blank and doesn’t contain the report. I am not sure why moving the checkbox and adding another button is messing up the email. I have attached the working file. The working file is named: reports-final-div-forum-sample.php. The file with my changes is named: reports-final-div3-forum-sample.php.

I moved the checkbox to below the output of the report and below shows it at the bottom of the code after the output of the report:


ob_start();
								
echo ("<h5>Start Date: " . date('F j, Y',strtotime($time1)) . " || End Date: " . date('F j, Y',strtotime($time2)) . "</h5>");
echo ("<table width='650' cellpadding='2' cellspacing='1' border='1' class='table'>");
     echo ("<tr align='center'>");
          echo ("<td class='bold'><b>Wire Gauge</b></td>");
          echo ("<td colspan='3' class='bold'><b>Beam Pull</b></td>");
          echo ("<td colspan='3' class='bold'><b>Fracture Point</b></td>");
     echo ("</tr>");
     echo ("<tr align='center'>");
            echo ("<td>&nbsp;</td>");
            echo ("<td class='bold'><b>MIN</b></td>");
            echo ("<td class='bold'><b>MAX</b></td>");
            echo ("<td class='bold'><b>AVG</b></td>");
            echo ("<td class='bold'><b>Anchor Bend</b></td>");
            echo ("<td class='bold'><b>Break Back</b></td>");
            echo ("<td class='bold'><b>Weld</b></td>");
      echo ("</tr>");
      echo ("<tr align='center'>");
            echo ("<td class='bold'><b>2 Gauge Loop</b></td>");
            echo ("<td>" . $beamPullmin['2.0'] . "</td>");
            echo ("<td>" . $beamPullmax['2.0'] . "</td>");
            echo ("<td>" . round($beamPullavg['2.0']) . "</td>");
            echo ("<td>" . round(($fractures['Anchor Bend']['2.0']/$total_2ga) * 100) . "%" . "</td>");
            echo ("<td>" . round(($fractures['Break Back']['2.0']/$total_2ga) * 100) . "%" . "</td>");
            echo ("<td>" . round(($fractures['Weld']['2.0']/$total_2ga) * 100) . "%" . "</td>");
       echo ("</tr>");
echo ("</table>");
										
$output=ob_get_contents();
ob_end_clean();
									
echo $output;
																			
echo ("<form action=\\"\\" method=\\"post\\" name=\\"Email-report\\" id=\\"Email-report\\">");
	echo ("<div>");
		echo ("<div>");
                  $time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
		  $time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : '';
		  echo ("<input type=\\"hidden\\" name=\\"time1\\" value=\\"$time1\\">");
		  echo ("<input type=\\"hidden\\" name=\\"time2\\" value=\\"$time2\\">");
		
	echo ("</div>");
											
// Mail Check Box //

        echo ("<div class=\\"check\\">");
             echo ("<input type=\\"checkbox\\" name=\\"mail_check\\" id=\\"mail_check\\" value=\\"email\\"");
		  if($mail_check=='1'){ echo "checked=\\"true\\"";}
	echo ("/>");
        echo ("Check if you want to email this report.");
        echo ("</div>");

        echo ("<div class=\\"clear\\"></div>");

        // Hidden Email Addresses //
        echo ("<div class=\\"check\\" id=\\"addresses\\">");
                           	
        $sql = "SELECT * FROM email ORDER BY email_drop ASC";
        $result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);

        while($row = mysql_fetch_assoc($result))
	        {
                echo ("<input type=\\"checkbox\\" name=\\"emails[]\\" checked " . " id=\\"" . $row['id'] . "\\"" . " value=\\"" . $row['email_drop'] . "\\" />");
	        echo ("&nbsp;");
	        echo ($row['name']);
	        echo ("<br />");
	         }
									
        echo ("</div>");
        echo ("<div class=\\"clear\\"></div>");
        echo ("<div class=\\"controls2\\">");
                echo ("<input type=\\"submit\\" name=\\"send_report\\" id=\\"send_report\\" value=\\"Send Report\\">");
        echo ("</div>");
						
echo ("</div>");
echo ("</form>");

Here is how the email portion changed:


if (isset($_POST['mail_check']) && isset($_POST['send_report'])){

All of the above code is in the file named “reports-final-div3-forum-sample.php” as well.

Any ideas on how to fix this would be great!
Thanks.

Watch this video to get the answer:slight_smile: