Currently I have an edit page where any changes I make are reflected in my database. But one part of my form is not sending the data which is the notes tab where if there is no notes, the user can add a new note to the database, or if a note already exists, then they should be able to edit it. Currently this is my code:
View Class:
<div id="notes-wrap">
<table id="tableobj1">
<thead>
<tr class="active">
<th>Date</th>
<th>Notes</th>
<th nowrap>Added By</th>
<th colspan="2" nowrap>Action</th>
</tr>
</thead>
<tbody>
<tr id="objrows1" class="tblerow1">
<td colspan="3"><textarea name="note" class="form-control notes" placeholder="Notes"></textarea></td>
<td colspan="2" width="1%"><button type="submit" class="btn-sm savenotes" data-href="<?php echo site_url('contacts/savenotes/'.$leadid); ?>" >
<i class="fa fa-save"></i></button></td>
</tr>
<?php if($notes) foreach($notes as $val):?>
<tr>
<td nowrap="nowrap"><?php echo($val['added_date']);?></td>
<td width="75%" class="textnote"><?php echo nl2br($val['note']) ?></td>
<td width="15%"><small><?php echo $val['display_name'] ?></small></td>
<td width="1%" align="center"><a><i class="fa fa-pencil"></i></a><a data-url="<?php echo site_url('contacts/updatenote/'.$val['notes_id']) ?>" style="display:none"><i class="fa fa-save"></i></a></td>
<td width="1%" valign="middle" align="center"><a data-url="<?php echo site_url('contacts/deletenote/'.$val['notes_id']) ?>" ><i class="fa fa-trash"></i></a></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<button type="submit" class="btn btn-success waves-light">Submit</button>
JS
$(document).ready(function(){
$("#notes-wrap").on("click", "button.savenotes", function(e){
e.preventDefault();
saveleadnotes($(this));
});
$("#tableobj1").on("click", "tbody a.remove", function(e) {
var $t=$(this).closest ('tr');$u = $(this).data('url');
swal({
title: "Are you sure?",
text: "You will not be able to recover this!",
type: "warning",
showCancelButton: true,
confirmButtonClass: 'btn-warning',
confirmButtonText: "Yes",
closeOnConfirm: true
}, function (e) {
if($u && e){
removeobj($u);
$t.remove();
}
else if(e){
$t.remove();
}
});
});
function saveleadnotes(obj){
$.ajax({
url: obj.data('href'),
async:false,
type:"post",
data:obj.closest('.tblerow1').find(':input').serialize(),
success: function(result){
if(result<1)
{
obj.closest('.tblerow1').find('[name="note"]').focus();
}
else {
leadnotesdocs('<?php echo site_url('contacts/notes/'.$contact->id); ?>','#notes-wrap');
}
}
});
}
Controller Class:
function edit($id,$return=0)
{
if ($this->form_validation->run() == FALSE)
{
$edit['notes']= $this->contacts_model->load_notes($id);
$edit['leadid']= $id;
} else {
$maindata=array('agents_id'=>$this->input->post('agents_id'));
$updated=$this->contacts_model->update($maindata,$id);
if($updated){
if($this->input->post('notes')){
foreach($this->input->post('notes') as $key => $note):
if($note !='') {
$smdata = array('contacts_id'=>$id,'note'=>$note,'added_by'=>$this->session->userdata('clientsessuserid'));
$this->contacts_model->save_notes($smdata); }
endforeach;
}
}
public function updatenote($id){
if($this->form_validation->run() == TRUE)
{
$maindata = array('note'=>$this->security->xss_clean($this->input->post('leadnote')));
$insid= $this->contacts_model->update_notes($maindata,array('notes_id'=>$id));
$c= $this->contacts_model->load($id,"refno");
$this->contacts_model->saveactivity(array('object_ref'=>$c->refno,'object_id'=>$insid,'activity'=>'Note Updated','action'=>'Updated'));
echo $insid;exit;
}
}
public function savenotes($id){
if($this->form_validation->run() == TRUE)
{
$maindata = array('contacts_id'=>$id,'note'=>$this->input->post('note'),'added_by'=>$this->session->userdata('clientsessuserid'));
$insid= $this->contacts_model->save_notes($maindata);
$contacts= $this->contacts_model->load($id,"refno");
$this->contacts_model->saveactivity(array('object_ref'=>$contacts->refno,'object_id'=>$insid,'activity'=>'Note Added','action'=>'Added'));
echo $insid;exit;
}
}
Model Class:
function save_notes($maindata){
$this->db->insert("crm_contacts_notes",$maindata);
return $this->db->insert_id();
}
function load($id)
{
$id=$this->db->escape_str($id);
$cond=array('id'=>$id);
$this->db->where($cond);
$this->db->from("crm_contacts");
$query = $this->db->get();
return $query->row();
}
function update_notes($maindata,$cond){
return $this->db->update("crm_contacts_notes",$maindata,$cond);
}
But doing so just refreshes the page whenever I click on the save button and nothing is sent to my database. I cannot even see if any requests are being made since whenever I click the save button, the page refreshes.
Related
I am trying to using Ajax to update the database to skip the standard submission and not refresh the page just update results (entries)
So, first I have a table with some data which is already saved into links table on my database
So here is my codes:
1st links.php:
<div id="links_list">
<table border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Link</td>
<td>Posted By</td>
<td></td>
</tr>
</thead>
<tbody>
<?php
$rows = $db->query("SELECT * FROM `links` ");
$totalLinks = $rows->num_rows;
if($totalLinks == 0){
echo "<tr><td colspan='5'><div class='' style='font-size: 16px;color: #F44336;background: #fff;width: fit-content;margin-left: 4px;'><i class='fa fa-meh-o'></i> <b>Error :</b> No LINK Has Been Added Yet.</div></td></tr>";
} else {
while ($row = mysqli_fetch_assoc($rows)) {
?>
<tr>
<td id="links_row" valign="middle">
<input type="checkbox" id="selectr-link-<?php echo $row['link_id']; ?>" name="select_link[]" value="<?php echo $row['link_id']; ?>" style="float:left;margin-top:3px;">
<span style="float:left;width:25px;height:20px;margin: 0 3px;text-align:center;font-size: 20px;line-height: 22px;color: #009688;"><?php if($row['link_type'] == "Iframe"){ echo "<i class='fa fa-tv'></i>"; } elseif($row['link_type'] == "Direct"){ echo "<i class='fa fa-globe'></i>"; } ?></span>
<span class="spanInMobile" style="float:left;height:20px;margin-right:5px;"><?php echo $row['link_url']; ?></span>
</td>
<td valign="middle">
<?php echo $row['posted_by']; ?>
</td>
<td valign="middle" class="text-right">
<?php $id = $row['link_id']; $status = $row['link_status']; if($status == '0'){ ?>
<span title="Enable" class="disabled-entry" id="enable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } elseif($status == '1'){ ?>
<span title="Disable" class="enabled-entry" id="disable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } ?>
<script>
$(document).ready(function(){
$('#enable_link').CLICK(function(){
var link_id = $('#enable_link').attr('data-id');
$.ajax({
url: "../src/ajax/enable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
});
</script>
<script>
$(document).ready(function(){
$('#disable_link').click(function(){
var link_id = $('#disable_link').attr('data-id');
$.ajax({
url: "../src/ajax/disable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
</script>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
</div>
** enable.php **
<?php
require_once ('../system/config.php');
$link_id = $_POST['link_id'];
$db->query("UPDATE `links` SET `link_status` = '1' WHERE `link_id` = '".$link_id."'");
?>
<table border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<td>Link</td>
<td>Posted By</td>
<td></td>
</tr>
</thead>
<tbody>
<?php
$rows = $db->query("SELECT * FROM `links` ");
$totalLinks = $rows->num_rows;
if($totalLinks == 0){
echo "<tr><td colspan='5'><div class='' style='font-size: 16px;color: #F44336;background: #fff;width: fit-content;margin-left: 4px;'><i class='fa fa-meh-o'></i> <b>Error :</b> No LINK Has Been Added Yet.</div></td></tr>";
} else {
while ($row = mysqli_fetch_assoc($rows)) {
?>
<tr>
<td id="links_row" valign="middle">
<input type="checkbox" id="selectr-link-<?php echo $row['link_id']; ?>" name="select_link[]" value="<?php echo $row['link_id']; ?>" style="float:left;margin-top:3px;">
<span style="float:left;width:25px;height:20px;margin: 0 3px;text-align:center;font-size: 20px;line-height: 22px;color: #009688;"><?php if($row['link_type'] == "Iframe"){ echo "<i class='fa fa-tv'></i>"; } elseif($row['link_type'] == "Direct"){ echo "<i class='fa fa-globe'></i>"; } ?></span>
<span class="spanInMobile" style="float:left;height:20px;margin-right:5px;"><?php echo $row['link_url']; ?></span>
</td>
<td valign="middle">
<?php echo $row['posted_by']; ?>
</td>
<td valign="middle" class="text-right">
<?php $id = $row['link_id']; $status = $row['link_status']; if($status == '0'){ ?>
<span title="Enable" class="disabled-entry" id="enable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } elseif($status == '1'){ ?>
<span title="Disable" class="enabled-entry" id="disable_link[]" data-id="<?php echo $row['link_id']; ?>"></span>
<?php } ?>
<script>
$(document).ready(function(){
$('#enable_link').click(function(){
var link_id = $('#enable_link').attr('data-id');
$.ajax({
url: "../src/ajax/enable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
</script>
<script>
$(document).ready(function(){
$('#disable_link').click(function(){
var link_id = $('#disable_link').attr('data-id');
$.ajax({
url: "../src/ajax/disable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
</script>
</td>
</tr>
<?php } } ?>
</tbody>
</table>
It works only if the first row was been enabled and then the second one... ect but if you want to enable the 2nd row for example it won't work until you enable the first one
I tried with each() function but I am not sure that the code was right written
you can't use same ID for pointing multiple elements in DOM, instead use CLASS.
so change your click event binding to a class instead of ID
Example
$(document).ready(function(){
$('.enabled-entry').click(function(){
var link_id = $(this).attr('data-id');
$.ajax({
url: "../src/ajax/enable_link.php",
method: "post",
data: {link_id:link_id},
success: function(data){
$('#links_list').html(data);
}
});
});
});
Also add this script after while loop
Hey guys can you guys help me with this checkbox problem? If I check the column and press update, I want the update to happen at current time.
this is an example
I want if I check the checkbox and press the update button and the column will update at the current time
This is my view :
<div class="container">
<div class="row">
<div class="col-md-10">
<div class="panel panel-info">
<div class="panel-heading">Setting Jam & Tanggal Mesin</div>
<div class="panel-body">
<table id="devid_auto" class="table table-bordered dt-responsive" cellspacing="0" width="100%">
<thead>
<tr>
<th width="1%" align="center"><input type="checkbox" onClick="toggle(this)" id="checkAll" name="checkAll" /></th>
<th width="10%" align="center">Nama Mesin</th>
<th width="5%" align="center">IP Address</th>
<th width="10%" align="center">SN</th>
<th width="5%" align="center">Ethernet Port</th>
<th width="10%" align="center">Last Update</th>
</tr>
</thead>
<tbody>
<?php
foreach($data as $d){
?>
<tr>
<td><input class="childbox" type="checkbox" name="msg[]" id="id" value="<?php echo $d['devid_auto'] ?>" /></td>
<td class="data-check" ><?php echo $d['device_name']; ?></td>
<td class="data-check" ><?php echo $d['ip_address']; ?></td>
<td class="data-check" ><?php echo $d['sn']; ?></td>
<td class="data-check" ><?php echo $d['ethernet_port']; ?></td>
<td class="data-check" ><?php echo $d['lastupdate_date']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<div class="form-group">
<label for="colFormLabelLg" class="col-sm-3 col-form-label col-form-label-lg">Tanggal & Waktu Sekarang</label>
<div class="col-sm-3">
<input type="" class="form-control" id="" value="<?php date_default_timezone_set("Asia/Jakarta"); echo date(" Y-m-d H:i:s ");?>"" readonly>
</div>
</div>
</div>
<div class="panel-footer text-right">
<button allign="center" class="btn btn-danger" onclick="update()">Update Date and time</button>
</div>
</div>
<script src="<?php echo base_url() ?>assets/baru/jquery-3.2.1.min.js"></script>
<script src="<?php echo base_url() ?>assets/baru/jquery.dataTables.min.js"></script>
<script src="<?php echo base_url() ?>assets/baru/dataTables.bootstrap4.min.js"></script>
<script type="text/javascript">
$(document).ready( function ()
{
$('#devid_auto').DataTable();
$("input[name='checkAll']").click(function() {
var checked = $(this).attr("checked");
$("#devid_auto tr td input:checkbox").attr("checked", checked); });
});
function toggle(id)
{
checkboxes = document.getElementsByName('msg[]');
for(var i=0, n=checkboxes.length;i<n;i++) {
checkboxes[i].checked = id.checked;
}
}
function update(id)
{
var list_id = [];
$("#id:checked").each(function() {
list_id.push(parseInt(this.value));
});
console.info(JSON.stringify(list_id));
if(list_id.length > 0)
{
if(confirm('Are you sure update this '+list_id.length+' data?'))
{
$.ajax({
type: 'POST',
data: {'devid_auto': list_id},
url: '<?php echo site_url('setting/mesin_update')?>',
success: function(result)
{
var hasil = result.replace(/\s/g,'');
if(hasil == 'y')
{
alert("Data Berhasil di Update");
location.reload();
}
else
{
alert('Failed.');
}
},
error: function (jqXHR, textStatus, errorThrown)
{
alert('Error update data');
}
});
}
}
else
{
alert('no data selected');
}
}
</script>
</div><!-- end cols-->
</div><!--end row-->
</div> <!-- end container-->
and this is my controller :
public function mesin_update()
{
$initiated_date = date('Y-m-d H:i:s');
$data = array(
'lastupdate_date' => $initiated_date,
);
$devids = $this->input->post('devid_auto', True);
foreach($devids as $devid){
$this->Jam_tgl_model->jam_tanggal_update(array('devid_auto' => $devid ), $data);
}
echo 'y';
}
and this is my model :
public function jam_tanggal_update($where, $data)
{
$this->db->update($this->table, $data, $where);
return $this->db->affected_rows();
}
I have tried it with my code but nothing has changed on column last update date
please help fix my code so i can finish it
( NEW EDIT) :
- this is the result:
- I checked the line
- and pressing the update button, can you change the script function update (id)?
- and this is the result after I hit update button
in contorller
$devids = $this->input->post('devid_auto', True);
foreach($devids as $devid){
$this->Jam_tgl_model->jam_tanggal_update(array('devid_auto' => $devid), $data);
}
NOTE: also you can use ci $this->db->update_batch()
Try this in your model
public function jam_tanggal_update($where, $data) {
$this->db->where($where);
$this->db->update($this->table_dept, $data);
return $this->db->affected_rows();
}
For More details on Updating: See CodeIgniter official documentation
Try This.. This May Help you .
Controller
public function delete_inbox()
{
$ids = ( explode( ',', $this->input->get_post('devid_auto') ));
$initiated_date = date('Y-m-d H:i:s');
$data = array(
'lastupdate_date' => $initiated_date,
);
$del= $this->model-mesin_update($ids,$data);
}
Model:
public function jam_tanggal_update($where, $data){
$ids = $ids;
$count = 0;
foreach ($ids as $id){
$did = intval($id);
$this->db->where('id', $did);
$res=$this->db->update('<table_name',$data);
$count = $count+1;
}
}
I have a list of users in my table. At first run it will load using the data from my controller. And I have a remove button function in every row. And then in my remove button I have an ajax load AFTER the successful removal of the user. Now I load the data using ajax. But the problem is the same functionality I have at first wont work anymore. I don't know why.
Here's my code:
Initial load of user from my controller
<table class="table table-bordered table-striped table-hover" id="user-group-list">
<thead>
<tr>
<th></th>
<th>Group Name</th>
<th>Description</th>
<th class="text-center">
<span class="fa fa-plus"></span>
<button class="btn ink-reaction btn-raised btn-danger btn-sm"><span class="fa fa-trash-o"></span></button>
</th>
</tr>
</thead>
<tbody>
<?php if($user_groups) { ?>
<?php foreach($user_groups as $g) { ?>
<tr>
<td class="text-center">
<input type="checkbox" name="group_id[]" value="<?php echo $g['id']; ?>" />
</td>
<td>
<label><?php echo $g['name']; ?></label>
</td>
<td>
<label><?php echo $g['definition']; ?></label>
</td>
<td class="text-center">
<a class="btn btn-icon-toggle btn-primary edit_group" data-id="<?php echo $g['id']; ?>"><i class="fa fa-pencil"></i></a>
<?php if($g['id'] > 2) { ?>
<a class="btn btn-icon-toggle btn-danger remove_group" data-id="<?php echo $g['id']; ?>" data-name="<?php echo $g['name']; ?>"><i class="fa fa-trash-o"></i></a>
<?php } ?>
</td>
</tr>
<?php } ?>
<?php } else { ?>
<tr>
<td colspan="4" class="text-center">
<p>There are no user group set</p>
</td>
</tr>
<?php } ?>
</tbody>
</table>
Then in my JS
$('.remove_group').on('click', function(e) {
e.preventDefault();
var group_id = $(this).data('id');
var name = $(this).data('name');
alert(group_id); //wont work anymore after ajax load
bootbox.dialog({
message: "Are you sure you want to remove the group <span class='text-danger'>" + name.toUpperCase() + "</span>",
title: "Notification",
buttons: {
success: {
label: "Yes, remove it",
className: "btn-info",
callback: function() {
$.ajax({
url: "<?php echo site_url('users/user/remove_user_group'); ?>",
data: {group_id: group_id},
dataType: 'json',
type: 'post',
beforeSend: function() {
console.log('Loading...');
},
success: function(d) {
loadUserGroup();
makeToast(d.status, d.toast_info);
},
error: function() {
alert('Error Found!');
}
});
}
},
danger: {
label: "Cancel",
className: "btn-default",
callback: function() {
$(this).modal('hide');
}
}
}
});
});
function loadUserGroup() {
$.ajax({
url: "<?php echo site_url('users/user/load_group_list'); ?>",
dataType: 'json',
beforeSend: function() {
var loader = "<tr>";
loader += " <td colspan='4' class='text-center'><span class='fa fa-spinner fa-pulse fa-3x'></span></td>";
loader += "</tr>";
$('#user-group-list tbody').empty();
$('#user-group-list tbody').html(loader);
},
success: function(data) {
$('#user-group-list tbody').empty();
var group_list = '';
$.each(data.user_groups, function(k, v){
group_list += "<tr>";
group_list += " <td class='text-center'><input type='checkbox' /></td>";
group_list += " <td><label>" + v.name + "</label></td>";
group_list += " <td><label>" + v.definition + "</label></td>";
group_list += " <td class='text-center'>";
group_list += " <a class='btn btn-icon-toggle btn-primary edit_group' data-id='" + v.id + "'><span class='fa fa-pencil'></span></a>";
if(v.id > 2) {
group_list += " <a class='btn btn-icon-toggle btn-danger remove_group' data-id='" + v.id + "' data-name='" + v.name + "'><span class='fa fa-trash-o'></span></a>";
}
group_list += " </td>";
group_list += "</tr>";
//console.log(v.id);
});
$('#user-group-list tbody').html(group_list);
},
error: function() {
alert('Error Occured!');
}
});
}
My ajax load
public function load_group_list() {
$json['user_groups'] = array();
$groups = $this->aauth->get_all_groups();
if(count($groups) > 0) {
foreach($groups as $group) {
$json['user_groups'][] = array(
'id' => $group['id'],
'name' => $group['name'],
'definition' => $group['definition'],
'href' => site_url('users/user/user_group_info?group_id=' . $group['id'])
);
}
}
header('Access-Control-Allow-Origin: *');
header("Content-Type: application/json");
echo json_encode($json);
}
Hope the below function fails
$('.remove_group').on('click', function(e) {//your code}
Change that to the below code
$(document).on('click','.remove_group', function(e) {//your code}
It occur because you load the class dynamically and DOM can't identify the element any more
this is my edit_table.php in which dynamic table is generated and by on click function i try to update database.
<script>
function table_edit(btn)
{
var id=btn.id;
if(btn.value=="Edit")
{
document.getElementById('college_id'+id).setAttribute("contenteditable" , "true");
document.getElementById('name'+id).setAttribute("contenteditable" , "true");
//document.getElementById('university_id'+id).removeAttribute("Readonly");
document.getElementById('university_id'+id).setAttribute("contenteditable" , "true");
document.getElementById(id).value="Save";
return false;
}
if(btn.value=="Save")
{
document.getElementById('name'+id).removeAttribute("contenteditable");
document.getElementById('college_id'+id).removeAttribute("contenteditable");
// document.getElementById('university_id'+id).setAttribute("Readonly" , "readonly");
document.getElementById('university_id'+id).removeAttribute("contenteditable");
document.getElementById(id).value="Edit";
var newuniversity_id=document.getElementById('university_id'+id).innerHTML;
var newcollege_id=document.getElementById('college_id'+id).innerHTML;
var newname=document.getElementById('name'+id).innerHTML;
alert(newname+" "+newcollege_id+" "+newuniversity_id+" "+id);
var dataString = 'name1=' + name + '&university_id=' + newuniversity_id + '&college_id=' + newcollege_id + '&id=' + id;
$.ajax({
type: "POST",
url: "update_table_data",
data: dataString,
success: function(html) {
alert("ajax calling done");
}
});
return true;
}
}
function table_update(){
//document.getElementById("edit_rows").value="Save"
alert("Working!");
}
</script>
<?php
function test()
{
echo "<script>alert('hello');</script>";
} ?>
<div id="page-content-wrapper">
<div class="container-fluid">
<i class="fa fa-bars"></i> <span>Menu</span>
<div class="content-block">
<div class="login-sign forgot_pass login forgot text-center">
<!-- <form action="<?php echo base_url(); ?>edit_table" method="post" accept-charset="utf-8"> -->
<div class="login-signup-head">Edit Table</div>
<table>
<tbody>
<thead>
<tr class="tredit">
<!-- <th width="10"></th> -->
<th> Id </th>
<th > University Id </th>
<th > Name </th>
<th > College Id </th>
<th width="100"> </th>
</tr>
</thead>
<tbody>
<?php
foreach($student_data as $row){
?>
<tr class="tredit" id="row_edit">
<td><?php echo $row['id']; ?></td>
<td id="university_id<?php echo $row['id']; ?>" ><?php echo $row['university_id']; ?></td>
<td id="name<?php echo $row['id']; ?>" ><?php echo $row['name']; ?></td>
<td id="college_id<?php echo $row['id']; ?>" > <?php echo $row['college_id']; ?></td>
<td><input type='button' class='editable' onclick=" return table_edit(this)" value='Edit' id= "<?php echo $row['id']; ?>">
<input type='button' class='tabledelete' onclick="table_update()" value='Delete' ></td>
</tr>
<?php
}
?>
</div>
</div>
</div>
<!-- </form> -->
</div>
</div>
</tbody>
</table>
and this is the route
$route['update_table_data'] = 'home/update_table';
and the following functions are for fetching the data as well as updating the data.
public function tableedit(){
if($this->session->userdata('university_id')){
$user_id = $this->session->userdata('university_id');
$data['user_id'] = $user_id;
$data['student_data'] = $this->home_model->table_data($user_id);
$this->load->view('home/new-header',$data);
$this->load->view('home/left_university_sidebar',$data);
$this->load->view('edit_table',$data);
$this->load->view('home/footer');
} else {
$data['error'] = 'Table Cannot be viewed!';
$this->load->view('home/header');
$this->load->view('edit_table');
$this->load->view('home/footer');
}
}
public function update_table() {
if($this->session->userdata('university_id')){
if($this->input->server("REQUEST_METHOD") === "POST"){
$university_id=$_POST['university_id'];
$college_id=$_POST['college_id'];
$id=$_POST['id'];
$name=$_POST['name'];
$this->home_model->update_table($university_id,$college_id,$name,$id);
}
}
}
and finally this is the model for update query.
public function update_table($university_id,$college_id,$name,$id) {
$data = array('University Id' => $university_id, 'name' => $name, 'College Id' => $college_id);
$this->db->where('id', $id);
$this->db->update('college', $data);
}
editable table
edit values
but when i save and refresh the page, no change occur in the table.
my issue is database is not updated when i update it from save button.
first of all :
Formatting your code , that must be clean and easily readable
for your ajax call you dont need use routed url , it work at background, you can use like this :
put : <script>var base_url='<?php echo base_url(); ?>'</script>
in your header menu , or upper place that you use in your theme.
then make ajax call like this:
$.ajax({
type: "POST",
url: base_url+"youController/yourAction",
data: dataString,
success: function(html) {
alert("ajax calling done");
}
});
I'm using keypress via this OOP function below to update my sites shopping cart and quantity value. Sometimes it works, but most of the time when I press the enter key it doesn't a) update the cart nor b) update the qty value. I think it's refreshing the page, which could be the issue.
I took a look at the request header value by inspecting the qty element and that doesn't update when enter key pressed, if that helps.
function initBinds() {
if ($('.remove_basket').length > 0) {
$('.remove_basket').bind('click', removeFromBasket);
}
if ($('.update_basket').length > 0) {
$('.update_basket').bind('click', updateBasket);
}
if ($('.fld_qty').length > 0) {
$('.fld_qty').bind('keypress', function(e) {
var code = e.keyCode ? e.keyCode : e.which;
if (code == 13) {
updateBasket();
}
});
}
}
And here's the updateBasket function
function updateBasket() {
$('#frm_basket :input').each(function() {
var sid = $(this).attr('id').split('-');
var val = $(this).val();
$.ajax({
type: 'POST',
url: '/mod/basket_qty.php',
data: ({ id: sid[1], qty: val }),
success: function() {
refreshSmallBasket();
refreshBigBasket();
},
error: function() {
alert('An error has occurred');
}
});
});
}
And, this is the page...
Note, you can see on line 41 the class of fld_qty is used for the initiBinds keypress function if statement.
<?php
$session = Session::getSession('basket');
$objBasket = new Basket();
$out = array();
if (!empty($session)) {
$objCatalogue = new Catalogue();
foreach ($session as $key => $value) {
$out[$key] = $objCatalogue->getProduct($key);
}
}
require_once('_header.php'); ?>
<div id="cat_prod"><h1>- BASKET -</h1></div>
<?php
if (!empty($out)) { ?>
<div id="big_basket">
<form action="" method="post" id="frm_basket">
<table cellpadding="0" cellspacing="0" border="0" class="tbl_repeat">
<tbody id="basket_table">
<tr style="background-color: #f2f3ee;">
<th class="ta_left">Item</th>
<th class="ta_r">Qty</th>
<th class="ta_r col_15">Price</th>
<th class="ta_r col_15"></th>
</tr>
<?php foreach ($out as $item) { ?>
<tr>
<td class="ta_left_name"><?php echo Helper::encodeHTML($item['name']); ?></td>
<td class="ta_left_qty"><input type="text" name="qty-<?php echo $item['id']; ?>"
id="qty-<?php echo $item['id']; ?>" class="fld_qty"
value="<?php echo $session[$item['id']]['qty']; ?>" /></td>
<td class="ta_r">£<?php echo number_format($objBasket->itemTotal($item['price'], $session[$item['id']]['qty']), 2); ?></td>
<td class="ta_r"> <?php echo Basket::removeButton($item['id']); ?></td>
</tr>
<?php } ?>
<?php if ($objBasket->_vat_rate != 0) { ?>
<tr style="border-bottom: dashed 1px #aaa">
<td class="ta_left" colspan="2">Sub-total :</td>
<td class="ta_r bt_td">£<?php echo number_format($objBasket->_sub_total, 2); ?></td>
<td class="ta_r bt_td"> </td>
</tr>
<tr style="border-bottom: dashed 1px #aaa">
<td class="ta_left" colspan="2">VAT (<?php $objBasket->_vat_rate; ?>%) :</td>
<td class="ta_r bt_td">£<?php echo number_format($objBasket->_vat, 2); ?></td>
<td class="ta_r bt_td"> </td>
</tr>
<?php } ?>
<tr>
<td class="ta_right" colspan="2"><strong>Total :</strong></td>
<td class="ta_r bt_td">£<?php echo number_format($objBasket->_total, 2); ?></td>
<td class="ta_r bt_td"> </td>
</tr>
</tbody>
</table>
<div class="dev br_td"> </div>
<div class="dev br_td"> </div>
<div class="sbm sbm_blue fl_r">
Checkout
</div>
<div class="sbm sbm_blue fl_l update_basket">
<span class="btn">Update</span>
</div>
</form>
</div>
<?php } else { ?>
<br />
<br />
<p><em>Your basket is currently empty.</em></p>
<?php } ?>
<?php require_once('_footer.php'); ?>
I have looked through some statckflow pages regarding this and have tried keydown and just using e.which and e.keyCode || e.which instead, but they all render the same issue of not working 100% of the time when you press enter key.
I understand some browsers may not support this, so is there a better approach for this operation? I have tested Firefox, Chrome and Safari (all latest).
Thanks for the help, appreciated! :)
Edit;
Here's the mod/basket_qty.php also...
<?php
require_once('../inc/autoload.php');
if (isset($_POST['qty']) && isset($_POST['id'])) {
$out = array();
$id = $_POST['id'];
$val = $_POST['qty'];
$objCatalogue = new Catalogue();
$product = $objCatalogue->getProduct($id);
if (!empty($product)) {
switch($val) {
case 0:
Session::removeItem($id);
break;
default:
Session::setItem($id, $val);
}
}
}
Looks like I needed to add e.preventDefault(); in the initBinds function for if (code == 13) { as pointed out by cmorrissey. It seems to be working fine with this. Thanks!!