i have this page
and i have this modal
now i what i want to do is when i click on the "Editar" button of the row that contains the ID Cartão of "1011022" it should show like this:
with the values belonging to that row
how do i do that ? i've already searched for at least an hour and i couldn't find any reliable solutions
my html code is as follows:
#extends('admin.layouts.layout')
#section('content')
<!-- page script -->
<div class="wrapper row-offcanvas row-offcanvas-left">
<!-- Right side column. Contains the navbar and content of the page -->
<aside class="right-side">
<!-- Content Header (Page header) -->
<section class="content-header">
<h1>
Lista de alunos
</h1>
</section>
<!-- Main content -->
<section class="content">
<div class="row">
<div class="col-xs-12">
<div class="box">
<div class="box-header">
</div> <!-- /.box-header -->
<div class="box-body table-responsive">
<button type="button" id="add_btn" class="btn btn-primary" data-toggle="modal" data-target="#addAluno">
<span class="glyphicon glyphicon-plus"></span>
Adicionar aluno
</button>
<br/><br/>
<!-- ADD MODAL SECTION -->
<div id="addAluno" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Adicionar aluno</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label>ID do Cartão</label>
<input type="text" class="form-control" id="id_cartao" placeholder="ID do Cartão"/>
</div>
<div class="form-group">
<label>Nome do Aluno</label>
<input type="text" class="form-control"
id="nome_aluno" placeholder="Nome do Aluno"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" id="email" placeholder="E-mail"/>
</div>
<div>
<label>Triénio</label>
<input type="text" class="form-control" id="trienio" placeholder="Triénio"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Adicionar aluno</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<!-- END OF THE ADD MODAL SECTION -->
<!-- EDIT MODAL SECTION -->
<div id="editAluno" class="modal fade" role="dialog">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title">Editar aluno</h4>
</div>
<div class="modal-body">
<form role="form">
<div class="form-group">
<label>ID do Cartão</label>
<input type="text" class="form-control" id="id_cartao" placeholder="ID do Cartão" value=>
</div>
<div class="form-group">
<label>Nome do aluno</label>
<input type="text" class="form-control"
id="nome_aluno" placeholder="Nome do Aluno"/>
</div>
<div class="form-group">
<label>Email</label>
<input type="email" class="form-control" id="email" placeholder="E-mail"/>
</div>
<div>
<label>Triénio</label>
<input type="text" class="form-control" id="trienio" placeholder="Triénio"/>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary">Editar aluno</button>
<button type="button" class="btn btn-default" data-dismiss="modal">Fechar</button>
</div>
</div>
</div>
</div>
<!-- END OF THE EDIT MODAL SECTION -->
<table id="alunos_list" class="table table-bordered table-striped">
<thead>
<tr>
<th>ID Cartão</th>
<th>Nome</th>
<th>E-mail</th>
<th>Triénio</th>
<th>Curso</th>
<th>Ações</th>
</tr>
</thead>
<tfoot>
<tr>
<th>ID Cartão</th>
<th>Nome</th>
<th>E-mail</th>
<th>Triénio</th>
<th>Curso</th>
<th>Ações</th>
</tr>
</tfoot>
<tfoot>
</tfoot>
</table>
</div><!-- /.box-body -->
</div><!-- /.box -->
</div>
</div>
</section><!-- /.content -->
</aside><!-- /.right-side -->
</div><!-- ./wrapper -->
#endsection
#push('scripts')
<script>
$(function() {
$('#alunos_list').DataTable({
processing: true,
serverSide: true,
ajax: '{!! route('alunos.data') !!}',
columns: [
{ data: 'id_cartao', name: 'alunos.id_cartao' },
{ data: 'nome', name: 'alunos.nome' },
{ data: 'email', name: 'alunos.email' },
{ data: 'trienio.data_trienio', name: 'trienio.data_trienio' },
{ data: 'trienio.curso.curso', name: 'trienio.curso.curso' },
{ data: 'action', name: 'action', orderable: false, searchable: false }
],
initComplete: function () {
this.api().columns().every(function () {
var column = this;
if(column[0][0] == 5){
// intentionally empty, we want to exclude column 5 from searching
} else {
var input = document.createElement("input");
$(input).appendTo($(column.footer()).empty())
.on('keypress', function () {
column.search($(this).val(), false, false, true).draw();
});
}
});
}
});
});
</script>
<script>
</script>
#endpush
Inside your editor button, put data target to target editor's modal. After that, we listen for show event of modal when it show/shown like following:
Html
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#editAluno">
<span class="glyphicon glyphicon-plus"></span> Edit
</button>
Js
You can choose either show.bs.modal or shown.bs.modal, recommended just listen to show.bs.modal as you want to populate the data immediately after button clicked. Notice relatedTarget :
// listening for show event of editAluno's modal
$( '#editAluno' ).on( 'show.bs.modal', function (e) {
var target = e.relatedTarget;
// get values for particular rows
var tr = $( target ).closest( 'tr' );
var tds = tr.find( 'td' );
// put values into editor's form elements
// tds.eq(0).val() -- 1st column
$( '#id_cartao' ).val( tds.eq(0).val() );
// tds.eq(1).val() -- 2nd column and so on.
// same goes to others element
});
Related
The spanish version failed me, so I'll test my luck here
I have the following code and I would like to know how I can pass the data from my table to the modal
This is my code in edit.blade.php. The modal does work but I don't know how to make the data show up in the modal, I'll do POST to the database myself but I just want to know how I make that data show up so I can edit it.
The data that I want to appear in the modal are the id, the name and the email.
This is my code in editar.blade.php
<script src="{{ url('/js/vendor/jquery-1.12.4.min.js') }} "></script>
<script src="{{ url('/js/bootstrap.min.js') }} "></script>
<link rel="stylesheet" href="{{ url('/css/bootstrap.min.css') }} ">
<script src="{{ url('/js/jquery.dataTables.min.js') }} "></script>
<div class="container">
<h2>Editar estudiante</h2>
<div class="container">
<h2>Laravel DataTables Tutorial Example</h2>
<table id="user_table" class="table table-bordered table-striped">
<thead>
<tr>
<th width="35%" id="name">Name</th>
<th width="35%" id="email">Email</th>
<th width="30%">Action</th>
</tr>
</thead>
</table>
</div>
<div class="modal fade" id="edit-modal" tabindex="-1" role="dialog" aria-labelledby="edit-modal-label" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="edit-modal-label">Edit Data</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body" id="attachment-body-content">
<form id="edit-form" class="form-horizontal" method="POST">
<div class="card text-white bg-dark mb-0">
<div class="card-header">
<h2 class="m-0">Edit</h2>
</div>
<div class="card-body">
<!-- id -->
<input type="hidden" name="modal-input-id" class="form-control" id="modal-input-id" required>
<!-- /id -->
<!-- name -->
<div class="form-group">
<label class="col-form-label" for="modal-input-name">Name</label>
<input type="text" name="modal-input-name" class="form-control" id="modal-input-name" required autofocus>
</div>
<!-- /name -->
<!-- description -->
<div class="form-group">
<label class="col-form-label" for="modal-input-description">Email</label>
<input type="text" name="modal-email" class="form-control" id="modal-input-email" required>
</div>
<!-- /description -->
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal">Done</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){
$('#user_table').DataTable({
processing: true,
serverSide: true,
ajax: {
url: "{{ route('getdata') }}",
},
columns: [
{
data: 'name',
name: 'name',
className: 'user-name',
},
{
data: 'email',
name: 'email',
className: 'user-email',
},
{
data: 'action',
name: 'action',
orderable: false
}
]
});
$(document).on('click', "#edit-item", function() {
//obtenemos la instancia de la fila
$tr = $(this).closest('tr');
//rellenamos los campos en el modal
$('#modal-input-name').val($('user-name', $tr).text());
$('#modal-input-email').val($('user-email', $tr).text());
// Mostramos el modal
$('#edit-modal').modal('show');
})
// on modal hide
$('#edit-modal').on('hide.bs.modal', function() {
$('.edit-item-trigger-clicked').removeClass('edit-item-trigger-clicked')
$('#edit-form').trigger('reset');
})
});
</script>
This is my get route in web.php
Route::get('/editar', 'AdminController#editar')->name('editar');
This is my function to bring data to the Datatable in AdminController.php
public function getPosts(Request $request)
{
if($request->ajax()){
$data = StudentModel::latest()->get();
return DataTables::of($data)
->addColumn('action', function($data){
$button = '<button id="edit-item" type="button" name="edit-item" data-target-id="'.$data->id.'" class="edit btn btn-primary btn-sm" data-toggle="modal" data-target="edit-modal">Edit</button>';
$button .= ' <button type="button" name="edit" data-target-id="'.$data->id.'" class="delete btn btn-danger btn-sm">Delete</button>';
return $button;
})
->rawColumns(['action'])
->make(true);
}
return view('editar');
}
Can you try this?
$('#modal-input-name').val($tr.find('td.user-name').text());
$('#modal-input-email').val($tr.find('td.user-email').text());
And also I would suggest that you use a css class for targeting a click event, as Id is normally for a unique identifier, and by doing your way you are having many table rows with the same Id which defeats the original idea.
I have this template
<template>
<div class="main">
<div class="container-fluid" style="padding-top: 2.5%">
<h4 class="page-title"><i class="lnr lnr-funnel"> </i> Infusionsoft Accounts
<button #click="addInfusionsoft" class="btn btn-xs btn-success" ><i class="lnr lnr-plus-circle"> </i> New </button>
</h4>
<div class="row">
<div class="col-md-12">
<table class="table table-responsive table-bordered table-striped">
<tr>
<th>ID</th>
<th>App Name</th>
<th>Auth Key</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th width="300px">Action</th>
</tr>
</table>
</div>
</div>
<div class="modal fade" id="modalAddAccount" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method="POST" action="" id="addInfsAccount"/>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Infusionsoft Account</h4>
</div>
<div class="modal-body">
<p class="small">Fill up the form to add a new Infusionsoft Account.</p>
<br/>
<div class="form-group row">
<div class="col-md-8 col-md-offset-2">
<input id="appName" type="text" required placeholder="App Name e.g l328" class="form-control" name="appName">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Account</button>
</div>
</div>
</form>
</div>
</div>
<app-legend/>
</div>
</div>
</template>
<script>
import Legend from "./Legend";
import NavBar from "../layouts/NavBar";
import SideBar from "../layouts/SideBar";
export default {
data() {
return {
infsModal: false,
}
},
created() {
},
components: {
'app-legend': Legend,
'nav-bar': NavBar,
'side-bar': SideBar,
},
methods: {
addInfusionsoft() {
$('#modalAddAccount').modal('show');
}
},
}
</script>
I'm using this template for the project https://www.themeineed.com/downloads/klorofil-free-bootstrap-admin-template/, I kept everything as default and didn't have any major modifications with the bootstrap classes. If I use jquery function to call the modal it will show correctly, however moving the modal to another template and calling is by using v-model and v-if, nothing really happens. Please see sample below
parent template
<template>
<div class="main">
<div class="container-fluid" style="padding-top: 2.5%">
<h4 class="page-title"><i class="lnr lnr-funnel"> </i> Infusionsoft Accounts
<button #click="infsModal = true" class="btn btn-xs btn-success" ><i class="lnr lnr-plus-circle"> </i> New </button>
</h4>
<div class="row">
<div class="col-md-12">
<table class="table table-responsive table-bordered table-striped">
<tr>
<th>ID</th>
<th>App Name</th>
<th>Auth Key</th>
<th>Status</th>
<th>Created</th>
<th>Updated</th>
<th width="300px">Action</th>
</tr>
</table>
</div>
</div>
<div v-if="infsModal">
<div class="modal fade" id="modalAddAccount" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method="POST" action="" id="addInfsAccount"/>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Infusionsoft Account</h4>
</div>
<div class="modal-body">
<p class="small">Fill up the form to add a new Infusionsoft Account.</p>
<br/>
<div class="form-group row">
<div class="col-md-8 col-md-offset-2">
<input id="appName" type="text" required placeholder="App Name e.g l328" class="form-control" name="appName">
<span v-if="false" span class="invalid-feedback">
<strong>Error</strong>
</span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Account</button>
</div>
</div>
</form>
</div>
</div>
</div>
<app-legend/>
</div>
</div>
</template>
<script>
import Legend from "./Legend";
import NavBar from "../layouts/NavBar";
import SideBar from "../layouts/SideBar";
import InfusionsoftAdd from "../infusionsoft/InfusionsoftAdd";
export default {
data() {
return {
infsModal: false,
}
},
created() {
},
components: {
'app-legend': Legend,
'nav-bar': NavBar,
'side-bar': SideBar,
'infs-modal': InfusionsoftAdd,
},
methods: {
addInfusionsoft() {
//this.$router.push('/infusionsoft')
}
},
}
</script>
this is the modal:
<template>
<div v-if="dialog">
<transition name="modal">
<div class="modal fade" id="modalAddAccount" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<form method="POST" action="" id="addInfsAccount"/>
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title" id="myModalLabel">Infusionsoft Account</h4>
</div>
<div class="modal-body">
<p class="small">Fill up the form to add a new Infusionsoft Account.</p>
<br/>
<div class="form-group row">
<div class="col-md-8 col-md-offset-2">
<input id="appName" type="text" required placeholder="App Name e.g l328" class="form-control" name="appName">
<span v-if="false" span class="invalid-feedback">
<strong>Error</strong>
</span>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" class="btn btn-primary">Add Account</button>
</div>
</div>
</form>
</div>
</div>
</transition>
</div>
</template>
<script>
export default {
data() {
return {
dialog: true
}
},
methods: {
back() {
//this.$router.back();
}
}
}
</script>
Is there anything I'm doing wrong?
When you set infsModal to true, all that's happening is that you're including the modal code but you're still not displaying the modal. You can still use jQuery here if you'd like, no reason not to. You can remove the infsModal variable since it's not needed (the modal won't display even if that's set to true... you still need to tell Bootstrap to actually show the modal). If you want to get away from jQuery, a solid option is Bootstrap-Vue https://bootstrap-vue.org/docs/components/modal ... let me know if that helps!
I just tried to insert data using ajax json but I'm getting a error like this. I checked in the controller, route and model. There's nothing wrong with how to set it up. Please help.
For View Blade
function add() {
save_method = 'add';
$('#form_jabatan')[0].reset(); // reset form on modals
$('#modal_form').modal('show'); // show bootstrap modal
$('[name="kode"]').prop("readonly", false);
}
function save() {
var url = "";
if (save_method === 'add') {
url = "<?php echo url('/simpanjabatan') ?>";
} else {
url = "<?php echo url('/simpanjabatan') ?>";
}
$.ajax({
url: url,
type: "POST",
data: $('#form_jabatan').serialize(),
dataType: "JSON",
success: function (data) {
if (data.errors) {
jQuery.each(data.errors, function (key, value) {
jQuery('.alert-danger').show();
jQuery('.alert-danger').append('<p>' + value + '</p>');
});
} else {
alert(data.status);
$('#modal_form').modal('hide');
reloadpage();
}
},
error: function (request, status, error) {
alert("Error json " + error);
json = $.parseJSON(request.responseText);
$.each(json.errors, function (key, value) {
$('.alert-danger').show("");
$('.alert-danger').append('<p>' + value + '</p>');
});
}
});
}
</script>
<!-- Page Content -->
<div class="content">
<h2 class="content-heading">Form Detail Jabatan</h2>
<!-- Dynamic Table Full -->
<div class="block">
<div class="block-header block-header-default">
<h3 class="block-title">DATA TABLE Detail Jabatan</h3>
</div>
<div class=" block-content block-content-full table-responsive">
<button type="button" onclick="add();" class="btn btn-primary mr-5 mb-5" data-toggle="modal">
<i class="fa fa-plus mr-5"></i>Kelola Jabatan
</button>
<div class="table-responsive">
<table id="example" class=" table table-striped js-dataTable-full-pagination ">
<thead>
<tr>
<th >ID JABATAN</th>
<th >JENIS JABATAN</th>
<th >GAJI_POKOK</th>
<th >Aksi</th>
</thead>
<tbody>
</tbody>
</table>
</div>
<!-- DataTables functionality is initialized with .js-dataTable-full-pagination class in js/pages/be_tables_datatables.min.js which was auto compiled from _es6/pages/be_tables_datatables.js -->
</div>
</div>
</div>
<!-- END Page Content -->
<!-- From Right Modal -->
<div class="modal fade" id="modal_form" tabindex="-1" role="dialog" aria-labelledby="modal-fromright"
aria-hidden="true">
<div class="modal-dialog modal-dialog-fromright" role="document">
<div class="modal-content">
<div class="block block-themed block-transparent">
<div class="block-header bg-primary-dark">
<h3 class="block-title">FORM Detail Jabatan</h3>
<div class="block-options">
<button type="button" class="btn-block-option" data-dismiss="modal" aria-label="Close">
<i class="si si-close"></i>
</button>
</div>
</div>
<div class="modal-body form">
<form action="#" id="form_jabatan" class="form-horizontal">
<input name="_token" type="hidden" id="_token" value="{{ csrf_token() }}"/>
<div class="form-body">
<div class="form-group">
{{--<label type="hidden" class="control-label col-md-3">Kode</label>--}}
<div class="col-md-9">
<input name="kode" type="hidden" class="form-control" type="text"
placeholder="Kode">
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label" for="val-name">Jenis Jabatan</label>
<div class="col-md-9">
<select class="form-control" id="jabatan" name="jabatan">
<option value="0">Silahkan Pilih</option>
<option value="1">Ka Mantri</option>
<option value="1">Ka Mantri(baru)</option>
<option value="2">Kasir</option>
<option value="2">Staff Admin</option>
<option value="2">Mantri</option>
<option value="2">Mantri(Baru)</option>
</select>
</div>
</div>
<div class="form-group row">
<label class="col-md-3 col-form-label" for="val-name">Gaji Pokok</label>
<div class="col-md-9">
<input type="text" class="form-control" id="gaji" name="gaji"
placeholder="Masukan gaji...">
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" id="btnSave" onclick="save();" class="btn btn-primary">Save
</button>
</div>
</div>
</div>
</div>
</div>
<!-- END From Right Modal -->
For Controller
public function simpanjabatan(Request $req)
{
$obj = new DetailJabatanModel();
$obj->id_jabatan = $req->input('kode');
$obj->jabatan = $req->input('jabatan');
$obj->gaji_pokok = $req->input('gaji');
$simpan = $obj->save();
if ($simpan == 1) {
$status = "Tersmpan";
} else {
$status = "Gagal";
}
echo json_encode(array("status" => $status));
}
for route
Route::post('/simpanjabatan', 'FormJabatanController#simpanjabatan');
I want to insert with json but I got erorr:
The GET method is not supported for this route. Supported methods: POST.
So I was developing some crud application with CodeIgniter Framework. But I am facing issues while retrieving data from the database. I am getting a 404 Not Found error for the AJAX function. The function is where it should be but I can't seem to find why it's giving me an error.
Please find the following code for the files and let me know if I am doing anything wrong here.
package_view.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Package List</title>
<link rel="stylesheet" type="text/css" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/1.10.20/css/jquery.dataTables.min.css">
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/css/dataTables.bootstrap4.css">
</head>
<body>
<div class="container">
<!-- Page Heading -->
<div class="row">
<div class="col-12">
<div class="col-md-12">
<h1>Package
<small>List</small>
<div class="float-right"><span class="fa fa-plus"></span> Add New</div>
</h1>
</div>
<table class="table table-striped" id="mydata">
<thead>
<tr>
<th>Package ID</th>
<th>Test Quantity</th>
<th>Price</th>
<th style="text-align: right;">Actions</th>
</tr>
</thead>
<tbody id="show_data">
</tbody>
</table>
</div>
</div>
</div>
<!-- MODAL ADD -->
<form>
<div class="modal fade" id="Modal_Add" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Add New Package</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label class="col-md-2 col-form-label">Test Quantity</label>
<div class="col-md-10">
<input type="text" name="test_quantity" id="test_quantity" class="form-control" placeholder="Test Quantity">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Price</label>
<div class="col-md-10">
<input type="text" name="price" id="price" class="form-control" placeholder="Price">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" type="submit" id="btn_save" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</form>
<!--END MODAL ADD-->
<!-- MODAL EDIT -->
<form>
<div class="modal fade" id="Modal_Edit" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Edit Package</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<div class="form-group row">
<label class="col-md-2 col-form-label">Package ID</label>
<div class="col-md-10">
<input type="text" name="pkg_id_edit" id="pkg_id_edit" class="form-control" placeholder="Package ID" readonly>
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Test Quantity</label>
<div class="col-md-10">
<input type="text" name="test_quantity_edit" id="test_quantity_edit" class="form-control" placeholder="Test Quantity">
</div>
</div>
<div class="form-group row">
<label class="col-md-2 col-form-label">Price</label>
<div class="col-md-10">
<input type="text" name="price_edit" id="price_edit" class="form-control" placeholder="Price">
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" type="submit" id="btn_update" class="btn btn-primary">Update</button>
</div>
</div>
</div>
</div>
</form>
<!--END MODAL EDIT-->
<!--MODAL DELETE-->
<form>
<div class="modal fade" id="Modal_Delete" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalLabel">Delete Package</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<strong>Are you sure to delete this record?</strong>
</div>
<div class="modal-footer">
<input type="hidden" name="pkg_id_delete" id="pkg_id_delete" class="form-control">
<button type="button" class="btn btn-secondary" data-dismiss="modal">No</button>
<button type="button" type="submit" id="btn_delete" class="btn btn-primary">Yes</button>
</div>
</div>
</div>
</div>
</form>
<!--END MODAL DELETE-->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script type="text/javascript" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/js/bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/datatables/1.10.19/js/dataTables.bootstrap4.js"></script>
<script type="text/javascript">
$(document).ready(function(){
show_package(); //call function show all package
$('#mydata').dataTable();
//function show all package
function show_package(){
$.ajax({
type : 'ajax',
url : '<?php echo site_url('Package/package_data')?>',
async : true,
dataType : 'json',
success : function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+data[i].pkg_id+'</td>'+
'<td>'+data[i].test_quantity+'</td>'+
'<td>'+data[i].price+'</td>'+
'<td style="text-align:right;">'+
'Edit'+' '+
'Delete'+
'</td>'+
'</tr>';
}
$('#show_data').html(html);
}
});
}
//Save package
$('#btn_save').on('click',function(){
var pkg_id = $('#pkg_id').val();
var test_quantity = $('#test_quantity').val();
var price = $('#price').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('Package/save')?>",
dataType : "JSON",
data : {pkg_id:pkg_id , test_quantity:test_quantity, price:price},
success: function(data){
$('[name="pkg_id"]').val("");
$('[name="test_quantity"]').val("");
$('[name="price"]').val("");
$('#Modal_Add').modal('hide');
show_package();
}
});
return false;
});
//get data for update record
$('#show_data').on('click','.item_edit',function(){
var pkg_id = $(this).data('pkg_id');
var test_quantity = $(this).data('test_quantity');
var price = $(this).data('price');
$('#Modal_Edit').modal('show');
$('[name="pkg_id_edit"]').val(pkg_id);
$('[name="test_quantity_edit"]').val(test_quantity);
$('[name="price_edit"]').val(price);
});
//update record to database
$('#btn_update').on('click',function(){
var pkg_id = $('#pkg_id_edit').val();
var test_quantity = $('#test_quantity_edit').val();
var price = $('#price_edit').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('Package/update')?>",
dataType : "JSON",
data : {pkg_id:pkg_id , test_quantity:test_quantity, price:price},
success: function(data){
$('[name="pkg_id_edit"]').val("");
$('[name="test_quantity_edit"]').val("");
$('[name="price_edit"]').val("");
$('#Modal_Edit').modal('hide');
show_package();
}
});
return false;
});
//get data for delete record
$('#show_data').on('click','.item_delete',function(){
var pkg_id = $(this).data('pkg_id');
$('#Modal_Delete').modal('show');
$('[name="pkg_id_delete"]').val(pkg_id);
});
//delete record to database
$('#btn_delete').on('click',function(){
var pkg_id = $('#pkg_id_delete').val();
$.ajax({
type : "POST",
url : "<?php echo site_url('Package/delete')?>",
dataType : "JSON",
data : {pkg_id:pkg_id},
success: function(data){
$('[name="pkg_id_delete"]').val("");
$('#Modal_Delete').modal('hide');
show_package();
}
});
return false;
});
});
</script>
</body>
</html>
Packages.php
<?php
class Packages extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('package_model');
}
function index(){
$this->load->view('package_view');
}
function package_data(){
$data=$this->package_model->package_list();
echo json_encode($data);
}
function save(){
$data=$this->package_model->save_package();
echo json_encode($data);
}
function update(){
$data=$this->package_model->update_package();
echo json_encode($data);
}
function delete(){
$data=$this->package_model->delete_package();
echo json_encode($data);
}
}
and
package_model.php
<?php
class package_model extends CI_Model{
function package_list(){
$hasil=$this->db->get('tblexampackages');
return $hasil->result();
}
function save_package(){
$data = array(
'pkg_id' => $this->input->post('pkg_id'),
'test_quantity' => $this->input->post('test_quantity'),
'price' => $this->input->post('price'),
);
$result=$this->db->insert('tblexampackages',$data);
return $result;
}
function update_package(){
$pkg_id=$this->input->post('pkg_id');
$test_quantity=$this->input->post('test_quantity');
$price=$this->input->post('price');
$this->db->set('price', $price);
$this->db->set('test_quantity', $test_quantity);
$this->db->where('pkg_id', $pkg_id);
$result=$this->db->update('tblexampackages');
return $result;
}
function delete_package(){
$pkg_id=$this->input->post('pkg_id');
$this->db->where('pkg_id', $pkg_id);
$result=$this->db->delete('tblexampackages');
return $result;
}
}
Other files are autoload.php, config.php and database.php which are configured properly. Please tell me where I am going wrong here.
As for database, MySQL connectivity is good and the table named tblexampackages only has 3 Columns named pkg_id, test_quantity and price.
Thanks.
Your controller is Packages, you're trying to access Package... Edit this line from
url : '<?php echo site_url('Package/package_data')?>', //old
url : "<?php echo site_url('Packages/package_data')?>" //new
After which, edit your package_list to something like this
$query = $this->db->get('tbl_name');
return $query->result_array();
in your Ajax function show_package() you have:
url : '<?php echo site_url('Package/package_data')?>',
it should be
url : '<?php echo site_url('Packages/package_data')?>',
I've created a button to show a modal but the thing is the button is working as an insert into DB as I don't know what's the problem in my code.
I need the modal to show a table of products fetched form the DB and showed as a table form later on in a report.
here's the modal code inside the form..
<div class="container">
<!-- ======================= Froms ======================= -->
<form action="input.php" method="post">
<div class="row">
<div class="form-group">
<label class="col-md-2 col-form-label">Attention:</label>
<div class="col-md-3">
<select class="form-control" placeholder="Choose" name="Attention" required="true">
<option>Asset</option>
<option>Consumable</option>
<option>Tool</option>
</select>
</div>
</div>
<div class="form-group">
<label class="col-md-2 col-form-label">PR No.:</label>
<div class="col-md-3">
<input class="form-control" type="text" name="PRno">
</div>
</div>
</div>
<!-- End of 1st Row -->
<div class="row">
<div class="form-group">
<label class="col-md-2 col-form-label">Project Name:</label>
<div class="col-md-3">
<select class="form-control" placeholder="Choose" name="ProjectName">
<option>Stock</option>
<option>In Use</option>
<option>Scrap</option>
<option>Lost</option>
</select>
</div>
</div>
</div>
<!-- End of 2nd Row -->
<!-- ============== Items Table ============= -->
<div class="row">
<div class="table-responsive">
<table class="table table-bordered" id="tab_logic">
<thead>
<tr>
<th>Item Description</th>
<th>Part Number</th>
<th>Unit</th>
<th>QTY</th>
<th>Unit Price (SR)</th>
<th>Total Price (SR)</th>
</tr>
</thead>
<tbody>
<tr>
<td class="col-md-4">
<button class="btn btn-secondary" data-toggle="modal">Select ID</button>
<div class="modal fade bd-example-modal-lg" tabindex="-1" role="dialog" aria-hidden="false">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<!-- HEADER | MODAL -->
<h3 class="modal-title">Title Here</h3>
</div>
<!-- BODY | MODAL -->
<div class="modal-body">
Text Content
</div>
<!-- FOOTER | MODAL -->
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-secondary">Save changes</button>
</div>
</div>
</div>
</div>
</td>
<td>
<strong>Record</strong>
</td>
<td>
<strong>Record</strong>
</td>
<td class="col-md-1">
<div>
<input class="form-control" type="number" name="PRno">
</div>
</td>
<td class="col-md-2">
<div>
<input class="form-control" type="number" name="price" step="0.01" min="0.01" max="1000000">
</div>
</td>
<td>
<strong>Record</strong>
</td>
<td class="col-md-1" id="deleteBtn">
<a class="btn btn-default deleteBtn">Delete</a>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<!-- End of 3rd Row -->
<div class="row">
<a id="add_row" class="btn btn-default pull-right">Add</a>
</div>
<!-- End of 4th Row -->
<!-- ============== Items Table ============= -->
<div class="input-tigger">
<input type="submit" class="btn btn-primary btn-md" name="btninsert" value="Insert" action="input.php" method="post">
</div>
</form>
</div>
and as shown I added the inset button to insert all the form into the DB.
How could the modal work as a insert button instead of the actual button?
You can use javascript/Jquery to prevent the default action of the button and write methods instance of that. I think this will be helpful to you.
$(function() // execute once the DOM has loaded
{
// wire up Add Item button click event
$("#myButton").click(function(event) //give an ID to modal button
{
event.preventDefault(); // cancel default behavior
$('#myModal').modal('show'); //give an ID to modal
//... rest of add logic
});
});