SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 32
  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Email the contents of a dynamic webpage generated by php

    I have a project in which I generate a report from a database and print it out into a table on the screen. I want to make it possible for the user to email the report that was just generated to someone else. I thought I could re-create the table in the $message of the email. Unfortunately I haven't been able to get the variables/arrays to output the values. I have attached a screenshot of the report to show how I want it to look in the email.
    Here is a sample of what I am trying to do:
    PHP Code:
    <?php 
    if (isset($_POST['sendmail'])){
                                    
                                    
    $message "<h5>Start Date: " date('F j, Y',strtotime($time1)) . " || End Date: " date('F j, Y',strtotime($time2)) . "</h5>"
    "<table width='950' cellpadding='2' cellspacing='1' border='1' class='table'>"
    "<tr>"
    "<td class='bold'>Wire Gauge</td>"
    "<td colspan='3' class='bold'>Beam Pull</td>"
    "<td colspan='3' class='bold'>Fracture Point</td>"
    "</tr>"
    "<tr>
      <td>&nbsp;</td>
      <td class='bold'>MIN</td>
      <td class='bold'>MAX</td>
      <td class='bold'>AVG</td>
      <td class='bold'>Anchor Bend</td>
      <td class='bold'>Break Back</td>
      <td class='bold'>Weld</td>
    </tr>
    <tr>
      <td class='bold'>2 Gauge Loop</td>
      <td>" 
    $beamPullmin['2.0'] . "</td>"
      
    "<td>" . echo $beamPullmax['2.0'] . "</td>"
      
    "<td>" . echo $beamPullavg['2.0'] . "</td>"
      
    "<td></td>
      <td></td>
      <td></td>
    </tr>
    </table>"
    ;

        
    $to "melinda@mccawphotographics.com";
        
        
    $headers .= "X-Mailer: PHP4\n" // mailer
        
    $headers .= "X-Priority: 3\n" ;  // priority
        
    $headers .= "Return-Path:  $to\n";
        
    $headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\n";
        
    $headers .= "Content-Type: text/html; charset=us-ascii\n";
        
        
    mail("melinda@mccawphotographics.com""QC Reports"$message,$headers);
    }
        
    ?>
    Am I even close to being on the right track to solving this? If so, how do I get it to output the values? Is there an easier or better solution?
    Attached Images Attached Images

  2. #2
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Unfortunately I haven't been able to get the variables/arrays to output the values.
    I guess you would have to perform the same database query that you performed originally, but that part of the script is missing?
    Dan G
    Marketing Strategist & Consultant

  3. #3
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Is the $beamPullxxx variables the stuff you are grabbing from your database
    Ryan B | My Blog | Twitter

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it is. Do you need to see the sql query or code for the report?

  5. #5
    doing my best to help c2uk's Avatar
    Join Date
    May 2005
    Location
    Cardiff
    Posts
    1,832
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    and where do you populate this variable within this part of the script with actual data? you've got to populate it the same way you've populated the original report.

    if you don't know how, then showing us the part of the script that deals with the report might be best - it may also be useful if you could explain us your overall setting and how the script is designed so far (both times with regards to the report output).
    Dan G
    Marketing Strategist & Consultant

  6. #6
    SitePoint Wizard rguy84's Avatar
    Join Date
    Sep 2005
    Location
    Durham, NC
    Posts
    1,659
    Mentioned
    5 Post(s)
    Tagged
    0 Thread(s)
    Yes.
    You will need to preform the query within there. You are basically trying to tell php to give you variables that only exist inside a loop outside a loop. So beamPullxxx does not exist in PHP's eyes when you called them there.
    Ryan B | My Blog | Twitter

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am still a beginner at php and mysql so I am not sure I totally understand what you are saying. Are you saying I need to run the same query within the $message variable for sending the email?

    Let me try to explain the project better. I have a form with 2 drop down menus. The first is start date and the 2nd is End date. The user selects the start and end date for the report they want to run. Next they click the "Get Report" Button and the report is echoed on the page below. What I want to do is add the functionality to allow the user to email the report that is now on the screen to their superior.

    Here is the code for my query:
    PHP Code:
    $conn mysql_connect("localhost","user","password") or trigger_error("SQL"E_USER_ERROR);
    $db mysql_select_db("database"$conn) or trigger_error("SQL"E_USER_ERROR);
                                                                    
    //query of min, max and avg for wire gauge 2, 3, 4 and 4.5 with endType Loop
    $sql="SELECT wireGauge, MIN(beamPull) AS `min`, MAX(beamPull)AS `max`, AVG(beamPull) AS `avg`
    FROM welded_wire_ties
    WHERE time between '
    $time1' and '$time2'
    AND endType = 'Loop'
    GROUP BY wireGauge"
    ;
                                    
    $result mysql_query($sql$conn) or trigger_error("SQL"E_USER_ERROR);
                                    
    while(
    $row mysql_fetch_array($result)) {
    $beamPullmin[$row['wireGauge']] = $row['min'];
    $beamPullmax[$row['wireGauge']] = $row['max'];
    $beamPullavg[$row['wireGauge']] = $row['avg'];

    Here is the code to output the report (I am just showing part of the table since it is large and redundant in many parts):
    HTML Code:
    echo ("<h5>Start Date: " . date('F j, Y',strtotime($time1)) . " || End Date: " . date('F j, Y',strtotime($time2)) . "</h5>");
    echo ("<table width='950' cellpadding='2' cellspacing='1' border='1' class='table'>");
            echo ("<tr>");
                    echo ("<td>");
                    echo $beamPullmin['2.0'];
                    echo ("</td>");
                    echo ("<td>");
                    echo $beamPullmax['2.0'];
                    echo ("</td>");
                    echo ("<td>");
                    echo $beamPullavg['2.0'];
                    echo ("</td>");
            echo ("</tr>");
            echo ("<tr>");
                    echo ("<td>");
                    echo $beamPullmin['3.0'];
                    echo ("</td>");
                    echo ("<td>");
                    echo $beamPullmax['3.0'];
                    echo ("</td>");
                    echo ("<td>");
                    echo $beamPullavg['3.0'];
                    echo ("</td>");
            echo ("</tr>");
    ech ("</table>");
    Thanks!

  8. #8
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can redirect the output into a normal variable.

    PHP Code:
    ob_start();
    echo 
    "<table>";
    echo 
    "<....>";
    echo 
    "<....>";
    echo 
    "</table>";
    $output=ob_get_contents();
    ob_end_clean();

    echo 
    $output
    and then email if its needed.

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So would I wrap this code around the table that outputs the data and then put the $output variable in the message of the email such as $message = "$output";?

    I actually tried that and it didn't work. Any thoughts on what I did wrong?

  10. #10
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Test this code on a short example, and see the results.

    You should see the examples and better explanations here: ob_start()
    And: ob_get_contents()

  11. #11
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I actually have the echo $output working. It echoes the table and all data on the page. When I try to put the variable of $output into the message part of my email and test it I get a blank email without the table and the data. Can you see anything wrong with how my mailer is working? Here is my code:
    PHP Code:
    if (isset($_POST['sendmail'])){
                                    
    $message "$output";
    $to "email@address.com";

    $headers .= "X-Mailer: PHP4\n" // mailer
    $headers .= "X-Priority: 3\n" ;  // priority
    $headers .= "Return-Path:  $to\n";
    $headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\n";
    $headers .= "Content-Type: text/html; charset=us-ascii\n";
        
    mail("email@address.com""QC Reports for $time1 to $time2"$message,$headers);
        


  12. #12
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    make sure the $output has the table data:

    The table doesn't appear:
    PHP Code:
    echo htmlspecialchars($output); 
    And add its values to the $message, like:
    PHP Code:
    $message $output;  // qoutes only been used if you're creating strings 

  13. #13
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just figured out that if I take my mail out of my isset statement that it works perfectly. Any idea why it won't work inside the isset statement?

  14. #14
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    test your $_POST array , and check the contents you get:
    Code PHP:
    print "<pre>";
    print_r($_POST);
    print "</pre>";

    especally your submit button is in with the index name: sendmail ?

  15. #15
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Any idea why it won't work inside the isset statement?
    Variable is not set

  16. #16
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I tested my $_POST array and got the following:
    Array
    (
    [sendmail] => Email Report
    )

    When you say "Variable is not set" are you talking about $message?

    To explain what happens: I have a form with 2 drop downs "time1" and "time2" (start date and end date) Once the user select the start and end date for the report they click "Get Report" (named "send_data1"). If there are no validation errors the report is printed out as well as the "Email Reports" button (named "sendmail"). The problem I am having is getting the email to contain the exact table that was on the screen.
    My current code looks like this:
    PHP Code:
    if (isset($_POST['send_data1'])){
                                    
    $time1=$_POST['time1'];
    $time2=$_POST['time2'];
    $error=NULL;
                                    
    // validation of time1
    if ($_POST['time1'] == 'null') {
    $time1 FALSE;
    $error "<span class='red>Please select a Start Date.</span>";
    }
                                    
    // validation of time2
    else if ($_POST['time2'] == 'null') {
    $time2 FALSE;
    $error "<span class='red>Please select an End Date.</span>";
    }
                                    
    if(
    is_null($error)){
                                    
    $wireGauge=$_POST['wireGauge'];
    $time1=$_POST['time1'];
    $time2=$_POST['time2'];

    $conn mysql_connect("localhost","user","password") or trigger_error("SQL"E_USER_ERROR);

    $db mysql_select_db("database"$conn) or trigger_error("SQL"E_USER_ERROR);

                                                                    

    //query of min, max and avg for wire gauge 2, 3, 4 and 4.5 with endType Loop

    $sql="SELECT wireGauge, MIN(beamPull) AS `min`, MAX(beamPull)AS `max`, AVG(beamPull) AS `avg`
    FROM welded_wire_ties
    WHERE time between '
    $time1' and '$time2'
    AND endType = 'Loop'
    GROUP BY wireGauge"
    ;

    $result mysql_query($sql$conn) or trigger_error("SQL"E_USER_ERROR);
    while(
    $row mysql_fetch_array($result)) {
    $beamPullmin[$row['wireGauge']] = $row['min'];
    $beamPullmax[$row['wireGauge']] = $row['max'];
    $beamPullavg[$row['wireGauge']] = $row['avg'];


    ob_start();

    echo (
    "<h5>Start Date: " date('F j, Y',strtotime($time1)) . " || End Date: " date('F j, Y',strtotime($time2)) . "</h5>");
    echo (
    "<table width='950' cellpadding='2' cellspacing='1' border='1' class='table'>");
            echo (
    "<tr>");
                    echo (
    "<td>");
                    echo 
    $beamPullmin['2.0'];
                    echo (
    "</td>");
                    echo (
    "<td>");
                    echo 
    $beamPullmax['2.0'];
                    echo (
    "</td>");
                    echo (
    "<td>");
                    echo 
    $beamPullavg['2.0'];
                    echo (
    "</td>");
            echo (
    "</tr>");
            echo (
    "<tr>");
                    echo (
    "<td>");
                    echo 
    $beamPullmin['3.0'];
                    echo (
    "</td>");
                    echo (
    "<td>");
                    echo 
    $beamPullmax['3.0'];
                    echo (
    "</td>");
                    echo (
    "<td>");
                    echo 
    $beamPullavg['3.0'];
                    echo (
    "</td>");
            echo (
    "</tr>");
    echo (
    "</table>");
    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>");
            echo (
    "<td width=\"100\"><input type=\"submit\" name=\"send_report\" value=\"Email Report\"></td>");
        echo (
    "</tr>");
    echo (
    "</table>");
    echo (
    "</form>");

    $output=ob_get_contents();
    ob_end_clean();
                                        
    echo 
    $output;
         }
    }

    if (isset(
    $_POST['sendmail'])){

    $message $output;
    $to "email@address.com";

    $headers .= "X-Mailer: PHP4\n" // mailer
    $headers .= "X-Priority: 3\n" ;  // priority
    $headers .= "Return-Path:  $to\n";
    $headers .= "Origin: ".$_SERVER['REMOTE_ADDR']."\n";
    $headers .= "Content-Type: text/html; charset=iso-8859-1\n";

    mail("email@address.com""QC Reports for $time1 to $time2"$message,$headers);


  17. #17
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have two submit buttons, and have two cases.

    in a case you press:
    send_data1 the dates will be countered (maybe)

    Then you have another form and another submit button: sendmail

    These two forms don't have a union. And you might don't save the entered date1 and date2 back to the form => the form values then erased on the next submit when you press email.

    If i will have a problem with my projects, i would post the relevant codes to the others who would like to help , or i give a test link where we can see how your form outputted.

    I don't like the "minimal info" games.

  18. #18
    Non-Member
    Join Date
    Oct 2009
    Posts
    1,852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When you say "Variable is not set" are you talking about $message?
    doesn't matter.
    The isset() function checks if particular variable set. if it returns false, that just means that passed variable is not set.

  19. #19
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you would like to see the form in action here is a link to it: http://www.mccawphotographics.com/qc/reports-email4.php I provided the code that I thought was relevant in my last post. I have also attached the entire php file.

    I agree that the two forms don't have a union. But being very new to php I don't yet understand how make the two forms have a union. Any help in figuring this out would be greatly appreciated.
    Attached Files Attached Files

  20. #20
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You should sate the $_POST["time1"] and $_POST["time2"] into your "send email "form:

    PHP Code:
    <?php
    $time1
    =isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
    $time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : '';
    ?>
    <input type="hidden" name="time1" value="<?php echo $time1?>">
    <input type="hidden" name="time2" value="<?php echo $time2?>">
    <input type="hidden" name="send_mail" value="1">
    <input type="submit" name="send_data1" value="Send mail">
    </form>
    The form action is ok.

    So what would it be do:
    if you select a time1 and time2 this selects the report, and you will see the Email report button. If you press you will repeat the previous post with the exact data, but in a hidden field you tell the processor program to send the email too.

  21. #21
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That worked! Here is the code I ended up with for the form:
    PHP Code:
    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.
    PHP Code:
    $time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
    $time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : ''
    Thanks for all of your help!

  22. #22
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This is the ternal operator (if i'm not mistaken)
    ( condition(s) ) ? (if its true) : ( if its false)
    PHP Code:
    $time1=isset($_POST["time1"]) ? htmlspecialchars($_POST["time1"]) : '';
    $time2=isset($_POST["time2"]) ? htmlspecialchars($_POST["time2"]) : ''
    Which is equivalent with an IF and else comdition:

    PHP Code:
    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.

  23. #23
    SitePoint Enthusiast
    Join Date
    Dec 2009
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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!!

  24. #24
    SitePoint Enthusiast
    Join Date
    Nov 2009
    Posts
    70
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think its allowed to attach a link to a CSS stylesheet file which is on your server.
    Code HTML4Strict:
    <link href="http://yourserver.com/mailcss.css" rel="stylesheet" type="text/css" />

  25. #25
    SitePoint Zealot
    Join Date
    Jul 2007
    Location
    Macomb, MI USA
    Posts
    138
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you considered intercepting the report HTML from the output buffer?
    inClick Ad Server - Start Your Own Performance Based Ad Network
    Key Features | Download inClick Now | Free Ad Server Hosting Offer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •