Sử dụng WebEditor hướng dẫn biến textarea thành vùng soạn thảo như Word trong trang web, thích hợp dùng cho nội dung tin, mô tả sản phẩm…
Chuẩn bị
- Chuẩn bị>Bạn tạo một trang web với form có sẵn để sử dụng webeditor
- Thực hiện tạo database hocPHP (nếu chưa tạo) và tạo table sach gồm các field: id, tensach, gia, urlhinh, ngay, idloai, anhien,mota
Tạo form để sử dụng webeditor
Trong >Tạo form để sử dụng webeditor ormThemSach.php và code:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/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 class="form-label">Tên Sách</label>
<input name="tensach" placeholder="Nhập tên sách" class="form-control" type="text">
</div>
<div class="mb-3 col-6">
<label class="form-label">Giá</label>
<input name="gia" placeholder="Nhập giá" type="number" class="form-control">
</div>
<div class="mb-3 col-6">
<label class="form-label">Hình</label>
<input name="hinh" placeholder="Địa chỉ hình" type="text" class="form-control" >
</div>
<div class="mb-3 col-6">
<label class="form-label">Ngày nhập</label>
<input name="ngay" type="date" class="form-control" >
</div>
<div class="mb-3 col-6">
<label class="form-label">Loại sách</label>
<select name="loaisach" 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>

Dùng CkEditor
CKEditor là 1 WebEditor rất dễ dùng, nó biến textarea thành vùng soạn thảo giống như word. Và khi submit fo>Dùng CkEditorde html để gửi lên server.
- Download: https://ckeditor.com/ckeditor-5/download/
- Xem ví dụ: https://ckeditor.com/docs/ckeditor5/latest/examples/index.html

