[英]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.