Post selected items to table - php

I have a results list produced by a search. Each item has a check box. What I want to do is post the selected items to a different table.
Here is the code that displays the data:
<section class="grid_12">
<div class="block-border">
<form class="block-content form" id="table_form" method="post" action="">
<h1>Sortable table</h1>
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
<thead>
<tr>
<th class="black-cell"><span class="loading"></span></th>
<th scope="col">
<span class="column-sort">
</span>
Name
</th>
<th scope="col">Client Of</th>
<th scope="col">
<span class="column-sort">
</span>
Home Phone
</th>
<th scope="col">
<span class="column-sort">
</span>
Email
</th>
<th scope="col">
<span class="column-sort">
</span>
Created On
</th>
<th scope="col" class="table-actions">Actions</th>
</tr>
</thead>
<tbody>
<?php foreach($r->result() as $row) : ?>
<tr data-clientid="$row->id">
<td class="th table-check-cell">
<input type="checkbox" name="selected[]" class="checkbox">
</td>
<td><?=$row->firstname?> <?=$row->lastname?></td>
<td><ul class="keywords">
<li><?=$row->clientof?></li>
<li class="orange-keyword"><?=$row->status?></li>
</ul></td>
<td><?=$row->homephone?></td>
<td><?=$row->email?></td>
<td><?=$row->created?></td>
<td class="table-actions">
<img src="/images/icons/fugue/pencil.png" width="16" height="16">
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
<div class="clear"></div>
<div class="block-footer">
<img src="images/icons/fugue/arrow-curve-000-left.png" width="16" height="16" class="picto">
Select All
Unselect All
<span class="sep"></span>
<button id="post_diff_table">POST selected</button>
</div>
</form></div>
</section>
Here is all the current javascript in the above page.
<? $campaignid = $this->uri->segment(4); ?>
<script type="text/javascript">
<!--
$(document).ready(function(){
toggleChecked = function(status)
{
alert('trigger');
$(".checkbox").each( function() {
$(this).attr("checked",status);
});
}
});
//-->
</script>
<script type="text/javascript">
$(function() {
$('#post_diff_table').click(function() {
var table = $('table#mylist');
var campagnid = table.data('campaignId');
//Array of checked client ids
var checked_clients = $.map(table.find('tr td.table-check-cell input:checked'), function(e) {
return $(e).parents('tr').data('clientid');
});
if (checked_clients.length === 0) {return false;}
//call the controller
$.post('/ajaxmodals/campaign_post_to_other_table', {
campagnid: campagnid,
clients: checked_clients
}, function(data) {
//process response
},"json");
});​});
</script>
Here is the code in ajax controller.
function campaign_post_to_other_table()
{
$campagnid=$this->input->post('campagnid');
/* #var Array */
$clients = $this->input->post('clients');
foreach ($clients as $client_id) {
$clientid = $this->input->post('clientid');
$submit['clientid'] = $clientid;
$submit['campaignid'] = $this->input->post('campaignid');
$submit['creationdate'] = date('Y-m-d H:i:s');
$submit['createdby'] = $this->session->userdata('uid');
//print_r($submit); exit();
$newid = $this->campaign_model->add_user_to_campaign($submit);
$cname = $this->client_model->get_client_name($clientid);
echo json_encode(array("response"=>"Success","name"=>$cname,"id"=>$newid,"clientid"=>$clientid));
}
}
Here is the add_user_to_campaign model.
function add_user_to_campaign($data)
{
$this->db->insert('campaign_to_client',$data);
$newid = $this->db->insert_id();
return $newid;
}

