-
Junie
Member
-
Posts: 53
Threads: 6
Joined: May 2017
Reputation:
-1
(07-22-2017, 08:42 AM)Paradinight Wrote: Can you pls add the current javascript code and the current php code?
View:
Code: <!-- Start Boxed Section -->
<div class="boxed">
<div class="container">
<div class="row">
</div>
</div>
</div>
<!-- End Boxed Section -->
<section id="content">
<div class="container">
<div class="row">
<div class="col-md-12">
<div class="heading">
<div class="section-title">Add <span>Agency</span></div>
<hr>
</div>
<div class="col-md-4 col-sm-6 col-xs-12">
<div class="activity-item">
<!-- action="<?=('agency/add_agency'); ?>" -->
<form id="agencyForm" class="form-horizontal" method="post">
<input type="hidden" name="agency_id" value="" id="agency_id">
<div class="form-group">
<div class="controls">
<input type="text" name="input_name" class="form-control" placeholder="Agency name">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" name="input_category" class="form-control" placeholder="Category">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" name="input_address" class="form-control" placeholder="Address">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<div class="controls">
<input type="text" name="input_acronym" class="form-control" placeholder="Acronym">
<span class="help-block"></span>
</div>
</div>
<button type="submit" id="btnSave" onclick="save()" class="btn btn-effect"><i class="fa fa-plus-circle" aria-hidden="true"></i> proceed...</button>
</form>
</div>
</div>
<div class="col-md-8 col-sm-6 col-xs-12">
<div class="activity-item">
<div class="table-responsive ">
<table class="table table-striped table-hover table-responsive table-condensed" id="agencyTbl">
<thead>
<tr>
<th>Name</th>
<th>Category</th>
<th>Address</th>
<th>Acronym</th>
</tr>
</thead>
<tbody id="showagency">
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</section>
<div class="clearfix"></div>
<script type="text/javascript">
var save_method; //for save method string
function save() {
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled', true); //set button disable
var url;
if (save_method == 'add') {
url = "<?php echo site_url('agency/addAgency')?>";
} else {
// url = "<?php echo site_url('agency/update_update')?>";
}
// ajax adding data to database
console.log($('#agencyForm').serialize());
$.ajax({
url: url,
type: "POST",
data: $('#agencyForm').serialize(),
// data : formData,
dataType: "JSON",
success: function(data) {
if (data.status) //if success close modal and reload ajax table
{
toastr.info('ajax success');
// reload_table();
} else {
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error adding / update data');
// toastr.info('ajax failed');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
}
});
}
</script>
Controller:
Code: <?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Agency extends CI_Controller {
function __construct(){
parent::__construct ();
$this->load->model('admin/agency_model');
}
public function index()
{
$this->load->view('template/main_header');
$this->load->view('admin/navigation_menu');
$this->load->view('admin/add_agency');
$this->load->view('template/main_footer');
}
public function getAgency(){
$result = $this->agency->showAllAgency();
echo json_encode($result);
}
public function addAgency()
{
var_dump($this->input->post(NULL, TRUE));
// $this->_validate();
$data = array(
'agency_name' => $this->input->post('input_name'),
'category' => $this->input->post('input_category'),
'address' => $this->input->post('input_address'),
'acronym' => $this->input->post('input_acronym'),
);
$insert = $this->agency_model->addNow($data);
echo json_encode(array("status" => TRUE));
}
private function _validate()
{
$data = array();
$data['error_string'] = array();
$data['inputerror'] = array();
$data['status'] = TRUE;
if($this->input->post('input_name') == '')
{
$data['inputerror'][] = 'agency_name';
$data['error_string'][] = 'agency_name is required';
$data['status'] = FALSE;
}
if($this->input->post('input_category') == '')
{
$data['inputerror'][] = 'category';
$data['error_string'][] = 'category is required';
$data['status'] = FALSE;
}
if($this->input->post('input_acronym') == '')
{
$data['inputerror'][] = 'acronym';
$data['error_string'][] = 'acronym is required';
$data['status'] = FALSE;
}
if($this->input->post('input_address') == '')
{
$data['inputerror'][] = 'address';
$data['error_string'][] = 'Addess is required';
$data['status'] = FALSE;
}
if($data['status'] === FALSE)
{
echo json_encode($data);
exit();
}
}
}//end
?>
Model:
Code: <?php
/**
* create by angily
*/
class Agency_model extends CI_Model
{
var $table = 'agency';
function __construct()
{
parent::__construct();
}
public function showAllAgency(){
$this->db->order_by('created', 'desc');
$query = $this->db->get('agency');
if($query->num_rows() > 0){
return $query->result();
}else{
return false;
}
}
public function addNow($data)
{
$this->db->insert($this->table, $data);
return $this->db->insert_id();
}
} // end
?>
-
Junie
Member
-
Posts: 53
Threads: 6
Joined: May 2017
Reputation:
-1
07-22-2017, 09:16 AM
(This post was last modified: 07-22-2017, 09:23 AM by Junie.)
(07-22-2017, 09:06 AM)Paradinight Wrote: 1.
<button type="submit" id="btnSave" onclick="save()" class="btn btn-effect"><i class="fa fa-plus-circle" aria-hidden="true"></i> proceed...</button> <- type="button"
2. save_method <- has no value and because of the empty value url is empty too
Sir,
In the save function, the if else condition for save_method is equals to 'add' and there's also a value for the url. Its just I remove this function upon calling the modal for adding method. Originally this CRUD operation is made using MODAL and I want to create a one-page crud without the modal.
Code: function add_agency() {
save_method = 'add';
$('#agencyForm')[0].reset();
$('.form-group').removeClass('has-error');
$('.help-block').empty();
$('#modal_form').modal('show');
$('.modal-title').text('Add Agency');
}
-
Paradinight
Senior Member
-
Posts: 445
Threads: 6
Joined: Jun 2015
Reputation:
25
(07-22-2017, 09:16 AM)Junie Wrote: (07-22-2017, 09:06 AM)Paradinight Wrote: 1.
<button type="submit" id="btnSave" onclick="save()" class="btn btn-effect"><i class="fa fa-plus-circle" aria-hidden="true"></i> proceed...</button> <- type="button"
2. save_method <- has no value and because of the empty value url is empty too
Sir,
In the save function, the if else condition for save_method is equals to 'add' and there's also a value for the url. Its just I remove this function upon calling the modal for adding method. Originally this CRUD operation is made uing MODAL and I want to create a one-page crud without the modal.
Code: function add_agency() {
save_method = 'add';
$('#agencyForm')[0].reset();
$('.form-group').removeClass('has-error');
$('.help-block').empty();
$('#modal_form').modal('show');
$('.modal-title').text('Add Agency');
}
i need the real code. the code you showed to me is wrong.
Code: var save_method; //for save method string
...
if (save_method == 'add') {
url = "<?php echo site_url('agency/addAgency')?>";
} else {
// url = "<?php echo site_url('agency/update_update')?>";
}
save_method has not the value of add.
-
Junie
Member
-
Posts: 53
Threads: 6
Joined: May 2017
Reputation:
-1
This is the snippet:
Code: <script type="text/javascript">
var save_method; //for save method string
var table;
$(document).ready(function() {
//datatables
table = $('#table').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('person/ajax_list')?>",
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [{
"targets": [-1], //last column
"orderable": false, //set not orderable
}, ],
});
//set input/textarea/select event when change value, remove class error and remove text help block
$("input").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
$("textarea").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
$("select").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
});
function add_person() {
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Add Person'); // Set Title to Bootstrap modal title
}
function edit_person(id) {
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url: "<?php echo site_url('person/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('[name="id"]').val(data.id);
$('[name="firstName"]').val(data.firstName);
$('[name="lastName"]').val(data.lastName);
$('[name="gender"]').val(data.gender);
$('[name="address"]').val(data.address);
$('[name="dob"]').datepicker('update', data.dob);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Person'); // Set title to Bootstrap modal title
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error get data from ajax');
}
});
}
function reload_table() {
table.ajax.reload(null, false); //reload datatable ajax
}
function save() {
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled', true); //set button disable
var url;
if (save_method == 'add') {
url = "<?php echo site_url('person/ajax_add')?>";
} else {
url = "<?php echo site_url('person/ajax_update')?>";
}
// ajax adding data to database
$.ajax({
url: url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data) {
if (data.status) //if success close modal and reload ajax table
{
$('#modal_form').modal('hide');
reload_table();
} else {
for (var i = 0; i < data.inputerror.length; i++) {
$('[name="' + data.inputerror[i] + '"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="' + data.inputerror[i] + '"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
}
});
}
function delete_person(id) {
if (confirm('Are you sure delete this data?')) {
// ajax delete data to database
$.ajax({
url: "<?php echo site_url('person/ajax_delete')?>/" + id,
type: "POST",
dataType: "JSON",
success: function(data) {
//if success reload ajax table
$('#modal_form').modal('hide');
reload_table();
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error deleting data');
}
});
}
}
</script>
-
Paradinight
Senior Member
-
Posts: 445
Threads: 6
Joined: Jun 2015
Reputation:
25
(07-22-2017, 09:29 AM)Junie Wrote: This is the snippet:
Code: <script type="text/javascript">
var save_method; //for save method string
var table;
$(document).ready(function() {
//datatables
table = $('#table').DataTable({
"processing": true, //Feature control the processing indicator.
"serverSide": true, //Feature control DataTables' server-side processing mode.
"order": [], //Initial no order.
// Load data for the table's content from an Ajax source
"ajax": {
"url": "<?php echo site_url('person/ajax_list')?>",
"type": "POST"
},
//Set column definition initialisation properties.
"columnDefs": [{
"targets": [-1], //last column
"orderable": false, //set not orderable
}, ],
});
//set input/textarea/select event when change value, remove class error and remove text help block
$("input").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
$("textarea").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
$("select").change(function() {
$(this).parent().parent().removeClass('has-error');
$(this).next().empty();
});
});
function add_person() {
save_method = 'add';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
$('#modal_form').modal('show'); // show bootstrap modal
$('.modal-title').text('Add Person'); // Set Title to Bootstrap modal title
}
function edit_person(id) {
save_method = 'update';
$('#form')[0].reset(); // reset form on modals
$('.form-group').removeClass('has-error'); // clear error class
$('.help-block').empty(); // clear error string
//Ajax Load data from ajax
$.ajax({
url: "<?php echo site_url('person/ajax_edit/')?>/" + id,
type: "GET",
dataType: "JSON",
success: function(data) {
$('[name="id"]').val(data.id);
$('[name="firstName"]').val(data.firstName);
$('[name="lastName"]').val(data.lastName);
$('[name="gender"]').val(data.gender);
$('[name="address"]').val(data.address);
$('[name="dob"]').datepicker('update', data.dob);
$('#modal_form').modal('show'); // show bootstrap modal when complete loaded
$('.modal-title').text('Edit Person'); // Set title to Bootstrap modal title
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error get data from ajax');
}
});
}
function reload_table() {
table.ajax.reload(null, false); //reload datatable ajax
}
function save() {
$('#btnSave').text('saving...'); //change button text
$('#btnSave').attr('disabled', true); //set button disable
var url;
if (save_method == 'add') {
url = "<?php echo site_url('person/ajax_add')?>";
} else {
url = "<?php echo site_url('person/ajax_update')?>";
}
// ajax adding data to database
$.ajax({
url: url,
type: "POST",
data: $('#form').serialize(),
dataType: "JSON",
success: function(data) {
if (data.status) //if success close modal and reload ajax table
{
$('#modal_form').modal('hide');
reload_table();
} else {
for (var i = 0; i < data.inputerror.length; i++) {
$('[name="' + data.inputerror[i] + '"]').parent().parent().addClass('has-error'); //select parent twice to select div form-group class and add has-error class
$('[name="' + data.inputerror[i] + '"]').next().text(data.error_string[i]); //select span help-block class set text error string
}
}
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error adding / update data');
$('#btnSave').text('save'); //change button text
$('#btnSave').attr('disabled', false); //set button enable
}
});
}
function delete_person(id) {
if (confirm('Are you sure delete this data?')) {
// ajax delete data to database
$.ajax({
url: "<?php echo site_url('person/ajax_delete')?>/" + id,
type: "POST",
dataType: "JSON",
success: function(data) {
//if success reload ajax table
$('#modal_form').modal('hide');
reload_table();
},
error: function(jqXHR, textStatus, errorThrown) {
alert('Error deleting data');
}
});
}
}
</script>
add_person is not called in you code
|