Làm việc với form trong php gồm các vấn đề: sử dụng $_GET, $_POST ; hiện lại giá trị ; kiểm tra dữ liệu và hiện lỗi ; xử lý file upload; ứng dụng của method get … Đậy là bài viết bổ trợ cho video ở cuối, các em hãy xem video trước nhé.
Nội dung chính
- $_GET và $_POST
- Lấy dữ liệu khi submit
- Hiện lại giá trị trong form
- Hiện các lỗi
- Xử lý dữ liệu kiểu file
- Xử lý submit ở file khác
- Form có method là GET
$_GET và $_POST
- $_GET: là array trong PHP dùng để chứa các tham số trong url
- $_POST: là array trong PHP dùng để chứa các giá trị trong form POST
- Để xem cấu trúc 2 array này: dùng lệnh print_r. Ví dụ:
- Lấy ra 1 giá trị trong $_GET, $_POST, dùng $_POST[‘key’] , $_GET[‘key’] . Ví dụ:
Tạo form
a. Tạo trang formThemSP.php và code:
<link href="//cdn.jsdelivr.net/npm//dist/css/bootstrap.min.css" rel="stylesheet"> <div class="m-auto border border-secondary p-2" style="width:800px"> <form method="post" class="row" enctype='multipart/form-data' > <div class="mb-3 col-6"> <label for="tensp" class="form-label">Tên SP</label> <input id="tensp" name="tensp" type="text" class="form-control"> </div> <div class="mb-3 col-6"> <label for="gia" class="form-label">Giá</label> <input id="gia" name="gia" type="number" class="form-control"> </div> <div class="mb-3 col-6"> <label for="hinh" class="form-label">Hình</label> <input id="hinh" name="hinh" type="file" class="form-control" > </div> <div class="mb-3 col-6"> <label for="ngay" class="form-label">Ngày nhập</label> <input id="ngay" name="ngay" type="date" class="form-control" > </div> <div class="mb-3 col-6"> <label for="loaisp" class="form-label">Loại sản phẩm</label> <select id="loaisp" name="loaisp" class="form-select" > <option selected> Sách lịch sử </option> <option value="1">Văn học </option> <option value="2">Nghệ thuật sống</option> <option value="3">Học làm người</option> </select> </div> <div class="mb-3 col-6"> <label class="form-label">Ẩn hiện</label> <div class="form-control"> <div class="form-check form-check-inline"> <input name="anhien" id="hien" value="1" class="form-check-input" type="radio"> <label class="form-check-label" for="hien">Hiện</label> </div> <div class="form-check form-check-inline"> <input name="anhien" id="an" value="0" class="form-check-input" type="radio" > <label class="form-check-label" for="an">Ẩn</label> </div> </div> </div> <div class="mb-3"> <textarea id="mota" name="mota" rows="4" placeholder="Mô tả" class="form-control"></textarea> </div> <div class="text-center"> <button name="btnLuu" type="submit" class="btn btn-primary py-1 px-5">Lưu dữ liệu</button> <button type="reset" class="btn btn-danger py-1 px-5">Xóa làm lại</button> </div> </form> </div>Lấy dữ liệu khi submit
<?php if (isset($_POST['btnLuu'])==true) { //nếu cần thì xem data trong $_POST với lệnh print_r($_POST); //$tenbiến = $_POST['nameCủaControlTrongForm']; //kiểm tra tính đúng đắn của dữ liệu //nếu dữ liệu ok thì kết nối db , lấy data hoặc lưu ... //chuyển hướng sang trang khác niếu cần với header("location:...); //thoát với lệnh exit(); $tensp = $_POST['tensp']; $gia = $_POST['gia']; $hinh = $_POST['hinh']; } ?>Hiện lại giá trị trong form
- Khi submit, trang web sẽ nạp lại, dẫn đến các giá trị user đã gõ trong các control sẽ bị mất. cho nên cần phải hiện lại.
- Hiện giá trị cho các control trong form bằng cách gán giá trị trong thuộc tính value . Ví dụ:
Hiện các lỗi
- Việc kiểm tra dữ liệu trong form có thể có một số lỗi cần thông báo
Để hiện lỗi, ở đầu form nếu thấy biến lỗi có giá trị thì hiện ra
<?php if ($loi!="") { ?> <div class="alert alert-danger"><?php echo $loi?></div> <?php } ?>Xử lý dữ liệu kiểu file
Nhận thông tin của file qua $_FILES[‘nameCủaControl’]. Ví dụ
$h = $_FILES['hinh']; // $h = Array ( [name] => bg cam.jpg [type] => image/jpeg [tmp_name] => E:\xampp\tmp\phpC11.tmp [error] => 0 [size] => 29342 )$h là mảng gồm 5 phần tử : name, size, type , tmp_name , error. Trong đó
- name:là tên của file hình mà user đã submit
- size: kích thước của file
- type: kiểu data trong file được submit
- tmp_name: đường path file đã được lưu trong folder tmp
- error: cho biết có lỗi không.
Lưu file submit bằng cách di chuyển từ vị trí lưu tạm đến chỗ mới bằng lệnh move_uploaded_file($tmp_name, $newPath);
$tmp_name= $h['tmp_name']; $name= $h['name']; $newPath="khoMedia/" . $name; move_uploaded_file($tmp_name, $newPath);Xử lý submit ở file khác
Địa chỉ file khác khai báo trong thuộc tính action của form
Form có method là GET
- Ít khi dùng hơn post
- Chỉ dùng method get khi thật sự cần , đó là khi cần đưa các biến trong form lên địa chỉ giúp user mà không cần user nhập các biến
- Ví dụ 1: trang tin trong loại với tham số idLT. => Chọn trong select 1 loại tin rồi submit để hiện tin trong loại
demoGET1.php
<?php $idLT=0; if (isset($_GET['idLT'])==true) $idLT = $_GET['idLT']; $conn = new PDO("mysql:host=localhost;dbname=thaylongweb_tintuc;charset=utf8", "root", ""); $sql="SELECT idTin, TieuDe, TomTat, Ngay, SoLanXem FROM tin WHERE idLT = $idLT"; $data = $conn->query($sql); ?> <?php foreach ($data as $index=> $row) {?> <div class="tin"> <h4> <?=$index+1?> <a href="#"> <?=$row['TieuDe'];?></a></h4> <i> Đăng: <?=date("d/m/Y",strtotime($row['Ngay']))?> - Xem <?=$row['SoLanXem']?> </i> <p> <?=$row['TomTat']?> </p> </div> <?php } ?>Hiện form loại tin
<?php $loaitin = $conn->query("SELECT idLT, Ten FROM loaitin");?> <form method="get"> <select name="idLT"> <?php foreach ($loaitin as $row) { ?> <option value="<?=$row['idLT']?>"><?=$row['Ten']?></option> <?php } ?> </select> <button type="submit">Xem </button> </form>Ví dụ 2: có trang tin theo từ khóa với tham số tukhoa. è gõ từ khóa trong form rồi submit để hiện tin chứa từ khóa (bài toán tìm kiếm)
demoGET2.php
<?php $tukhoa=0; if (isset($_GET['tukhoa'])==true) $tukhoa = $_GET['tukhoa']; $conn = new PDO("mysql:host=localhost;dbname=thaylongweb_tintuc;charset=utf8", "root", ""); $sql="SELECT idTin, TieuDe, TomTat, Ngay, SoLanXem FROM tin WHERE TieuDe like '%$tukhoa%'"; $data = $conn->query($sql); ?> <?php foreach ($data as $index=> $row) {?> <div class="tin"> <h4> <?=$index+1?> <a href="#"> <?=$row['TieuDe'];?></a></h4> <i> Đăng: <?=date("d/m/Y",strtotime($row['Ngay']))?> - Xem <?=$row['SoLanXem']?> </i> <p> <?=$row['TomTat']?> </p> </div> <?php } ?>Form tìm kiếm với method get được dùng để đưa từ khóa lên url
<form method="get"> <input type="text" name="tukhoa" placeholder="Từ khóa"> <button type="submit">Xem </button> </form>Database thaylongweb_tintuc
Các vấn đề khi làm việc với form trong php cơ bản như trình bày ở trên, vẫn còn một vài đấn đề khác thầy sẽ trình bày sau.
Chi tiết các hướng dẫn ở trên mời em xem video nhé.