While I'm just trying to pull up data from the db, as soon as the page shows up with the data it is already sending an email. How can I prevent it from sending the email right away. What I want to happen is I retrieved data from the db > shows it to a page > edit the page > update the db and send an email. This is the only point where I want an email to be sent out.

Here is my entire code,

<form action="assign_test1.php" method="post">

<?php

if(is_array($result)){
    echo '
    <fieldset>
        <legend>Assign Ticket</legend>
        <div>Changes will affect updated rows only.</div>
        <p></p>
        <table width=auto cellpadding=1px cellspacing=0px border=1 align=center id=assign>              
            <thead>
            <tr>';      

    // column comment from DB as column header
    foreach($result[0] as $key => $val){
        echo '<th align=center>'.$colcomments[$key].'</th>';
        }
    echo '
            </tr>
            </thead>
            <tbody>';
    foreach($result as $row => $info){
    echo '<tr>';
    foreach($info as $key => $val){
    if($key=='id'){
    echo '<td title="'.$colcomments[$key].'">'.$val.'.<input type="hidden" name="'.$key.'['.$info['id'].']" value="'.$val.'" id="rowid_'.$val.'" /></td>';
         }
    else {
    echo '<td title="'.$colcomments[$key].'"><input type="text" name="'.$key.'['.$info['id'].']" value="'.$val.'" /></td>';
         }
         }
    echo '</tr>'; 
         }
    echo '
            </tbody>
            </table>
    </fieldset>';

    function two_dim_array_to_html_table($arr, $colcomments){
    $ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
    $ret .= "\t<tr>\n";
    foreach($arr[0] as $key => $val){
        $ret .= "\t\t<th>".$colcomments[$key]."</th>\n";
        }
    $ret .= "\t</tr>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</tr>\n";
        }
    $ret .= "<table>\n";
    return $ret;
    }

    if($result) {
        $Body = "<html>\n"
            . "<head>\n"
            . "</head>\n"
            . "<body>\n"
            . two_dim_array_to_html_table($result, $colcomments)
            . "</body>\n"
            . "</html>\n";

    //Setting up Mail       
        $mail = new PHPMailer();
        if (EMAIL_USE_SMTP) {
            // Set mailer to use SMTP
            $mail->IsSMTP();
            //useful for debugging, shows full SMTP errors
            //$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
            // Enable SMTP authentication
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            // Enable encryption, usually SSL/TLS
            if (defined(EMAIL_SMTP_ENCRYPTION)) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            }
            // Specify host server
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
        } else {
            $mail->IsMail();
        }
        $mail->From = EMAIL_FROM_ADDRESS;
        $mail->FromName = EMAIL_FROM_NAME;
        $mail->AddAddress('test.test@domain.COM');
        $mail->Subject = 'Daily Tasks - "'.date('d-m-Y').'"';
        $mail->WordWrap = 100;
        $mail->IsHTML(true);
        $mail->Body = $Body;
        if $mail->Send();           
        }
}   
?>

<fieldset>
        <legend>Select Date</legend>
        <div>Select Date from and Date to</div>
        <p></p>
        <input type="date" name="from" id="from" value="<?=$date['from']; ?>" />
        <input type="date" name="to" id="to" value="<?=$date['to']; ?>" />
        <div><input type="submit" value="Submit" /></div>
</fieldset>
</form>
Member Avatar for diafol

What editor or IDE are you using? It didn't pick up line 95: if $mail->Send();
At least use a decent editor that would highlight this error - picking out errors like this is what you should be able to do.
OK, will have a look...

Sorry about that, it is a copy/paste error, I was trying to edit the code earlier when I am trying to fix it and I didn't noticed that it still exist. It should just be $mail->Send(); and I am just using Notepad++

Member Avatar for diafol

had a look

Your PHP and HTML are totally mashed up together. Makes if v difficult to read. Why is the function part of a conditional? Why is the function in the middle of your html?
Have a separate file for functions like this and just include them when required. Minimal php - the lightest touch - when outputting HTML.

You said this is all your code - but we have no idea what some of these variables are or where they came from.

You are sending mail if $result is true. We have no idea what this is.

This is all very confused. I think I mentioned in a previous thread of yours that you shouldn't send data to itself (same page) as that is just making problems for yourself (no reply to that as yet).

