如何将表单详细信息发送到数据库并发送包含表单详细信息的电子邮件 代码点火器.



这是我提交表单后的代码。我已经成功地使值显示在页面上,但随后我希望将这些相同的值插入到数据库中,同时将它们作为电子邮件发送给用户和管理员电子邮件。用户的电子邮件(如果表单的一部分$_POST值(。我想稍后提取并更新值。这不是问题。问题是实现保存到数据库,电子邮件客户端和管理员并同时或一个接一个地保存到数据库。

注意:我正在使用API在显示之前直接转换某些值,因此在控制器或模型中执行此操作可能会保存原始值,而不是不需要的转换值。除非有更好的方法可以做到这一点。我正在使用CodeIgniter 3。

请帮帮我。谢谢。代码如下:

<?php
    function generateRandomString($length = 26) {
        $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
        $charactersLength = strlen($characters);
        $randomString = '';
        for ($i = 0; $i < $length; $i++) {
            $randomString .= $characters[rand(0, $charactersLength - 1)];
        }
        return $randomString; 
    }
?>
<?php
    $user_id = $_POST['user_id'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $txn_ref = strtoupper(generateRandomString());
    $txn_status = "Pending";
    $exc_from = $_POST['exc_from'];
    $exc_to = $_POST['exc_to'];
    $from_amount = $_POST['from_amount'];
    $to_amount = $_POST['to_amount'];
    $account = $_POST['account'];
?>
<div class="row">
<h3>Your <?php echo $exc_from ?> to <?php echo $exc_to ?> Transaction Details</h3>
<div class="table-responsive">
 <table class="table table-striped">
  <tbody>
    <tr>
      <th>Payment ID</th>
      <td><?php echo $txn_ref ?></td>
    </tr>
    <tr>
      <th>Email</th>
      <td><?php echo $email ?></td>
    </tr>
    <tr>
      <th>Username</th>
      <td><?php echo $username ?></td>
    </tr>
    <tr>
      <th>You Give</th>
      <td><?php echo $from_amount ?> <?php echo $exc_from ?></td>
    </tr>
    <tr>
      <th>You Get</th>
      <td><?php echo $to_amount ?> <?php echo $exc_to ?></td>
    </tr>
    <tr>
      <th>Your <?php echo $exc_to ?> Wallet</th>
      <td><?php echo $account ?></td>
    </tr>
    <tr>
      <th>Transaction Status</th>
      <td><?php echo $txn_status ?></td>
      <td></td>
    </tr>
    <tr>
      <th>Make Payment to</th>
      <td><?php echo $ouraccount; ?></td>
      <td></td>
    </tr>
    <tr>
      <th>View in Dashboard</th>
      <td><a href="<?=base_url()?>transaction/<?php echo $txn_ref ?>">View Transaction</a><br></td>
      <td></td>
    </tr>
  </tbody>
</table>

对于任何对我问题的答案感兴趣的人,我做了一些工作,并找出了对我有用的方法。也许它可以帮助你。我对编辑持开放态度,因为我不太确定我是否遵循了最佳实践,但我遵循的大多数最佳实践都没有给出预期的结果。所有内容都按此顺序放入同一文件中。

<?php
function generateRandomString($length = 26) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString; }?>
<?php
$user_id = $_POST['user_id'];
$email = $_POST['email'];
$username = $_POST['username'];
$txn_ref = strtoupper(generateRandomString());
$txn_status = "Pending";
$exc_from = $_POST['exc_from'];
$exc_to = $_POST['exc_to'];
$from_amount = $_POST['from_amount'];
$to_amount = $_POST['to_amount'];
$account = $_POST['account'];
?>

在页面上显示

 <!-- Display Values -->
   <div class="row">
    <h3>Your <?php echo $exc_from ?> to <?php echo $exc_to ?> Details</h3>
    <div class="table-responsive">
   <table class="table table-striped">
    <tbody>
      <tr>
        <th>Payment ID</th>
      <td><?php echo $txn_ref ?></td>
      </tr>
      ...
         <th>View in Dashboard</th>
      <td><a href="<?=base_url()?>transaction/<?php echo $txn_ref ?>">View 
  Transaction</a><br></td>
       <td></td>
    </tr>
   </tbody>
  </table>
  </div>
  </div>

插入到数据库中

    <?php
  //Insert into Database
 $params = array(
'user_id' => $user_id ,
'email' => $email ,
'username' => $username ,
'txn_ref' => $txn_ref ,
'txn_status' => $txn_status ,
'exc_to' => $exc_to ,
'exc_from' => $exc_from ,
'from_amount' => $from_amount ,
'to_amount' => $to_amount ,
'account' => $account ,
);
$this->db->insert('transactions',$params);
return $this->db->insert_id();
?>

发送电子邮件

<?php
 //Send as Email
$m_user_id = $user_id;
$m_email = $email;
$m_username = $username;
$m_txn_ref = $txn_ref;
$m_exc_to = $exc_to;
$m_exc_from = $exc_from;
$m_from_amount = $from_amount;
$m_to_amount = $to_amount;
$m_account = $account;
$message = 'Message Here';
 $this->load->library('email');
 config['mailtype'] = 'html';
 $this->email->from('from@email.com', 'From');
 $this->email->to($m_email);
 $this->email->cc('cc@email.com');
 $this->email->subject('Email Test');
 $this->email->message($message);
 $this->email->send();
 if ($this->email->send(FALSE))
 {
    echo 'Mail was not sent';
 }
 ?>

我希望这有帮助

最新更新