NenTang.vn
Thiết kế Lập trình Web BackendTạo trang web Bán hàng Thương mại điện tử Sử dụng PHP MySQL và render giao diện với template TWIG
Thực hiện Trang Liên hệ (có gởi mail thông báo)
Chương 6 · Bài 6
Dự án thực tế mẫu (TWIG template) - Trang web bán hàng trực tuyến - Thiết kế Frontend
Thời lượng: 79 phútĐọc khoảng: 3 phút
Từ viết tắt
Nội dung bài học không khai báo từ viết tắt bằng thẻ abbr.
Nội dung bài học
Step 1: tạo file xử lý Logic cho Trang Liên hệ
- Tạo file
frontend/pages/contact.php
php
1<?php2// Include file cấu hình ban đầu của `Twig`3require_once __DIR__.'/../../bootstrap.php';4 5// Truy vấn database để lấy danh sách6// 1. Include file cấu hình kết nối đến database, khởi tạo kết nối $conn7include_once(__DIR__.'/../../dbconnect.php');8 9use PHPMailer\PHPMailer\PHPMailer;10use PHPMailer\PHPMailer\Exception;11 12if(!isset($_POST['btnGoiLoiNhan']))13{14 // Yêu cầu `Twig` vẽ giao diện được viết trong file `frontend/pages/contact.html.twig`15 // với dữ liệu truyền vào file giao diện được đặt tên16 echo $twig->render('frontend/pages/contact.html.twig');17 return;18}19 20// Lấy dữ liệu người dùng hiệu chỉnh gởi từ REQUEST POST21$email = $_POST['email'];22$title = $_POST['title'];23$message = $_POST['message'];24 25// Gởi mail kích hoạt tài khoản26$mail = new PHPMailer(true); // Passing `true` enables exceptions27try {28 //Server settings29 //$mail->SMTPDebug = 2; // Enable verbose debug output30 $mail->isSMTP(); // Set mailer to use SMTP31 $mail->Host = 'smtp.gmail.com'; // Specify main and backup SMTP servers32 $mail->SMTPAuth = true; // Enable SMTP authentication33 $mail->Username = '[email protected]';// SMTP username34 $mail->Password = 'apmcxgzjndlbjybj'; // SMTP password35 $mail->SMTPSecure = 'tls'; // Enable TLS encryption, `ssl` also accepted36 $mail->Port = 587; // TCP port to connect to37 $mail->CharSet = "UTF-8";38 39 // Bật chế bộ tự mình mã hóa SSL40 $mail->SMTPOptions = array(41 'ssl' => array(42 'verify_peer' => false,43 'verify_peer_name' => false,44 'allow_self_signed' => true45 )46 );47 48 //Recipients49 $mail->setFrom('[email protected]', 'Test Mail');50 $mail->addAddress('[email protected]'); // Add a recipient51 $mail->addReplyTo($email);52 // $mail->addCC('[email protected]');53 // $mail->addBCC('[email protected]');54 55 //Attachments56 // $mail->addAttachment('/var/tmp/file.tar.gz'); // Add attachments57 // $mail->addAttachment('/tmp/image.jpg', 'new.jpg'); // Optional name58 59 //Content60 $mail->isHTML(true); // Set email format to HTML61 $mail->Subject = "[Có người liên hệ] - $title";62 $siteUrl = siteURL();63 $body = <<<EOT64 Có người liên hệ cần giúp đỡ. <br />65 Email của khách: $email <br />66 Nội dung: <br />67 $message68EOT;69 $mail->Body = $body;70 71 $mail->send();72} catch (Exception $e) {73 echo 'Lỗi khi gởi mail: ', $mail->ErrorInfo;74}75 76// Sau khi cập nhật dữ liệu, tự động điều hướng về trang Đăng ký thành công77// header("location:register-success.php?kh_tendangnhap=$kh_tendangnhap");Step 2: tạo file template cho Trang Liên hệ
- Tạo file
templates/frontend/pages/contact.html.twig
php
1{# Kế thừa layout frontend #}2{% extends "frontend/layouts/layout.html.twig" %}3 4{# Nội dung trong block title #}5{% block title %}6Liên hệ7{% endblock %}8{# End Nội dung trong block title #}9 10{# Nội dung trong block headline #}11{% block headline %}12Liên hệ13{% endblock %}14{# End Nội dung trong block headline #}15 16{# Nội dung trong block content #}17{% block content %}18<div class="container mt-2">19 <h1 class="text-center">Liên hệ với Nền tảng</h1>20 <div class="row">21 <div class="col col-md-6">22 <img src="/assets/frontend/img/marketing-1.png" />23 </div>24 <div class="col col-md-6">25 <form method="post" action="/frontend/pages/lienhe">26 <div class="form-group">27 <label for="email">Email của bạn</label>28 <input type="email" class="form-control" id="email" name="email" placeholder="Email của bạn">29 </div>30 <div class="form-group">31 <label for="title">Tiêu đề của bạn</label>32 <input type="text" class="form-control" id="title" name="title" placeholder="Tiêu đề của bạn">33 </div>34 <div class="form-group">35 <label for="message">Lời nhắn của bạn</label>36 <textarea name="message" class="form-control"></textarea>37 </div>38 <button class="btn btn-primary" name="btnGoiLoiNhan">Gởi lời nhắn</button>39 </form>40 </div>41 </div>42 <div class="row mt-2">43 <div class="col col-md-12">44 <iframe45 src="https://www.google.com/maps/embed?pb=!1m18!1m12!1m3!1d3928.7235485722294!2d105.78061631523369!3d10.039656175103817!2m3!1f0!2f0!3f0!3m2!1i1024!2i768!4f13.1!3m3!1m2!1s0x31a062a768a8090b%3A0x4756d383949cafbb!2zMTMwIFjDtCBWaeG6v3QgTmdo4buHIFTEqW5oLCBBbiBI4buZaSwgTmluaCBLaeG7gXUsIEPhuqduIFRoxqEsIFZp4buHdCBOYW0!5e0!3m2!1svi!2s!4v1556697525436!5m2!1svi!2s"46 width="100%" height="450" frameborder="0" style="border:0" allowfullscreen></iframe>47 </div>48 </div>49</div>50{% endblock %}51{# End Nội dung trong block content #}52 53{% block customscripts %}54{% endblock %}