I'd suggest...

  1. Request info from DB and display in form.php
  2. Edit info and submit form to handler.php
  3. handler.php gets info from form, updates the DB, sends the email - checks everything is OK and returns you (via header()) to the page of your choice - with or without a confirmation message.

Do not try to do everything in one page. It's a noob mistake that we all make - thinking it is easier like this - it's not in the long run.

Sorry for that. I cant edit my code above so I'm posting an updated code here, I tried to re- arrange the code here now and added all the other information.

$update = filter_input_array(INPUT_POST, $update_args);
$date = filter_input_array(INPUT_POST, $date_args);
$result = NULL;
$colcomments = NULL;

$dsn = 'mysql:dbname='.DBname.';host='.DBhost.';port=';
try {
    $conn = new PDO($dsn, DBuser, DBpswd);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if($date['from'] !== NULL && $date['to'] !== NULL){

        // get table data
        if(isset($_POST['submit'])) {
            if(!isset($_SESSION['update'])) {

        $sql = 'SELECT `id`, `changeid`, `taskid`, `summary`, `type`, 
        `reviewed_approved_by`, `scheduled_start_date`, `implemented_by` 
        FROM `tracker` WHERE `scheduled_start_date` BETWEEN :d1 AND :d2';
        $stmt = $conn->prepare($sql);
        $stmt->bindParam(':d1', $date['from'], PDO::PARAM_STR);
        $stmt->bindParam(':d2', $date['to'], PDO::PARAM_STR);
        $stmt->execute();
        $result = $stmt->fetchAll(PDO::FETCH_ASSOC);
        // get column comments
        $sql = "select t.column_name, t.column_comment 
        from information_schema.columns t 
        where t.table_name = 'tracker'";
        $stmt = $conn->prepare($sql);
        $stmt->execute();
        $colcomments = $stmt->fetchAll(PDO::FETCH_KEY_PAIR);

        }else {

            unset($_SESSION['update']);

            function two_dim_array_to_html_table($arr, $colcomments){
    $ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n";
    $ret .= "\t<tr>\n";
    foreach($arr[0] as $key => $val){
        $ret .= "\t\t<th>".$colcomments[$key]."</th>\n";
        }
    $ret .= "\t</tr>\n";
    foreach($arr as $row){
        $ret .= "\t<tr>\n";
        foreach($row as $column){
            $ret .= "\t\t<td>".$column."</td>\n";
            }
        $ret .= "\t</tr>\n";
        }
    $ret .= "<table>\n";
    return $ret;
    }

    if($result) {
        $Body = "<html>\n"
            . "<head>\n"
            . "</head>\n"
            . "<body>\n"
            . two_dim_array_to_html_table($result, $colcomments)
            . "</body>\n"
            . "</html>\n";

    //Setting up Mail       
        $mail = new PHPMailer();
        if (EMAIL_USE_SMTP) {
            // Set mailer to use SMTP
            $mail->IsSMTP();
            //useful for debugging, shows full SMTP errors
            //$mail->SMTPDebug = 1; // debugging: 1 = errors and messages, 2 = messages only
            // Enable SMTP authentication
            $mail->SMTPAuth = EMAIL_SMTP_AUTH;
            // Enable encryption, usually SSL/TLS
            if (defined(EMAIL_SMTP_ENCRYPTION)) {
                $mail->SMTPSecure = EMAIL_SMTP_ENCRYPTION;
            }
            // Specify host server
            $mail->Host = EMAIL_SMTP_HOST;
            $mail->Username = EMAIL_SMTP_USERNAME;
            $mail->Password = EMAIL_SMTP_PASSWORD;
            $mail->Port = EMAIL_SMTP_PORT;
        } else {
            $mail->IsMail();
        }
        $mail->From = EMAIL_FROM_ADDRESS;
        $mail->FromName = EMAIL_FROM_NAME;
        $mail->AddAddress('test.test@domain.COM');
        $mail->Subject = 'Daily Tasks - "'.date('d-m-Y').'"';
        $mail->WordWrap = 100;
        $mail->IsHTML(true);
        $mail->Body = $Body;
        $mail->Send();

        }

    if(isset($update['id']) && is_array($update['id']) && !empty($update['id'])){
        $sql = "UPDATE `tracker`
            SET `changeid` = :bv_changeid
            ,`taskid` = :bv_taskid
            ,`summary` = :bv_summary
            ,`type` = :bv_type
            ,`reviewed_approved_by` = :bv_reviewed_approved_by
            ,`scheduled_start_date` = :bv_scheduled_start_date
            ,`implemented_by` = :bv_implemented_by
            WHERE `id` = :bv_id ";
        if($stmt = $conn->prepare($sql)){
            $stmt->bindParam(':bv_changeid', $changeid, PDO::PARAM_INT);
            $stmt->bindParam(':bv_taskid', $taskid, PDO::PARAM_INT);
            $stmt->bindParam(':bv_summary', $summary, PDO::PARAM_STR);
            $stmt->bindParam(':bv_type', $type, PDO::PARAM_STR);
            $stmt->bindParam(':bv_reviewed_approved_by', $reviewed_approved_by, PDO::PARAM_STR);
            $stmt->bindParam(':bv_scheduled_start_date', $scheduled_start_date, PDO::PARAM_STR);
            $stmt->bindParam(':bv_implemented_by', $implemented_by, PDO::PARAM_STR);
            $stmt->bindParam(':bv_id', $id, PDO::PARAM_INT);
            $updateRowCount = 0;
            // update multiple rows - all of selected in form
            foreach($update['id'] as $key => $val){
                $changeid = $update['changeid'][$val];
                $taskid = $update['taskid'][$val];
                $summary = $update['summary'][$val];
                $type = $update['type'][$val];
                $reviewed_approved_by = $update['reviewed_approved_by'][$val];
                $scheduled_start_date = $update['scheduled_start_date'][$val];
                $implemented_by = $update['implemented_by'][$val];
                $id = $val;
                $stmt->execute();
                $updateRowCount += $stmt->rowCount();
                }
            if($updateRowCount > 0){
                $message['info'][] = "Updated ".$updateRowCount." row/s";
                }
            else {
                $message['warning'][] = "CRQ Tracker db not updated.";
                }
            }
        else {
            $message['error'][] = "Prepare error!!!";
            }
        }   
        }

        }else {

            if(is_array($result)){
    echo '
    <fieldset>
        <legend>Assign Ticket</legend>
        <div>Changes will affect updated rows only.</div>
        <p></p>
        <table width=auto cellpadding=1px cellspacing=0px border=1 align=center id=assign>

            <thead>
            <tr>';      

    // column comment from DB as column header
    foreach($result[0] as $key => $val){
        echo '<th align=center>'.$colcomments[$key].'</th>';
        }
    echo '
            </tr>
            </thead>
            <tbody>';
    foreach($result as $row => $info){
    echo '<tr>';
    foreach($info as $key => $val){
    if($key=='id'){
    echo '<td title="'.$colcomments[$key].'">'.$val.'.<input type="hidden" name="'.$key.'['.$info['id'].']" value="'.$val.'" id="rowid_'.$val.'" /></td>';
         }
    else {
    echo '<td title="'.$colcomments[$key].'"><input type="text" name="'.$key.'['.$info['id'].']" value="'.$val.'" /></td>';
         }
         }
    echo '</tr>'; 
         }
    echo '
            </tbody>
            </table>
    </fieldset>';
    }   
        }
        }
        }
    }
catch(PDOException $e){
    $message['error'][] = $e->getMessage();
    }
Member Avatar for diafol

Getting a bit of trouble working this out. Is this how it works?

  1. User chooses dates in form and submits
  2. Data selected from DB and sent to form
  3. User manipulates data in form and submits (OR new dates selected and submitted as in step 2)
  4. Data updated in DB and email sent
  5. Return to dates form

//EDIT

foreach($result[0] as $key => $val){
        echo '<th align=center>'.$colcomments[$key].'</th>';
        }
    echo '
            </tr>
            </thead>
            <tbody>';
    foreach($result as $row => $info){
    echo '<tr>';

Using $result in two loops - won't the second loop include the table header data as well?

This is how it is supposed to be

User chooses dates in form and submits
Data selected from DB and sent to form
User manipulates data in form and submits
Data updated in DB and email sent
Return the form with the updated value

I was able to fix this though using another button for send mail. And checks if the button is clicked before sending the email.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.