Yêu cầu
Giao diện như hình sau:
Yêu cầu chức năng
- Sử dụng
Bootstrapđể tạo giao diện. - Sử dụng thư viện
Font Awesomeđể tạo các icon. - Khi bấm nút
Tìm kiếm, dữ liệu từ người dùng (End User) nhập liệu sẽ được gởi đến Server. - Cần xử lý trên Server và hiển thị đúng các dữ liệu do người dùng (End User) đã cung cấp bên khung
Kết quả Tìm kiếmbên phải.
Ví dụ người dùng (End User) nhập liệu
Kết quả hiển thị mong muốn
Hướng dẫn giải bài tập
Step 1: tạo giao diện
- Tạo file
examples/xu-ly-form/form-tim-kiem-san-pham.php - Nội dung file:
php
1<!DOCTYPE html>2<html lang="en">3 4<head>5 <meta charset="UTF-8">6 <meta name="viewport" content="width=device-width, initial-scale=1.0">7 <title>Tìm kiếm Sản phẩm | Nền tảng - Kiến thức cơ bản về Lập trình Web</title>8 9 <!-- Bootstrap CSS -->10 <link rel="stylesheet" href="../../assets/vendor/bootstrap/css/bootstrap.min.css" type="text/css">11 <!-- Font awesome -->12 <link rel="stylesheet" href="../../assets/vendor/font-awesome/css/font-awesome.min.css" type="text/css">13 <!-- Custom css - Các file css do chúng ta tự viết -->14 <link rel="stylesheet" href="../../assets/examples/assets/css/custom-style-for-example.css" type="text/css">15 16</head>17 18<body>19 20 <div class="container">21 <!-- 22 1. Thuộc tính action="file-xu-ly.php" dùng để chỉ định địa chỉ file PHP - nơi sẽ nhận dữ liệu từ CLIENT gởi đến và xử lý theo các LOGIC nào đó...23 - Nêu muốn gởi đến chính mình thì để thuộc tính action là rỗng, tức là action=""24 2. Phương thức (method) dùng để gởi request có thể sử dụng: GET hoặc POST25 * Nếu sử dụng phương thức GET:26 - Dữ liệu trong FORM sẽ được truyền theo dạng tham số trên địa chỉ URL theo định dạng sau:27 http://domain/action.php?param1=value1¶m2=value2...28 29 - Trong đó:30 + Các thông tin được gởi từ FORM đến SERVER (file action được chỉ định) sẽ được phân cách bởi dấu ?31 + Các tham số `param1`, `param2` là thuộc tính name của các thành phần Nhập liệu (inputs) trong FORM32 `value1`, `value2` là những thông tin người dùng (End user) nhập liệu trong FORM33 ...34 + Các thành phần Tham số sẽ được phân cách nhau bởi dấu &35 36 Ví dụ: http://localhost/hoc-php/form-tim-kiem-san-pham.php?username=dnpcuong&password=12345637 -->38 <form>39 <div class="row">40 <div class="col-md-12 text-center">41 <h1>Form Tìm kiếm Sản phẩm | NenTang.vn</h1>42 </div>43 <div class="col col-md-12">44 <div class="text-center">45 <button type="button" id="btnReset" class="btn btn-warning">Xóa bộ lọc</button>46 <button class="btn btn-primary btn-lg" id="btnTimKiem">Tìm kiếm <i class="fa fa-forward" aria-hidden="true"></i></button>47 </div>48 </div>49 </div>50 <hr />51 <div class="row">52 <!-- START: Form nhập liệu Tiêu chí Tìm kiếm -->53 <div class="col-md-4">54 <div class="form-tim-kiem-san-pham">55 <h5 class="text-center">Các tiêu chí Tìm kiếm Sản phẩm</h5>56 57 <div class="card">58 <!-- Tìm kiếm theo tên sản phẩm -->59 <article class="card-group-item">60 <header class="card-header">61 <h6 class="title">Tên sản phẩm</h6>62 </header>63 <div class="filter-content">64 <div class="card-body">65 <!-- 66 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)67 FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""68 Ví dụ: đặt tên là name="keyword_tensanpham"69 -->70 <input class="form-control" type="text" placeholder="Tìm kiếm" aria-label="Search" value="">71 </div> <!-- card-body.// -->72 </div>73 </article> <!-- // Tìm kiếm theo Tên sản phẩm -->74 75 <!-- Tìm kiếm theo Loại sản phẩm -->76 <article class="card-group-item">77 <header class="card-header">78 <h6 class="title">Loại sản phẩm </h6>79 </header>80 <div class="filter-content">81 <div class="card-body">82 <div class="custom-control custom-checkbox">83 <span class="float-right badge badge-light round">3 sp</span>84 <!-- 85 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)86 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""87 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"88 Ví dụ: đặt tên là name="keyword_loaisanpham[]"89 -->90 <input type="checkbox" class="custom-control-input" id="chk-loaisanpham-1">91 <label class="custom-control-label" for="chk-loaisanpham-1">Máy tính bảng</label>92 </div> <!-- form-check.// -->93 <div class="custom-control custom-checkbox">94 <span class="float-right badge badge-light round">1 sp</span>95 <input type="checkbox" class="custom-control-input" id="chk-loaisanpham-2">96 <label class="custom-control-label" for="chk-loaisanpham-2">Máy tính xách tay</label>97 </div> <!-- form-check.// -->98 <div class="custom-control custom-checkbox">99 <span class="float-right badge badge-light round">4 sp</span>100 <input type="checkbox" class="custom-control-input" id="chk-loaisanpham-3">101 <label class="custom-control-label" for="chk-loaisanpham-3">Điện thoại</label>102 </div> <!-- form-check.// -->103 <div class="custom-control custom-checkbox">104 <span class="float-right badge badge-light round">1 sp</span>105 <input type="checkbox" class="custom-control-input" id="chk-loaisanpham-4">106 <label class="custom-control-label" for="chk-loaisanpham-4">Linh phụ kiện</label>107 </div> <!-- form-check.// -->108 </div> <!-- card-body.// -->109 </div>110 </article> <!-- // Tìm kiếm theo Loại sản phẩm -->111 112 <!-- Tìm kiếm theo Nhà sản xuất -->113 <article class="card-group-item">114 <header class="card-header">115 <h6 class="title">Nhà sản xuất </h6>116 </header>117 <div class="filter-content">118 <div class="card-body">119 <div class="custom-control custom-checkbox">120 <span class="float-right badge badge-light round">3 sp</span>121 <!-- 122 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)123 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""124 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"125 Ví dụ: đặt tên là name="keyword_nhasanxuat[]"126 -->127 <input type="checkbox" class="custom-control-input" id="chk-nhasanxuat-1">128 <label class="custom-control-label" for="chk-nhasanxuat-1">Apple</label>129 </div> <!-- form-check.// -->130 <div class="custom-control custom-checkbox">131 <span class="float-right badge badge-light round">3 sp</span>132 <input type="checkbox" class="custom-control-input" id="chk-nhasanxuat-2">133 <label class="custom-control-label" for="chk-nhasanxuat-2">Samsung</label>134 </div> <!-- form-check.// -->135 <div class="custom-control custom-checkbox">136 <span class="float-right badge badge-light round">1 sp</span>137 <input type="checkbox" class="custom-control-input" id="chk-nhasanxuat-3">138 <label class="custom-control-label" for="chk-nhasanxuat-3">HTC</label>139 </div> <!-- form-check.// -->140 <div class="custom-control custom-checkbox">141 <span class="float-right badge badge-light round">1 sp</span>142 <input type="checkbox" class="custom-control-input" id="chk-nhasanxuat-4">143 <label class="custom-control-label" for="chk-nhasanxuat-4">Nokia</label>144 </div> <!-- form-check.// -->145 </div> <!-- card-body.// -->146 </div>147 </article> <!-- // Tìm kiếm theo Nhà sản xuất -->148 149 <!-- Tìm kiếm theo Khuyến mãi -->150 <article class="card-group-item">151 <header class="card-header">152 <h6 class="title">Khuyến mãi </h6>153 </header>154 <div class="filter-content">155 <div class="card-body">156 <div class="custom-control custom-radio">157 <!-- 158 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)159 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""160 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"161 Ví dụ: đặt tên là name="keyword_khuyenmai"162 -->163 <input type="radio" class="custom-control-input" id="rd-khuyenmai-1">164 <label class="custom-control-label" for="rd-khuyenmai-1">Khuyến mãi Trung Thu (giảm giá 15%)</label>165 </div> <!-- form-radio.// -->166 <div class="custom-control custom-radio">167 <input type="radio" class="custom-control-input" id="rd-khuyenmai-2">168 <label class="custom-control-label" for="rd-khuyenmai-2">Khuyến mãi Dịp Sinh nhật (được tặng 2 món quà trị giá 500k)</label>169 </div> <!-- form-radio.// -->170 </div> <!-- card-body.// -->171 </div>172 </article> <!-- // Tìm kiếm theo Nhà sản xuất -->173 174 <!-- Tìm kiếm theo khoảng giá tiền -->175 <article class="card-group-item">176 <header class="card-header">177 <h6 class="title">Khoảng tiền </h6>178 </header>179 <div class="filter-content">180 <div class="card-body">181 <div class="form-row">182 <div class="form-group col-md-6">183 <label>Từ</label>184 <!-- 185 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)186 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""187 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"188 Ví dụ: đặt tên là name="keyword_khuyenmai"189 -->190 <input type="range" class="custom-range" min="0" max="50000000" step="100000" id="sotientu" value="0">191 <span><span id="sotientu-text">0</span></span>192 </div>193 <div class="form-group col-md-6 text-right">194 <label>Đến</label>195 <input type="range" class="custom-range" min="0" max="50000000" step="100000" id="sotienden" value="50000000">196 <span><span id="sotienden-text">50.000.000</span></span>197 </div>198 </div>199 </div> <!-- card-body.// -->200 </div>201 </article> <!-- // Tìm kiếm theo khoảng giá tiền -->202 203 <!-- Tìm kiếm theo màu sắc sản phẩm -->204 <article class="card-group-item">205 <header class="card-header">206 <h6 class="title">Màu sắc (tùy chọn thêm)</h6>207 </header>208 <div class="filter-content">209 <div class="card-body">210 <label class="btn btn-danger">211 <!-- 212 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)213 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""214 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"215 Ví dụ: đặt tên là name="keyword_mausac[]"216 -->217 <input class="" type="checkbox" id="keyword-mausac-1">218 <span class="form-check-label">Đỏ</span>219 </label>220 <label class="btn btn-success">221 <input class="" type="checkbox" id="keyword-mausac-2">222 <span class="form-check-label">Xanh lá</span>223 </label>224 <label class="btn btn-primary">225 <input class="" type="checkbox" id="keyword-mausac-3">226 <span class="form-check-label">Xanh dương</span>227 </label>228 </div> <!-- card-body.// -->229 </div>230 </article> <!-- // Tìm kiếm theo màu sắc sản phẩm -->231 </div>232 </div>233 </div>234 <!-- END: Form nhập liệu Tiêu chí Tìm kiếm -->235 236 <!-- START: Kết quả tìm kiếm theo Tiêu chí Tìm kiếm -->237 <div class="col-md-8">238 <div class="ket-qua-tim-kiem-san-pham">239 <h5 class="text-center">Kết quả Tìm kiếm Sản phẩm</h5>240 241 <!-- START: CODE XỬ LÝ PHP -->242 <?php243 244 ?>245 <!-- END: CODE XỬ LÝ PHP -->246 247 </div>248 </div>249 <!-- END: Kết quả tìm kiếm theo Tiêu chí Tìm kiếm -->250 </div>251 </div>252 </form>253 254 <!-- Liên kết JavaScript -->255 <!-- jQuery first, then Popper.js, then Bootstrap JS -->256 <script src="../../assets/vendor/jquery/jquery.min.js"></script>257 <script src="../../assets/vendor/popperjs/popper.min.js"></script>258 <script src="../../assets/vendor/bootstrap/js/bootstrap.min.js"></script>259 260 <!-- Custom script - Các file js do mình tự viết -->261 <script src="../../assets/examples/js/custom-script-for-example.js"></script>262</body>263 264</html>Step 2: thiết lập các thông số thuộc tính cho FORM Tìm kiếm
Chúng ta sẽ thiết lập cho Form Tìm kiếm các thông số sau:name="frmTimKiem"method=GETaction="": do chúng ta muốn gởi dữ liệu đến chính file PHP Form đang hiển thị, nên để action là rỗng
- Tìm dòng code
38, hiệu chỉnh như sau:
php
1<form name="frmTimKiem" id="frmTimKiem" method="GET" action="">2</form>Step 3: tiến hành đặt tên name="" và giá trị value="" cho các thành phần Nhập liệu trong Form
- Hiệu chỉnh file trên thành:
php
1<!DOCTYPE html>2<html lang="en">3 4<head>5 <meta charset="UTF-8">6 <meta name="viewport" content="width=device-width, initial-scale=1.0">7 <title>Tìm kiếm Sản phẩm | Nền tảng - Kiến thức cơ bản về Lập trình Web</title>8 9 <!-- Bootstrap CSS -->10 <link rel="stylesheet" href="../../assets/vendor/bootstrap/css/bootstrap.min.css" type="text/css">11 <!-- Font awesome -->12 <link rel="stylesheet" href="../../assets/vendor/font-awesome/css/font-awesome.min.css" type="text/css">13 <!-- Custom css - Các file css do chúng ta tự viết -->14 <link rel="stylesheet" href="../../assets/examples/assets/css/custom-style-for-example.css" type="text/css">15 16</head>17 18<body>19 20 <div class="container">21 <!-- 22 1. Thuộc tính action="file-xu-ly.php" dùng để chỉ định địa chỉ file PHP - nơi sẽ nhận dữ liệu từ CLIENT gởi đến và xử lý theo các LOGIC nào đó...23 - Nêu muốn gởi đến chính mình thì để thuộc tính action là rỗng, tức là action=""24 2. Phương thức (method) dùng để gởi request có thể sử dụng: GET hoặc POST25 * Nếu sử dụng phương thức GET:26 - Dữ liệu trong FORM sẽ được truyền theo dạng tham số trên địa chỉ URL theo định dạng sau:27 http://domain/action.php?param1=value1¶m2=value2...28 29 - Trong đó:30 + Các thông tin được gởi từ FORM đến SERVER (file action được chỉ định) sẽ được phân cách bởi dấu ?31 + Các tham số `param1`, `param2` là thuộc tính name của các thành phần Nhập liệu (inputs) trong FORM32 `value1`, `value2` là những thông tin người dùng (End user) nhập liệu trong FORM33 ...34 + Các thành phần Tham số sẽ được phân cách nhau bởi dấu &35 36 Ví dụ: http://localhost/hoc-php/form-tim-kiem-san-pham.php?username=dnpcuong&password=12345637 -->38 <form name="frmTimKiem" id="frmTimKiem" method="GET" action="">39 <div class="row">40 <div class="col-md-12 text-center">41 <h1>Form Tìm kiếm Sản phẩm | NenTang.vn</h1>42 </div>43 <div class="col col-md-12">44 <div class="text-center">45 <button type="button" id="btnReset" class="btn btn-warning">Xóa bộ lọc</button>46 <button class="btn btn-primary btn-lg" name="btnTimKiem" id="btnTimKiem">Tìm kiếm <i class="fa fa-forward" aria-hidden="true"></i></button>47 </div>48 </div>49 </div>50 <hr />51 <div class="row">52 <!-- START: Form nhập liệu Tiêu chí Tìm kiếm -->53 <div class="col-md-4">54 <div class="form-tim-kiem-san-pham">55 <h5 class="text-center">Các tiêu chí Tìm kiếm Sản phẩm</h5>56 57 <div class="card">58 <!-- Tìm kiếm theo tên sản phẩm -->59 <article class="card-group-item">60 <header class="card-header">61 <h6 class="title">Tên sản phẩm</h6>62 </header>63 <div class="filter-content">64 <div class="card-body">65 <!-- 66 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)67 FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""68 Ví dụ: đặt tên là name="keyword_tensanpham"69 -->70 <input class="form-control" type="text" placeholder="Tìm kiếm" aria-label="Search" name="keyword_tensanpham" value="">71 </div> <!-- card-body.// -->72 </div>73 </article> <!-- // Tìm kiếm theo Tên sản phẩm -->74 75 <!-- Tìm kiếm theo Loại sản phẩm -->76 <article class="card-group-item">77 <header class="card-header">78 <h6 class="title">Loại sản phẩm </h6>79 </header>80 <div class="filter-content">81 <div class="card-body">82 <div class="custom-control custom-checkbox">83 <span class="float-right badge badge-light round">3 sp</span>84 <!-- 85 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)86 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""87 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"88 Ví dụ: đặt tên là name="keyword_loaisanpham[]"89 -->90 <input type="checkbox" class="custom-control-input" name="keyword_loaisanpham[]" value="1" id="chk-loaisanpham-1">91 <label class="custom-control-label" for="chk-loaisanpham-1">Máy tính bảng</label>92 </div> <!-- form-check.// -->93 <div class="custom-control custom-checkbox">94 <span class="float-right badge badge-light round">1 sp</span>95 <input type="checkbox" class="custom-control-input" name="keyword_loaisanpham[]" value="2" id="chk-loaisanpham-2">96 <label class="custom-control-label" for="chk-loaisanpham-2">Máy tính xách tay</label>97 </div> <!-- form-check.// -->98 <div class="custom-control custom-checkbox">99 <span class="float-right badge badge-light round">4 sp</span>100 <input type="checkbox" class="custom-control-input" name="keyword_loaisanpham[]" value="3" id="chk-loaisanpham-3">101 <label class="custom-control-label" for="chk-loaisanpham-3">Điện thoại</label>102 </div> <!-- form-check.// -->103 <div class="custom-control custom-checkbox">104 <span class="float-right badge badge-light round">1 sp</span>105 <input type="checkbox" class="custom-control-input" name="keyword_loaisanpham[]" value="4" id="chk-loaisanpham-4">106 <label class="custom-control-label" for="chk-loaisanpham-4">Linh phụ kiện</label>107 </div> <!-- form-check.// -->108 </div> <!-- card-body.// -->109 </div>110 </article> <!-- // Tìm kiếm theo Loại sản phẩm -->111 112 <!-- Tìm kiếm theo Nhà sản xuất -->113 <article class="card-group-item">114 <header class="card-header">115 <h6 class="title">Nhà sản xuất </h6>116 </header>117 <div class="filter-content">118 <div class="card-body">119 <div class="custom-control custom-checkbox">120 <span class="float-right badge badge-light round">3 sp</span>121 <!-- 122 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)123 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""124 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"125 Ví dụ: đặt tên là name="keyword_nhasanxuat[]"126 -->127 <input type="checkbox" class="custom-control-input" name="keyword_nhasanxuat[]" value="1" id="chk-nhasanxuat-1">128 <label class="custom-control-label" for="chk-nhasanxuat-1">Apple</label>129 </div> <!-- form-check.// -->130 <div class="custom-control custom-checkbox">131 <span class="float-right badge badge-light round">3 sp</span>132 <input type="checkbox" class="custom-control-input" name="keyword_nhasanxuat[]" value="2" id="chk-nhasanxuat-2">133 <label class="custom-control-label" for="chk-nhasanxuat-2">Samsung</label>134 </div> <!-- form-check.// -->135 <div class="custom-control custom-checkbox">136 <span class="float-right badge badge-light round">1 sp</span>137 <input type="checkbox" class="custom-control-input" name="keyword_nhasanxuat[]" value="3" id="chk-nhasanxuat-3">138 <label class="custom-control-label" for="chk-nhasanxuat-3">HTC</label>139 </div> <!-- form-check.// -->140 <div class="custom-control custom-checkbox">141 <span class="float-right badge badge-light round">1 sp</span>142 <input type="checkbox" class="custom-control-input" name="keyword_nhasanxuat[]" value="4" id="chk-nhasanxuat-4">143 <label class="custom-control-label" for="chk-nhasanxuat-4">Nokia</label>144 </div> <!-- form-check.// -->145 </div> <!-- card-body.// -->146 </div>147 </article> <!-- // Tìm kiếm theo Nhà sản xuất -->148 149 <!-- Tìm kiếm theo Khuyến mãi -->150 <article class="card-group-item">151 <header class="card-header">152 <h6 class="title">Khuyến mãi </h6>153 </header>154 <div class="filter-content">155 <div class="card-body">156 <div class="custom-control custom-radio">157 <!-- 158 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)159 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""160 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"161 Ví dụ: đặt tên là name="keyword_khuyenmai"162 -->163 <input type="radio" class="custom-control-input" name="keyword_khuyenmai" value="1" id="rd-khuyenmai-1">164 <label class="custom-control-label" for="rd-khuyenmai-1">Khuyến mãi Trung Thu (giảm giá 15%)</label>165 </div> <!-- form-radio.// -->166 <div class="custom-control custom-radio">167 <input type="radio" class="custom-control-input" name="keyword_khuyenmai" value="2" id="rd-khuyenmai-2">168 <label class="custom-control-label" for="rd-khuyenmai-2">Khuyến mãi Dịp Sinh nhật (được tặng 2 món quà trị giá 500k)</label>169 </div> <!-- form-radio.// -->170 </div> <!-- card-body.// -->171 </div>172 </article> <!-- // Tìm kiếm theo Nhà sản xuất -->173 174 <!-- Tìm kiếm theo khoảng giá tiền -->175 <article class="card-group-item">176 <header class="card-header">177 <h6 class="title">Khoảng tiền </h6>178 </header>179 <div class="filter-content">180 <div class="card-body">181 <div class="form-row">182 <div class="form-group col-md-6">183 <label>Từ</label>184 <!-- 185 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)186 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""187 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"188 Ví dụ: đặt tên là name="keyword_khuyenmai"189 -->190 <input type="range" class="custom-range" min="0" max="50000000" step="100000" id="sotientu" name="keyword_sotientu" value="0">191 <span><span id="sotientu-text">0</span></span>192 </div>193 <div class="form-group col-md-6 text-right">194 <label>Đến</label>195 <input type="range" class="custom-range" min="0" max="50000000" step="100000" id="sotienden" name="keyword_sotienden" value="50000000">196 <span><span id="sotienden-text">50.000.000</span></span>197 </div>198 </div>199 </div> <!-- card-body.// -->200 </div>201 </article> <!-- // Tìm kiếm theo khoảng giá tiền -->202 203 <!-- Tìm kiếm theo màu sắc sản phẩm -->204 <article class="card-group-item">205 <header class="card-header">206 <h6 class="title">Màu sắc (tùy chọn thêm)</h6>207 </header>208 <div class="filter-content">209 <div class="card-body">210 <label class="btn btn-danger">211 <!-- 212 Thuộc tính name="" cần có trong các thành phần Nhập liệu (input, select, ...)213 - FORM sẽ đóng gói dữ liệu người dùng (End User) nhập liệu vào đúng tên được đặt trong thuộc tính name=""214 - Nếu muốn truyền dữ liệu dạng mảng (array) thì sử dụng cú pháp name="ten_tham_so[]"215 Ví dụ: đặt tên là name="keyword_mausac[]"216 -->217 <input class="" type="checkbox" name="keyword_mausac[]" id="keyword-mausac-1" value="red">218 <span class="form-check-label">Đỏ</span>219 </label>220 <label class="btn btn-success">221 <input class="" type="checkbox" name="keyword_mausac[]" id="keyword-mausac-2" value="green">222 <span class="form-check-label">Xanh lá</span>223 </label>224 <label class="btn btn-primary">225 <input class="" type="checkbox" name="keyword_mausac[]" id="keyword-mausac-3" value="blue">226 <span class="form-check-label">Xanh dương</span>227 </label>228 </div> <!-- card-body.// -->229 </div>230 </article> <!-- // Tìm kiếm theo màu sắc sản phẩm -->231 </div>232 </div>233 </div>234 <!-- END: Form nhập liệu Tiêu chí Tìm kiếm -->235 236 <!-- START: Kết quả tìm kiếm theo Tiêu chí Tìm kiếm -->237 <div class="col-md-8">238 <div class="ket-qua-tim-kiem-san-pham">239 <h5 class="text-center">Kết quả Tìm kiếm Sản phẩm</h5>240 241 <?php242 ?>243 244 </div>245 </div>246 <!-- END: Kết quả tìm kiếm theo Tiêu chí Tìm kiếm -->247 </div>248 </div>249 </form>250 251 <!-- Liên kết JavaScript -->252 <!-- jQuery first, then Popper.js, then Bootstrap JS -->253 <script src="../../assets/vendor/jquery/jquery.min.js"></script>254 <script src="../../assets/vendor/popperjs/popper.min.js"></script>255 <script src="../../assets/vendor/bootstrap/js/bootstrap.min.js"></script>256 257 <!-- Custom script - Các file js do mình tự viết -->258 <script src="../../assets/examples/js/custom-script-for-example.js"></script>259</body>260 261</html>Step 4: bổ sung đoạn code xử lý thu thập dữ liệu và xử lý
- Bổ sung đoạn code xử lý PHP:
php
1<?php2 // Kiếm tra, nếu người dùng có bấm nút Tìm kiếm thì sẽ xử lý Logic/Nghiệp vụ Tìm kiếm sản phẩm3 // Nếu có bấm Submit Tìm kiếm thì Request GET sẽ có tham số "btnTimKiem" được gởi đến Server4 if(isset($_GET['btnTimKiem'])) {5 // --- PHÂN TÁCH DỮ LIỆU ----------------------------------------------------------------------------6 // Thu thập thông tin người dùng (End User) gởi đến7 $keyword_tensanpham = $_GET['keyword_tensanpham'];8 9 // Đối với dữ liệu truyền từ CHECKBOX và RADIO10 // Nếu người dùng không chọn (trạng thái CHECKED = true) thì sẽ không có key tồn tại trong $_GET11 // Do đó chúng ta cần phải kiểm tra bằng hàm isset()12 13 // Xử lý phân tách dữ liệu CHECKBOX Loại sản phẩm14 $keyword_loaisanpham = []; // Khởi tạo Array rỗng mặc định15 if(isset($_GET['keyword_loaisanpham'])) {16 $keyword_loaisanpham = $_GET['keyword_loaisanpham'];17 }18 19 // Xử lý phân tách dữ liệu CHECKBOX Nhà Sản xuất20 $keyword_nhasanxuat = []; // Khởi tạo Array rỗng mặc định21 if(isset($_GET['keyword_nhasanxuat'])) {22 $keyword_nhasanxuat = $_GET['keyword_nhasanxuat'];23 }24 25 // Xử lý phân tách dữ liệu RADIO Khuyến mãi26 $keyword_khuyenmai = null; // Khởi tạo giá trị mặc định27 if(isset($_GET['keyword_khuyenmai'])) {28 $keyword_khuyenmai = $_GET['keyword_khuyenmai'];29 }30 31 $keyword_sotientu = $_GET['keyword_sotientu'];32 $keyword_sotienden = $_GET['keyword_sotienden'];33 34 // Xử lý phân tách dữ liệu CHECKBOX Màu sắc35 $keyword_mausac = []; // Khởi tạo Array rỗng mặc định36 if(isset($_GET['keyword_mausac'])) {37 $keyword_mausac = $_GET['keyword_mausac'];38 }39 // ---------------------------------------------------------------------------------------------------40 41 // --- HIỂN THỊ KẾT QUẢ RA MÀN HÌNH (RENDER HTML) ----------------------------------------------------42 // Hiển thị ra màn hình các thông số Tìm kiếm đã nhận được43 echo "<ul>";44 echo "<li>Từ khóa tìm kiếm: <b>{$keyword_tensanpham}</b></li>";45 46 // Nếu người dùng có CHECK chọn Loại sản phẩm bất kỳ (không rỗng), thì in ra các giá trị được chọn47 if(!empty($keyword_loaisanpham)) {48 // Sử dụng hàm implode để ghép các giá trị trong mảng (array) lại với nhau thành 1 chuỗi giá trị49 $keyword_loaisanpham_string = implode(',', $keyword_loaisanpham);50 echo "<li>Các Loại sản phẩm cần tìm kiếm: <b>{$keyword_loaisanpham_string}</b></li>";51 }52 53 // Nếu người dùng có CHECK chọn Nhà Sản xuất bất kỳ (không rỗng), thì in ra các giá trị được chọn54 if(!empty($keyword_nhasanxuat)) {55 // Sử dụng hàm implode để ghép các giá trị trong mảng (array) lại với nhau thành 1 chuỗi giá trị56 $keyword_nhasanxuat_string = implode(',', $keyword_nhasanxuat);57 echo "<li>Các Nhà sản xuất cần tìm kiếm: <b>{$keyword_nhasanxuat_string}</b></li>";58 }59 60 // Nếu người dùng có CHECK chọn Loại khuyến mãi bất kỳ (không rỗng), thì in ra giá trị được chọn61 if(!empty($keyword_khuyenmai)) {62 echo "<li>Loại Khuyến mãi cần tìm kiếm: <b>{$keyword_khuyenmai}</b></li>";63 }64 65 echo "<li>Giá tiền tìm kiếm từ: <b>{$keyword_sotientu}</b></li>";66 echo "<li>Giá tiền tìm kiếm đến: <b>{$keyword_sotienden}</b></li>";67 68 // Nếu người dùng có CHECK chọn Màu sắc bất kỳ (không rỗng), thì in ra các giá trị được chọn69 if(!empty($keyword_mausac)) {70 // Sử dụng hàm implode để ghép các giá trị trong mảng (array) lại với nhau thành 1 chuỗi giá trị71 $keyword_mausac_string = implode(',', $keyword_mausac);72 echo "<li>Các Nhà sản xuất cần tìm kiếm: <b>{$keyword_mausac_string}</b></li>";73 }74 75 echo "</ul>";76 // ---------------------------------------------------------------------------------------------------77 }78?>