<script>
ClassicEditor
.create( document.querySelector( '#mota' ) )
.then( editor => {
console.log( editor );
} )
.catch( error => {
console.error( error );
} );
</script>
Hiện ra nội dung của editor
Cách 1: Bạn gõ nội dung trong tag textarea hoặc hiện từ database, ví dụ: <?=$sach[‘mota’]?><>Hiện ra nội dung của editorode"> Hoặc gán trong lúc khởi tạo với hàm setdata Lấy giá trị từ editor: khi cần có thể lấy data trong editor bằng hàm getdate Thêm code này vào trang : Thêm style định dạng cho vùng soạn thảo: Cứ làm bình thường như đã biết. Xem thử để biết cấu trúc của $_POST a. Download CKFinder : https://ckeditor.com/ckfinder/download/ b. Giải nén và chép folder ckfinder vào website c. Vào Sử dụng CKFinder trong file urlHinh/#!/guide/dev_integration">https://ckeditor.com/docs/ckfinder/ckfinder3/#!/guide/dev_integration , cuộn xuống mục để xem code demo ở mục Using CKFinder in a Popup Window Trong code trên , chú ý có tag input với tên url, hàm openPopup sẽ được gọi khi click nút Select file d. Mở trang web của bạn, nhúng script ckfinder.js vào e. Trong sự kiện click của input hinh, gọi hàm openPopup f. Định nghĩa hàm openPopup g. Mở file ckfinnder/config.php, tìm đến mục anthentication, sửa false thành true Xong rồi, hưởng thành quả nhé Trong trang web của bạn, cuộn đến vùng code nhúng CkEdior, và thêm code như mẫu để được như sau: Kết quả, nhắp nút chèn hình trong toolbar thì CKFinder sẽ bật lên<textarea id="mota" name="mota" rows="4" placeholder="Mô tả" class="form-control">
<p>Đây là nội dung của sách</p>
</textare
<script>
ClassicEditor
.create( document.querySelector( '#mota' ) )
.then( editor => {
editor.setData( '<p>Nội dung của sách lấy từ database</p>' );
} )
.catch( error => {
console.error( error );
} );
</script>
<button type="button" onclick="laydata()">Lấy data</button>
<script>
var ed;
ClassicEditor.create( document.querySelector( '#mota' ) )
.then( editor => {
editor.setData( '<h4>Nội dung của sách lấy từ database</h4>' );
ed = editor;
})
.catch( error => {console.error( error )} );
</script>
<script>
function laydata(){
e = ed.getData();
console.log(e);
}
</script>
Tiếng việt cho editor:
<script src="https://cdn.ckeditor.com/ckeditor5/31.0.0/classic/translations/vi.js"> </script>Tiếng việt cho editor: cEditor.create( document.querySelector('#mota') , {language: 'vi'} )
.then( editor => { } )
.catch( error => {console.error( error )} );
</script>
Chỉnh độ cao cho Ckeditor
<style>
.ck-editor__editable_inline {
min-height: 250px;
max-height: 450px;
}
</style>
Chỉnh độ cao cho Ckeditor
rong Editor vào db
<?php
if ( isset( $_POST['btnLuu'])) {
echo "<xmp>"; print_r($_POST);
}
?>
Sử dụng CKFinder trong file urlHinh
<head>
<meta charset="utf-8"> <title>CKFinder 3</title>
<script src="/ckfinder/ckfinder.js"></script>
</head>
<body>
<script>
function openPopup() {
CKFinder.popup( {
chooseFiles: true,
onInit: function( finder ) {
finder.on( 'files:choose', function( evt ) {
var file = evt.data.files.first();
document.getElementById( 'url' ).value = file.getUrl();
} );
finder.on( 'file:choose:resizedImage', function( evt ) {
document.getElementById( 'url' ).value = evt.data.resizedUrl;
} );
}
} );
}
</script>
<input type="text" size="48" name="url" id="url" /> <button onclick="openPopup()">Select file</button>
</body>
<script src="ckfinder/ckfinder.js"><input ondblclick="openPopup('hinh')" id="hinh" name="hinh" placeholder="Địa chỉ hình" type="text" class="form-control" > <script>
function openPopup(idobj) {
CKFinder.popup( {
chooseFiles: true,
onInit: function( finder ) {
finder.on( 'files:choose', function( evt ) {
var file = evt.data.files.first();
document.getElementById( idobj ).value = file.getUrl();
} );
finder.on( 'file:choose:resizedImage', function( evt ) {
document.getElementById( idobj ).value = evt.data.resizedUrl;
} );
}
} );
}
</script>
$config['authentication'] = function () {
return true;//false;
};
Sử dụng CKFinder để nhúng hình vào CkEditor
Sử dụng CKFinder để nhúng hình vào CkEditortent/uploads/2021/11/image-20.png" alt="" class="wp-image-4699" srcset="https://longnv.name.vn/wp-content/uploads/2021/11/image-20.png 698w, https://longnv.name.vn/wp-content/uploads/2021/11/image-20-300x125.png 300w, https://longnv.name.vn/wp-content/uploads/2021/11/image-20-696x291.png 696w" sizes="(max-width: 698px) 100vw, 698px" />
<script>
ClassicEditor.create( document.querySelector('#mota') , {
language: 'vi',
ckfinder: {
uploadUrl: 'ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files&responseType=json'
},
toolbar: [ 'ckfinder', 'imageUpload', '|', 'heading', '|', 'bold', 'italic', '|', 'undo', 'redo' ]
})
.then( editor => { })
.catch( error => {console.error( error )} );
</script>

Điều chỉnh các nút trong toolbar của CkEditor
<script>
ClassicEditor.create( document.querySelector('#mota') , {
language: 'vi',
ckfinder: {
uploadUrl: 'ckfin>Điều chỉnh các nút trong toolbar của CkEditorpload&type=Files&responseType=json'
},
toolbar: {
items: [
'fontfamily', 'fontsize', '|',
'heading', '|',
'alignment', '|',
'fontColor', 'fontBackgroundColor', '|',
'bold', 'italic', 'underline', 'subscript', 'superscript', '|',
'link', '|',
'outdent', 'indent', '|',
'bulletedList', 'numberedList', 'todoList', '|',
'code', 'codeBlock', '|',
'insertTable', '|',
'uploadImage', '|',
'ckfinder',
'undo', 'redo'
],
shouldNotGroupWhenFull: true
}
})
.then( editor => { })
.catch( error => {console.error( error )} );
</script>


