繁体   English   中英

如何将表单详细信息发送到数据库以及如何发送包含表单详细信息的电子邮件CodeIgniter

[英]How to send form details to database and send email with form details CodeIgniter

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

注意:我正在使用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';
 }

 ?>

我希望这有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM