i have a search input field in my page. currently i am displaying data from the database successfully in a table(id="table_userinfo"). Now i want to add the live search functionality to my table(id="table_userinfo") as well. i am using php codeigniter.
in my user_model i have:
function search_userInfo($keyword)
{
$this->db->like('DeviceName',$keyword);
$this->db->or_like('RegistrationDateTime',$keyword);
$this->db->or_like('LastUpdateDateTime',$keyword);
$this->db->or_like('AppVersion ',$keyword);
$this->db->or_like('iOSVersion',$keyword);
$this->db->or_like('DeviceDID',$keyword);
$this->db->or_like('DeviceToken',$keyword);
$query = $this->db->get('userinfo');
if($query)
{
return $query->result();
}
else
{
return NULL;
}
}
in my login_controller:
function displayDatabase()
{
$data['tableInfo']=$this->user_model->fetchData();
$this->load->view('adminPanel_view',$data);
}
function search()
{
$search=$this->input->post('searchString');
$data['tableInfo']=$this->user_model->search_userInfo($search);
}
in my adminPanel_view page, i have:
<script>
$('#search').keyup(function(){
var input_data = {searchString: $('#search').val() };
//console.log(input_data);
$.ajax({
type:"POST",
url:base_url+'index.php/login_controller/search',
data:input_data,
success:function(data){
if(data.length>0)
{
console.log(data);
}
}
});
});
</script>
<table class="table table-bordered table-hover table-full-width" id="table_userinfo">
<thead>
<tr>
<th>Serial No.</th>
<th class="hidden-xs">Device Name</th>
<th>Device Type</th>
<th>RegistrationDateTime </th>
<th>LastUpdateTime</th>
<th>LastPushNotificationSent</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php //print_r ($tableInfo);exit;?>
<?php $no=0;?>
<?php foreach($tableInfo as $r): ?>
<tr>
<td><?php echo $no ?></td>
<td><?php echo $r->DeviceName ?></td>
<td><?php echo $r->DeviceType ?></td>
<td><?php echo $r->RegistrationDateTime ?></td>
<td><?php echo $r->LastUpdateDateTime ?></td>
<td><?php echo $r->LastPushNotificationSent ?></td>
<td><?php echo "Actions" ?></td>
</tr>
<?php $no+=1;?>
<?php endforeach; ?>
</tbody>
</table>
this is the search bar that i am using in addition to this table
<div id="" class="dataTables_filter">
<label>
<input placeholder="Search" class="form-control input-sm" aria-controls="sample_1" type="text" id="search" name="search_userinfo">
</label>
</div>
how do i display the data that i am getting through the Ajax call and show it in the table(id="search_userinfo).
inside of function search() return it as:
echo json_encode($this->user_model->search_userInfo($search));
and inside of javascript do this
<script>
$('#search').keyup(function(){
var input_data = {searchString: $('#search').val() };
//console.log(input_data);
$.ajax({
type:"POST",
url:base_url+'index.php/login_controller/search',
data:input_data,
dataType: "json",
success:function(data){
$("#table_userinfo").html(" "); //clear everything from table
data.forEach(function(entry) {
$("#table_userinfo").append("<td>"+entry.DeviceName+"</td><td>");
//just add everything here
});
}
});
});
</script>
Your Controller
function search()
{
$search=$this->input->post('searchString');
$data['tableInfo']=$this->user_model->search_userInfo($search);
print_r(json_encode($data['tableInfo']));exit
}
In your ajax add this line
dataType:'json';
Now In Your Success function You will get an json for that data ,You can append like this,
$.each(data, function(i, item) {
alert(item.DeviceName); // append data where you need to append
});
But Please be sure about amount of data you get in success function, with keyup
Related
Hy,
I want to ban a user when the admin clicks on "bannir" (this will collect the id of the user and put "banned" at 1 in the database)
to print the user informations I'm using a while loop, but when I try to collect the id of the user where in the html tag with the class "idUser", it is always sending the first id and I don't know why..
image of membership area
<div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Prénom</th>
<th>Nom</th>
<th>Email</th>
<th>Admin</th>
<th>Banni</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<?php while($userInfo = $req->fetch()){?>
<tr>
<td class="idUser"><?= $userInfo['id_user'] ?></td>
<td><?= $userInfo['last_name'] ?></td>
<td><?= $userInfo['first_name'] ?></td>
<td><?= $userInfo['email'] ?></td>
<td><?php if($userInfo['admin'] == 1){ ?>
<img src="../img/ok.png">
<?php } else { ?>
<img src="../img/no.png">
<?php } ?></td>
<td><?php if($userInfo['banned'] == 1){ ?>
<strong style="color:#E04949;">OUI</strong>
<?php } else { ?>
<strong style="color:#6AC259;">NON</strong>
<?php } ?></td>
<td>Modifier | <?php if($userInfo["banned"] == 0){ ?> Bannir <?php }else{ ?> Débannir <?php } ?></td>
</tr>
<?php } ?>
</tbody>
</table>
<script type="text/javascript">
$('.banMemberJquery').click(function(){
var idUser = $( ".idUser" ).html();
alert('Utilisateur Numero ' + idUser + ' banni avec succès.');
$.ajax({
url:"banMemberRequest.php",
data:'idUser='+idUser,
}).done(function(data){
$('#result').html(data);
});
});
PS : When I click on "bannir", the request in the file "banMemberRequest.php" is working correctly.
Thank's in advance for helping
The issue is because you're selecting all .idUser elements. Calling html() on that will only read the HTML of the first one in that collection.
To fix this you need to use DOM traversal to find only the .idUser element which is related to the .banMemberJquery element which was clicked. To do that you can use closest() and find(), like this:
$('.banMemberJquery').click(function() {
var idUser = $(this).closest('tr').find('.idUser').text(); // note text(), not html()
$.ajax({
url: 'banMemberRequest.php',
data: { idUser: idUser },
}).done(function(data) {
$('#result').html(data);
});
});
I want to implement drag and drop reordering functionality with auto save on a cakephp table.
I am trying to implement this example in my CakePHP project without the use of SQL commands.
I want to update the priority fields of the table rows.
So in view.ctp:
<div class="container">
<table class="table table-bordered">
<tr>
<th>#</th>
<th>Name</th>
<th>Defination</th>
</tr>
<tbody class="row_position">
<?php
foreach ($user->tasks_to as $task) {
?>
<tr id="<?php echo $task['id'] ?>">
<td><?php echo $task['id'] ?></td>
<td><?php echo $task['name'] ?></td>
<td><?php echo $task['description'] ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
<script type="text/javascript">
$( ".row_position" ).sortable({
delay: 150,
stop: function() {
var selectedData = new Array();
$('.row_position>tr').each(function() {
selectedData.push($(this).attr("id"));
});
updateOrder(selectedData);
}
});
function updateOrder(data) {
$.ajax({
url:"updateTasksOrder",
type:'post',
data:{position:data},
success:function(){
alert('your change successfully saved');
}
})
}
</script>
and in the controller:
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->Auth->allow();
}
public function updateTasksOrder()
{
$this->autoRender = false;
$position = $_POST['position'];
$i=1;
$connection = ConnectionManager::get('default');
foreach($position as $k=>$v){
$connection->update('tasks', ['priority' => $i], ['id' => $v]);
$i++;
}
}
But its not working, it's not updating priority field. Any suggestions why?
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 having a problem in updating the qty field in my database using ajax. I don't know where the error occur because when I tried to click the minus button it keeps on subtracting it multiple times. I've google same problem but didn't find any solution. Here is my code.
Here is my CONTROLLER:
public function deduct(){
$this->inpatient_model->deduct();
$data['inpatient'] = $this->inpatient_model->getinpatientlab();
$this->load->view('admin/queryinpatient',$data);
}
Here is my MODEL:
public function deduct(){
$this->db->select('*');
$this->db->from('inpatientlab');
$this->db->where('ilid',$this->input->post('lid'));
$query = $this->db->get();
$row = $query->result_array();
if($query->num_rows() > 0 ){
if($row[0]['qty'] > 1){
$uno = 1;
$this->db->set('qty','qty-'.$uno,FALSE)
->where('ilid',$this->input->post('lid'))
->update('inpatientlab');
}
}
}
Here is my VIEW:
<?php if($inpatient): ?>
<table class="table table-striped">
<tr>
<th>Description</th>
<th>Quantity</th>
<th>Price</th>
<th>Sub Total</th>
<th>Action</th>
</tr>
<?php foreach($inpatient as $rows): ?>
<tr>
<td><?php echo $rows->ldesc; ?></td>
<td><?php echo $rows->qty; ?></td>
<td><?php echo $rows->lprice; ?></td>
<td><?php echo number_format($rows->qty * $rows->lprice,2); ?></td>
<td>
<button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnRemove"><i class="icon-trash"></i></button>
<button value="<?php echo $rows->ilid; ?>" class="btn btn-danger btnMinus"><i class="icon-minus"></i></button>
</td>
</tr>
<?php endforeach; ?>
</table>
<script type="text/javascript" >
(function(){
$(document).on('click','.btnMinus',function(){
var lid = $(this).val(),
pid = $('.pid').val(),
dataString = "id=" + pid + "&lid=" + lid;
console.log(dataString);
$.ajax({
type:"POST",
url: base_url + "inpatient/deduct",
data:dataString,
success:function(data){
$('.pickedlab').html(data);
}
})
return false;
});
})()
My View in here is also loaded via ajax inside a bootstrap-modal. I really have no idea why it keeps on subtracting multiple times. Any help?
Have you tried using the browser debug and find out whether your AJAX is calling multiple times. I am not familiar with your Controller-Model settings, normally my models are pure POCO class only. All computations are done at the controller level.
I am developing a web site with Code Igniter 2.1.3. I am using an AJAX call to post data to my controller, but for some reason the data is not being sent.
I am using AJAX calls all over the application and they all work well, just not this one.
My view:
<div id="ajaxResult">
<div class="page-header"><h2>Review Comments</h2></div>
<div class="row-fluid">
<div class="span12">
<?php if(count($comments)): ?>
<table class="table">
<thead>
<tr>
<th>Author</th>
<th>Email</th>
<th>IP Address</th>
<th>Date</th>
<th>Comment</th>
<th>Approve</th>
</tr>
</thead>
<tbody>
<?php foreach($comments as $comment): ?>
<tr>
<td>
<?php echo $comment->comment_author; ?>
</td>
<td><?php echo $comment->comment_author_email; ?></td>
<td><?php echo $comment->comment_author_IP; ?></td>
<td>
<?php
$date = new DateTime($comment->comment_date);
$date = $date->format('d/m/Y');
echo $date;
?>
</td>
<td>
<?php echo $comment->comment_content; ?>
</td>
<td>
<?php
$approve = array(
'name' => 'comment_approved',
'class' => 'approve',
'value' => '1',
'data-commentid' => $comment->id
);
echo form_checkbox($approve);
?>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php else: ?>
<p>No comments were found for review.</p>
<?php endif; ?>
</div>
</div>
</div>
<script>
$(function()
{
$('.approve').on('click', function()
{
var id = $(this).data('commentid'); alert(id);
$.ajax({
url: "<?php echo site_url('blog/admin/review_comment'); ?>",
type:'POST',
data: { comment_id: id },
success: function(data) { $('#ajaxResult').html(data); } // End of success function of ajax form
}); // End of ajax call
});
});
</script>
My controller function:
public function review_comment()
{
$this->load->helper('form');
$this->load->model('blog_comment_m');
if(isset($_POST['comment_id']))
{
$id = $this->input->post('comment_id');
$data['comment_approved'] = 1;
$this->blog_comment_m->save($data, $id);
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->load->view('admin/review_comments', $this->data);
}
else
{
$this->data['comments'] = $this->blog_comment_m->get_by(array('comment_approved'=>0));
$this->data['subview'] = 'admin/review_comments';
$this->load->view('admin/_layout_main', $this->data);
}
}
I tried to alert the comment_id and it does give me a value of 1. In the controller I do var_dump($_POST), always empty.
I also tried if(isset($_POST['comment_id'])). In the controller it never goes into the if(isset($_POST['comment_id']) statement.
Why is that?
Found it.
It was the fact that I started using csrf on form submits, a new thing for me, and I didn't add this url to the config file.
Adding this:
$config['csrf_exclude_uris'] = array('blog/admin/review_comment');
Solved the problem......
Thank you very very much for all the help.