php - PHPMail sending email right away -
while i'm trying pull data db, page shows data sending email. how can prevent sending email right away. want happen retrieved data db > shows page > edit page > update db , send email. point want email sent out.
here entire edited code per suggestion,
$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(isset($_post['submit'])) { if(!isset($_session['update'])) { $_session['update'] = true; //retrieve values database if($date['from'] !== null && $date['to'] !== null){ // table data $sql = 'select `id`, `changeid`, `tracker` `scheduled_start_date` between :d1 , :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); } }else { unset($_session['update']); //put mail function here function two_dim_array_to_html_table($arr){ $ret = "<table border='1' width='auto' cellpadding='1px' cellspacing='0px' align='center'>\n"; $ret .= "\t<tr>\n"; foreach($arr[0] $key => $val){ $ret .= "\t\t<th>".$colcomments[$key]."</th>\n"; } $ret .= "\t</tr>\n"; foreach($arr $row){ $ret .= "\t<tr>\n"; foreach($row $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 mail $mail = new phpmailer(); if (email_use_smtp) { // set mailer use smtp $mail->issmtp(); //useful debugging, shows full smtp errors //$mail->smtpdebug = 1; // debugging: 1 = errors , messages, 2 = messages // enable smtp authentication $mail->smtpauth = email_smtp_auth; // enable encryption, 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(); } //update database records if(isset($update['id']) && is_array($update['id']) && !empty($update['id'])){ $sql = "update `tracker` set `changeid` = :bv_changeid `id` = :bv_id "; if($stmt = $conn->prepare($sql)){ $stmt->bindparam(':bv_changeid', $changeid, pdo::param_int); $stmt->bindparam(':bv_id', $id, pdo::param_int); $updaterowcount = 0; // update multiple rows - of selected in form foreach($update['id'] $key => $val){ $changeid = $update['changeid'][$val]; $id = $val; $stmt->execute(); $updaterowcount += $stmt->rowcount(); } if($updaterowcount > 0){ $message['info'][] = "updated ".$updaterowcount." row/s"; } else { $message['warning'][] = "tracker db not updated."; } } else { $message['error'][] = "prepare error!!!"; } } } }else { //show normal calender/form if(is_array($result)){ echo ' <fieldset> <legend>assign</legend> <div>changes affect updated rows only.</div> <p></p> <table width=auto cellpadding=1px cellspacing=0px border=1 align=center id=assign> <thead> <tr>'; // column comment db column header foreach($result[0] $key => $val){ echo '<th align=center>'.$colcomments[$key].'</th>'; } echo ' </tr> </thead> <tbody>'; foreach($result $row => $info){ echo '<tr>'; foreach($info $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>'; } } }
here entire code submit
button,
<html> <head> <title>ticket assignment</title> </head> <body class="onecolfixctrhdr"> <script> </script> <div id="maincontent"> <?php foreach($message $key => $val){ // show error, warning , info messages if(!empty($val)){ echo '<div style="margin:4px;background:'.$bgcol[$key].';border:2px solid grey;border-radius:8px;">'; foreach($val $item){ echo '<div style="margin:8px;">'.$item.'</div>'; } echo '</div>'; } } ?> <div> <form action="assign_test1.php" method="post"> <fieldset> <legend>select date</legend> <div>select date , 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" name="submit" id="submit" value="submit"/></div> </fieldset> </form> </div> </div> </body> </html>
the code provided doesn't have explain how should work
if(isset($_post['submit'])) { if(!isset($_session['update'])) { $_session['update'] = true; //retrieve values database }else { unset($_session['update']); //put mail function here //update database records } }else { //show normal calender/form }
i hope helps you.
if need else, please let me know. here help. luck!
Comments
Post a Comment