I want an email filtering function.
this is my url :
https://localhost/quantrictvv1/preview/271.
this is my database:
I want the condition that numbers in the email column matching the id of the above url (which is 271) are allowed to appear, I’m new.
I want a filter function with the condition that email equals url’s id 271, what should I do? I’m new to javascript
View :
<div class="row">
<div class="col-md-12 mt-4">
<div class="table-responsive">
<table class="table" id="records">
<thead>
<tr>
<th>Nhận xét</th>
<th>Note</th>
<!-- <th>Email</th> -->
<th>Action</th>
</tr>
</thead>
</table>
</div>
</div>
</div>
Javascript :
<script>
$(document).on("click", "#add", function(e){
e.preventDefault();
var name = $("#name").val();
var email = $("#email").val();
if (name == "" || email == ""){
alert("Không được để trống");
}else{
$.ajax({
url:"<?php echo base_url(); ?>insert",
type: "post",
dataType: "json",
data: {
name: name,
email: email
},
success: function(data){
if (data.responce == "success") {
$('#records').DataTable().destroy();
fetch();
$('#exampleModal').modal('hide');
toastr["success"](data.message);
}else{
toastr["error"]('Ghi chú không thành công');
}
}
});
$("#form")[0].reset();
}
});
//fetch records
function fetch(){
//lấy url xuống
var check = window.location.href ;
var final = check.substr(check.lastIndexOf('/') + 1);
var text = <?= $text_from_db; ?>
$.ajax({
url:"<?php echo base_url();?>fetch",
type: "post",
dataType: "json",
success: function(data){
var i = "1";
var check_id_js = $("#check_id_js").val();
$('#records').DataTable({
"data": data.posts,
columns: [
{"render": function(){
return a = i++;
}},
{ data: 'name',
},
//{ data: 'email' },
{ "render": function ( data, type, row, meta ) {
var a = `<a href="#" value="${row.id}" id="del" class="btn btn-sm btn-outline-danger"><i class="fas fa-trash"></i></a>
<a href="#" value="${row.id}" id="edit" class="btn btn-sm btn-outline-success"><i class="fas fa-edit"></i></a>`;
return a ;
} }
],
});
}
});
}
fetch();
// Delete Record
$(document).on("click", "#del", function(e){
e.preventDefault();
var del_id = $(this).attr("value");
const swalWithBootstrapButtons = Swal.mixin({
customClass: {
confirmButton: 'btn btn-success',
cancelButton: 'btn btn-danger mr-2'
},
buttonsStyling: false
})
swalWithBootstrapButtons.fire({
title: 'Are you sure?',
text: "You won't be able to revert this!",
icon: 'warning',
showCancelButton: true,
confirmButtonText: 'Yes, delete it!',
cancelButtonText: 'No, cancel!',
reverseButtons: true
}).then((result) => {
if (result.value) {
$.ajax({
url: "<?php echo base_url(); ?>delete",
type: "post",
dataType: "json",
data: {
del_id: del_id
},
success: function(data){
if (data.responce == "success") {
$('#records').DataTable().destroy();
fetch();
swalWithBootstrapButtons.fire(
'Deleted!',
'Your file has been deleted.',
'success'
);
}else{
swalWithBootstrapButtons.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
);
}
}
});
} else if (
/* Read more about handling dismissals below */
result.dismiss === Swal.DismissReason.cancel
) {
swalWithBootstrapButtons.fire(
'Cancelled',
'Your imaginary file is safe :)',
'error'
)
}
});
});
// Edit Record
$(document).on("click", "#edit", function(e){
e.preventDefault();
var edit_id = $(this).attr("value");
$.ajax({
url: "<?php echo base_url(); ?>edit",
type: "post",
dataType: "json",
data: {
edit_id: edit_id
},
success: function(data){
if (data.responce == "success") {
$('#edit_modal').modal('show');
$("#edit_record_id").val(data.post.id);
$("#edit_name").val(data.post.name);
$("#edit_email").val(data.post.email);
}else{
toastr["error"](data.message);
}
}
});
});
// Update Record
$(document).on("click", "#update", function(e){
e.preventDefault();
var edit_record_id = $("#edit_record_id").val();
var edit_name = $("#edit_name").val();
var edit_email = $("#edit_email").val();
if (edit_record_id == "" || edit_name == "" || edit_email == "") {
alert("Both field is required");
}else{
$.ajax({
url: "<?php echo base_url(); ?>update",
type: "post",
dataType: "json",
data: {
edit_record_id: edit_record_id,
edit_name: edit_name,
edit_email: edit_email
},
success: function(data){
if (data.responce == "success") {
$('#records').DataTable().destroy();
fetch();
$('#edit_modal').modal('hide');
toastr["success"](data.message);
}else{
toastr["error"](data.message);
}
}
});
}
});
</script>
Controllers :
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller{
public function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->load->model('crud_model');
}
public function index()
{
$this->load->view('preview');
}
public function insert()
{
if ($this->input->is_ajax_request()) {
$this->form_validation->set_rules('name', 'Name', 'required');
$this->form_validation->set_rules('email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$data = array('responce' => 'error', 'message' => validation_errors());
}
else
{
$ajax_data = $this->input->post();
if ($this->crud_model->insert_entry($ajax_data)) {
$data = array('responce' => 'success', 'message' => 'Ghi chú thành công');
} else {
$data = array('responce' => 'error', 'message' => 'Không thêm được bản ghi');
}
}
echo json_encode($data);
} else {
echo "No direct script access allowed";
}
}
public function fetch(){
if($this->input->is_ajax_request()){
if($posts = $this->crud_model->get_entries()){
$data = array('responce' => 'success','posts' => $posts);
}else{
$data = array('responce' => 'error','massage' => 'Không tìm thấy dữ liệu');
}
echo json_encode($data);
}else{
echo "không cho phép truy cập data";
}
}
public function delete(){
if($this->input->is_ajax_request()){
$del_id = $this->input->post('del_id');
if($this->crud_model->delete_entry($del_id)){
$data = array('responce' => 'success');
}else{
$data = array('responce' => 'error');
}
echo json_encode($data);
}else{
echo "không cho phép truy cập data";
}
}
public function edit()
{
if ($this->input->is_ajax_request()) {
$edit_id = $this->input->post('edit_id');
if ($post = $this->crud_model->edit_entry($edit_id)) {
$data = array('responce' => 'success', 'post' => $post);
} else {
$data = array('responce' => 'error', 'message' => 'failed to fetch record');
}
echo json_encode($data);
} else {
echo "No direct script access allowed";
}
}
public function update(){
if($this->input->is_ajax_request()){
$this->form_validation->set_rules('edit_name', 'Name', 'required');
$this->form_validation->set_rules('edit_email', 'Email', 'required');
if ($this->form_validation->run() == FALSE)
{
$data = array('responce' => 'error', 'message' => validation_errors());
}
else
{
$data['id'] = $this->input->post('edit_record_id');
$data['name'] = $this->input->post('edit_name');
$data['email'] = $this->input->post('edit_email');
if ($this->crud_model->update_entry($data)) {
$data = array('responce' => 'success', 'message' => 'Cập nhật thành công');
} else {
$data = array('responce' => 'error', 'message' => 'Cập nhật không thành công');
}
}
echo json_encode($data);
}else{
echo "No direct script access allowed";
}
}
} ''
models :
`` ` <?php
class Crud_model extends CI_Model {
public function get_entries()
{
$query = $this->db->get('crud');
if (count( $query->result() ) > 0) {
return $query->result();
}
}
public function insert_entry($data)
{
return $this->db->insert('crud', $data);
}
public function delete_entry($id){
return $this->db->delete('crud', array('id' => $id));
}
public function edit_entry($id){
$this->db->select("*");
$this->db->from("crud");
$this->db->where("id", $id);
$query = $this->db->get();
if (count($query->result()) > 0) {
return $query->row();
}
}
public function update_entry($data)
{
return $this->db->update('crud', $data, array('id' => $data['id']));
}
}