In table definition (I assume that $campaignid is campaignid, change mylist to whatever you want):
<table id="mylist" data-campaignId="<?=$campaignid?>" class="table sortable no-margin" cellspacing="0" width="100%">
In your loop set data for client
<?php foreach($r->result() as $row) : ?>
<tr data-clientid="$row->id">
<td class="th table-check-cell">
<input type="checkbox" name="selected[]" class="checkbox">
</td>
<!---other td here -->
</tr>
<?php endforeach;?>
</table>
<!--button to post all selected items -->
<button id="post_diff_table">POST selected</button>
javascript:
$(function() {
$('#post_diff_table').click(function() {
    var table = $('table#mylist');
    var campagnid = table.data('campaignId');
//Array of checked client ids
    var checked_clients = $.map(table.find('tr td.table-check-cell input:checked'), function(e) {
        return $(e).parents('tr').data('clientid');
    });
if (checked_clients.length === 0) {return false;}
//call the controller
    $.post('/ajaxmodals/campaign_post_to_other_table', {
        campagnid: campagnid,
        clients: checked_clients
    }, function(data) {
        //process response
    },"json");
});​});
in controller:
function campaign_post_to_other_table()
{
$campagnid=$this->input->post('campagnid');
/* #var Array */
$clients = $this->input->post('clients');
foreach ($clients as $client_id) {
/* sql insertion code here
or call $this->campaign_model->add_user_to_campaign for each clientid
*/
}
}
The answer is not the copy-pastable code, as i have no idea of your project internals, but you should get the idea.
the suggestions on other code: do not do this in js:
$.post('/ajaxmodals/campaign_subscriber_add', {
clientid : cid,
campaignid : <?=$r->id?>
get the campaignid from table data as in my sample
do not do this:
$("#subscriberList").append('<li id="'+data.id+'">'+data.name+'<span style="display: inline; float: right" onclick="javascript:delete_subscriber(\''+data.id+'\')">delete</span></li>');
compose element in js, add event handlers, etc. your way make you to use to much global functions and make the code unreadable.

Related

search on table,real time

I have the below table:
<input type="search" class="light-table-filter" id="search" data-
table="order-table" placeholder="Kerko">
<table border="0" class=" table table-striped table-hover table-bordered table-condensed tableDemo bordered order-table table" >
<tr id="header">
<th>ID</th>
<th>emri</th>
<th>mbiemri</th>
<th>username</th>
<th>password</th>
<th>email</th>
<th>Nr Tel</th>
<th>ACTION</th>
</tr>
<?php
if(count($records)){
$i = 1;
$eachRecord= 0;
foreach($records as $key=>$eachRecord){
?>
<tr id="<?=$eachRecord['ID'];?>">
<td><?=$eachRecord['ID'];?></td>
<td class="Emri"><?=$eachRecord['Emri'];?></td>
<td class="Mbiemri"><?=$eachRecord['Mbiemri'];?></td>
<td class="Username"><?=$eachRecord['Username'];?></td>
<td class="Password"><?=$eachRecord['Password'];?></td>
<td class="Email"><?=$eachRecord['Email'];?></td>
<td class="nrtel"><?=$eachRecord['nrtel'];?></td>
<td>
<img src="" class="eimage"> <span class="glyphicon glyphicon-pencil"></span>
<img src="" class="dimage"> <span class="glyphicon glyphicon-trash"></span>
</td>
</tr>
<?php }
}
?>
</table>
Also,i want to make a real time search and i have the below jquery code:
<script type="text/javascript">
(function(document) {
'use strict';
var LightTableFilter = (function(Arr) {
var _input;
function _onInputEvent(e) {
_input = e.target;
var tables = document.getElementsByClassName(_input.getAttribute('data-table'));
Arr.forEach.call(tables, function(table) {
Arr.forEach.call(table.tBodies, function(tbody) {
Arr.forEach.call(tbody.rows, _filter);
});
});
}
function _filter(row) {
var text = row.textContent.toLowerCase(), val = _input.value.toLowerCase();
row.style.display = text.indexOf(val) === -1 ? 'none' : 'table-row';
}
return {
init: function() {
var inputs = document.getElementsByClassName('light-table-filter');
Arr.forEach.call(inputs, function(input) {
input.oninput = _onInputEvent;
});
}
};
})(Array.prototype);
document.addEventListener('readystatechange', function() {
if (document.readyState === 'complete') {
LightTableFilter.init();
}
});
})(document);
Everything it works perfectly.But I want to search all rows except the header of table that has id #header.I can't change the jquery code.Also I have tried some different jquery but only the above jquery code works perfect for me. The only issue is that the code when I write something,it find the row but the header hidded.

how can set multiple submit buttons for uploading

i want 14 records to be uploaded. Before uploading i want to select corresponding check button. At a time only one record can be uploaded. So all 14 records are arranged in a table with two buttons, one for choose file and other for submit. But problem is that only very first document is uploaded, others don't get uploaded. later i knew that i must use more than one form for all submission. how can it possible?
<div class="portal-body" style="min-height: 268px;">
<table class="table striped hover bordered" id="sample_editable_4">
<thead>
<tr>
<th id="eval_style">Sl No.</th>
<th id="eval_style">Document Name</th>
<th id="eval_style">select</th>
<th id="eval_style">Choose</th>
<th id="eval_style">Submit</th>
</tr>
</thead>
<tbody>
<?php $i=1;
foreach($evidence_details->result() as $row_evidence) { ?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $row_evidence->evidence_name; ?> </td>
<td>
<input type="checkbox" name="evidences" id="<?php echo $row_evidence->evidence_id; ?>" value="<?php echo $row_evidence->evidence_id; ?>" <?php if(in_array($row_evidence->evidence_id,$evidence)) { ?>checked="checked"<?php } ?> />
</td>
<td><input type="file" name="multiUpload" id="multiUpload" multiple /></td>
<td><button type="submit" class="btn blue" name="submitHandler" id="submitHandler" style="margin-top: 15px;margin-left: 54px;">Submit</button></td>
</tr>
<?php $i++; } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
var config = {
support : "image/jpg,image/png,image/bmp,image/jpeg,image/gif,application/vnd.ms-excel,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,text/csv,application/pdf",
form: "demoFiler", // Form ID
dragArea: "dragAndDropFiles",
uploadUrl: "<?php echo base_url(); ?>home/multiple_upload"
}
$(document).ready(function()
{
initMultiUploader(config);
});
</script>
controller
function multiple_upload()
{
$application_id=$this->session->userdata('application_id');
if(!is_dir('./application/assets/uploads/'.$application_id))
{
mkdir('./application/assets/uploads/'.$application_id, 0777, TRUE);
}
if($_SERVER['REQUEST_METHOD'] == "POST"){
if(move_uploaded_file($_FILES['file']['tmp_name'], "./application/assets/uploads/".$application_id."/".$_FILES['file']['name'])){
echo($_POST['index']);
}
exit;
}
}
**multiupload.js**
function initMultiUploader(){
new multiUploader(config);
}

Unable to make the html of the data in the controller and show on the dropdown select

i want to show the restaurant after selecting its area from dropdown list. but my code did not show the restaurant name and the menu button of that restaurant please tell me where i did mistake
This is my view code
<div id="restaurant">
</div>
This is controller code
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array('city_id'=>$cit_id,'city_area_id'=>$area);
$html = '
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<?php $i=1;foreach($result as $row){
?>
<tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
<th style="">
<?php echo $i++; ?>
</th>
<th style="">
<?php echo $row->restaurant_name; ?>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
';
echo $html;
This is my model code view
function select_record($table, $where = NULL){
$this->db->select();
if($where)
$this->db->where($where);
$this->db->from($table);
$query = $this->db->get();
// echo $this->db->last_query();
return $query->result();
}
Script code
function get_rests(){
var city_id = $('#city_id').val();
var area_id = $("#area_id").val();
$.ajax({
type: "POST",
url: "<?=base_url();?>index.php/Bulk_Controller/get_rests",
data: {cit_id: city_id,areaID: area_id},
dataType: "text",
cache:false,
success:
function(data){
// alert(data);
$('#restaurant').html(data);
}
});
}
You didn't called a model function to get data from your DB Table.
public function get_rests()
{
$cit_id = $this->input->post('cit_id');
$area = $this->input->post('areaID');
$where = array('city_id'=>$cit_id,'city_area_id'=>$area);
$result= $this->your_model->select_record('table_name',$where);
$html = '
<div class="container" id="">
<table align="centre" class="table table-condensed table-striped table-hover no-margin"style="width:70%" id="">
<thread>
<tr style="width: 56%;">
<th>
No.
</th>
<th style="">
Restaurant Names
</th>
</tr>
</thread>
<tbody>
<?php $i=1;foreach($result as $row){
?>
<tr id="<?php echo $row->restaurant_id; ?>" class="res_id">
<th style="">
<?php echo $i++; ?>
</th>
<th style="">
<?php echo $row->restaurant_name; ?>
</th>
<th style="width: 1%" >
</i>See Menu
</th>
</tr>
<?php } ?>
</tbody>
</table>
</div>
';
echo $html;

jquery updation after ajax search updation

i have a script which works for deletion confirmation...
$('document').ready(function(){
$('.ask').jConfirmAction();
});
and the table of products listing is under below
<?php if(isset($query)){ ?>
<input disabled type="hidden" id="recNum" value="<?php echo $rec ?>"/>
<table id="rounded-corner" summary="2007 Major IT Companies' Profit" width="100%">
<thead>
<tr>
<th scope="col" class="rounded-company"></th>
<th scope="col" class="rounded">Product ID</th>
<th scope="col" class="rounded">Product Name</th>
<th scope="col" class="rounded">Product slug</th>
<th scope="col" class="rounded">Type</th>
<th scope="col" class="rounded">Product Category</th>
<th scope="col" class="rounded">Edit</th>
<th scope="col" class="rounded-q4">Delete</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="7" class="rounded-foot-left"><em>All The Pages That Being Included</em></td>
<td class="rounded-foot-right"> </td>
</tr>
</tfoot>
<tbody>
<?php
foreach($query as $key => $row) {
?>
<tr>
<td><input type="checkbox" name="" /></td>
<td><?php echo $row['product_id'];?></td>
<td><?php echo $row['product_name'];?></td>
<td><?php echo $row['product_slug'];?></td>
<td><?php echo $row['type'];?></td>
<td><img src="<?php echo base_url().$row['product_image'];?>" width="80px" height="80px"/></td>
<td><img src="<?php echo base_url();?>assets/admin_assets/images/user_edit.png" alt="" title="" border="0" /></td>
<td><img src="<?php echo base_url();?>assets/admin_assets/images/trash.png" alt="" title="" border="0" /></td>
</tr>
<?php
}?>
</tbody>
</table>
<span class="bt_green_lft"></span><strong>Add new item</strong><span class="bt_green_r"></span>
<span class="bt_blue_lft"></span><strong>View all items from category</strong><span class="bt_blue_r"></span>
<span class="bt_red_lft"></span><strong>Delete items</strong><span class="bt_red_r"></span>
heres the jquery for the jQuery confirmation:
/*
* jQuery Plugin : jConfirmAction
*
* by Hidayat Sagita
* http://www.webstuffshare.com
* Licensed Under GPL version 2 license.
*
*/
(function($){
jQuery.fn.jConfirmAction = function (options) {
// Some jConfirmAction options (limited to customize language) :
// question : a text for your question.
// yesAnswer : a text for Yes answer.
// cancelAnswer : a text for Cancel/No answer.
var theOptions = jQuery.extend ({
question: "Are You Sure ?",
yesAnswer: "Yes",
cancelAnswer: "Cancel"
}, options);
return this.each (function () {
$(this).bind('click', function(e) {
e.preventDefault();
thisHref = $(this).attr('href');
if($(this).next('.question').length <= 0)
$(this).after('<div class="question">'+theOptions.question+'<br/> <span class="yes">'+theOptions.yesAnswer+'</span><span class="cancel">'+theOptions.cancelAnswer+'</span></div>');
$(this).next('.question').animate({opacity: 1}, 300);
$('.yes').bind('click', function(){
window.location = thisHref;
});
$('.cancel').bind('click', function(){
$(this).parents('.question').fadeOut(300, function() {
$(this).remove();
});
});
});
});
}
})(jQuery);
all the thing was working well,
then i made ana jax pagination for the lists;
now the problem is that after this task, the jquery confirmation isnt working, i want the jquery script to be converted into simple jquery function like this
function ask()
{
}
EDIT:
i have written the function
function ask(obj)
{
alert('hii');
//var theOptions = jQuery.extend ({
var question= "Are You Sure ?";
var yesAnswer= "Yes";
var cancelAnswer= "Cancel";
//e.preventDefault();
var thisHref = $(obj).attr('href');
alert(thisHref) ;
if($(obj).next('.question').length <= 0)
$(obj).after('<div class="question">'+question+'<br/> <span class="yes">'+yesAnswer+'</span><span class="cancel">'+cancelAnswer+'</span></div>');
$(obj).next('.question').animate({opacity: 1}, 300);
$('.yes').bind('click', function(){
window.location = thisHref;
});
$('.cancel').bind('click', function(){
$(obj).parents('.question').fadeOut(300, function() {
$(obj).remove();
});
});
}
and i am calling this from
<img src="<?php echo base_url();?>assets/admin_assets/images/trash.png" alt="" title="" border="0" />
its taking the effect also;
but just after the animating effect, its getting redirected to the link, i need to make it stay, so that user can choose the option. but i cant...
MORE EDIT:
Ok i got the answer; i used the small code return false and it solved my problem
but then there's another problem
this line of code isnt working
$('.cancel').bind('click', function(){
$(obj).parents('.question').fadeOut(300, function() {
$(obj).remove();
});
});
You need to bind $('.ask').jConfirmAction(); to all the links again after you have gone to the new page.

jQuery Datatable: Edit button

I added Edit button to my jQuery Datatable. When the user clicks on this button, the row becomes editable. Updated row should be saved to MySQL DB, but here comes the problem - the updated row is not saved to DB. Firebug does not show any error message. Can someone help me figure out the problem?
<script type="text/javascript" charset="utf-8">
$(document).ready(function(){
$('#newspaper-b').dataTable({
"sPaginationType":"full_numbers",
"aaSorting":[[3, "asc"]],
"bJQueryUI":true
});
$(".edit_but").click(function() {
var ID=$(this).attr('id');
$("#first_"+ID).hide();
$("#last_"+ID).hide();
$("#first_input_"+ID).show();
$("#last_input_"+ID).show();
});
$(".edit_tr").change(function() {
var ID=$(this).attr('id');
var first=$("#first_input_"+ID).val();
var last=$("#last_input_"+ID).val();
var dataString = 'flightNum='+ ID +'&from='+first+'&STADate='+last;
$("#first_"+ID).html('<img src="load.gif" />'); // Loading image
if(first.length>0&& last.length>0) {
$.ajax({
type: "POST",
url: "callpage.php?page=tables/edit.php",
data: dataString,
cache: false,
success: function(html) {
$("#first_"+ID).html(first);
$("#last_"+ID).html(last);
}
});
} else
{
alert('All fields must be filled out.');
}
});
// Edit input box click action
$(".editbox").mouseup(function() {
return false
});
// Outside click action
$(document).mouseup(function() {
$(".editbox").hide();
$(".text").show();
});
$("tr").click(function(){
$(this).addClass("selected").siblings().removeClass("selected");
});
});
</script>
<table id="newspaper-b" border="0" cellspacing="2" cellpadding="2" width = "100%">
<thead>
<tr>
<th scope="col">Flt Num</th>
<th scope="col">From</th>
<th scope="col">STA Date</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
<?php foreach ($result1 as $row):
$flightNum=$row['flightNum'];
$from=$row['frm'];
$STADate=$row['STADate'];
?>
<tr id="<?php echo $flightNum; ?>" class="edit_tr">
<td><?php echo $row['flightNum'];?></td>
<td class="edit_td">
<span id="first_<?php echo $flightNum; ?>" class="text">
<?php echo $from;?>
</span>
<input type="text" value="<?php echo $from;?>"
class="editbox" id="first_input_<?php echo $flightNum; ?>"/>
</td>
<td class="edit_td">
<span id="last_<?php echo $flightNum; ?>" class="text">
<?php echo $STADate; ?>
</span>
<input type="text" value="<?php echo $STADate; ?>"
class="editbox" id="last_input_<?php echo $flightNum; ?>"/>
</td>
<td class="edit_td"><?php echo $row['pkID']; ?></td>
<td id="<?php echo $flightNum; ?>" class="edit_but">
<div>
<img src='images/edit.png' alt='Edit' />
</div>
</td>
</tr>
<?php endforeach;?>
</tbody>
</table>
edit.php
<?php
include_once 'include/DatabaseConnector.php';
if(isset($_POST['flightNum'])) {
$flightnum=$_POST['flightNum'];
$from=$_POST['from'];
$STADate=$_POST['STADate'];
$query = 'UPDATE flightschedule
SET frm="'.$from.'",STADate="'.$STADate.'"
WHERE flightNum="'.$flightNum.'"';
DatabaseConnector::ExecuteQuery($query);
echo '1';
} else {
echo '0';
}
?>
Try $(".edit_tr input").change() as the trigger.
Change events are limited to inputs, selects and textareas only, but you've tried binding it to a table row. The code above will bind it to any inputs inside the row with the class.

Categories