I want to change the status (Active/Deactive) of records in database without actually submitting(refreshing page) data, using jquery.
In my script Status is getting changed in database but as it is not refreshing, display status is not updating. is there a way to display the changed status without refreshing? i mean as soon as it updates in database, it should reflect in the status.
Here is my script
<table class="table table-bordered table-condensed table-striped mb-6">
<thead>
<tr>
<th>Firstname</th>
<th>Mobile</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php
//$CQuery = ""; my query
while($ConRow = DB_fetch_array($CQuery ))
{
?>
<tr>
<td><?php echo $ConRow['fname']; ?></td>
<td><?php echo $ConRow['mobile']; ?></td>
<?php if($ConRow['status']=='A') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-danger btn-sm">De-Activate</button> </td>
<?php } else if($ConRow['status']=='D') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-success btn-sm">Activate</button> </td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
</table>
Script
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var data1 = 'id=' + del_id;
if (confirm("Sure you want to De-Activate this?.")) {
$.ajax({
type : "POST",
url : "DisableContact.php",
data : data1,
success : function() {
}
});
$(this).parents(".record").animate("fast").animate({
opacity : "hide"
}, "slow");
}
return false;
});
});
</script>
PHP
$id = $_POST['id'];
$sel1 = "SELECT status FROM contacts WHERE con_id=".$id."";
$Sel = DB_query($sel1);
$srow = DB_fetch_array($Sel);
if($srow['status']=='A')
{
$sql = "UPDATE contacts SET status='D' WHERE con_id=".$id;
}
else
if($srow['status']=='D')
{
$sql = "UPDATE contacts SET status='A' WHERE con_id=".$id;
}
$result = DB_query($sql);
In database , Status is getting changed, but in table it is not showing up.
//Updated
<script type="text/javascript" >
$(function() {
$(".delbutton").click(function() {
var del_id = $(this).attr("id");
var info = 'id=' + del_id;
if (confirm("Sure you want to De-Activate this?.")) {
$.ajax({
type : "POST",
url : "DisableContact.php", //URL to the delete php script
data : info,
success: function(data) {
var d = $.trim(data); //triming value if there is any whitespaces
if (d == "A") {
//means data is activate so show that button
$("#"+del_id+ ".btn-success").show();
//hiding other
$("#"+del_id +".btn-danger").hide();
} else {
//show deactivate buttton
$("#"+del_id +".btn-danger").show();
//hide other button
$("#"+del_id +".btn-success").hide();
}
}
});
$(this).parents(".record").animate("fast").animate({
opacity : "hide"
}, "slow");
}
return false;
});
});
</script>
PHP
if($srow['status']=='A')
{
$sql = "UPDATE cust_contacts SET status='D' WHERE con_id=".$id;
echo 'A';
}
else
if($srow['status']=='D')
{
$sql = "UPDATE cust_contacts SET status='A' WHERE con_id=".$id;
echo 'D';
}
$result = DB_query($sql);
You can pass some value from php to ajax call and depending on that the required button will get displayed .So your php code will look like below :
..
if($srow['status']=='A')
{
$sql = "UPDATE contacts SET status='D' WHERE con_id=".$id;
echo "D";//will get passed as response to ajax
}
else
if($srow['status']=='D')
{
$sql = "UPDATE contacts SET status='A' WHERE con_id=".$id;
echo "A";//will get passed to ajax as response
}
Your ajax success function will look like below :
..
success: function(data) {
var d = $.trim(data); //triming value if there is any whitespaces
if (d == "A") {
//means data is activate so show that button
$("#"+del_id+ ".btn-success").show();
//hiding other
$("#"+del_id +".btn-danger").hide();
} else {
//show deactivate buttton
$("#"+del_id +".btn-danger").show();
//hide other button
$("#"+del_id +".btn-success").hide();
}
}
Update 1:
As you have use if-else to show button so i forgot here that other button will not exist in this case thats the reason jquery is not able to find other button and display blank.Now, to solve this you need to make some changes in your php code where you are displaying your table.Changes you need to make are as follows :
Change this :
<?php if($ConRow['status']=='A') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-danger btn-sm">De-Activate</button> </td>
<?php } else if($ConRow['status']=='D') { ?>
<td><button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-success btn-sm">Activate</button> </td>
<?php } ?>
to below :
<td> <div class="<?php echo $ConRow['con_id']; ?>"> <?php if($ConRow['status']=='A') { ?>
<button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-danger btn-sm">De-Activate</button>
<?php } else if($ConRow['status']=='D') { ?>
<button id="<?php echo $ConRow['con_id']; ?>" class="delbutton btn btn-success btn-sm">Activate</button>
<?php } ?> </div> </td>
Now ,in ajax success function we will use .html to add button inside <div></div> .So ajax will look like below :
if (d == "A") {
$("." + del_id).html('<button id="' + del_id + '" class="delbutton btn btn-danger btn-sm">De-Activate</button>');
} else {
$("." + del_id).html(' <button id="' + del_id + '" class="delbutton btn btn-success btn-sm">Activate</button> ');
}
Related
I was making a 'like system' - like the twitter system - with PHP and AJAX. In a way that I can't understand, it applies to all buttons on the page when I press only one button. Please help.
enter image description here
index.php
<div id="rezbtn">
<span id="rezarea">
<button style="font-size:15px; float:left; <? if($sorus["soru_id"] == $rezs['soru_id'] && $user_id == $rezs['user_id']){ echo 'color:green;';}else{ echo ''; } ?>" class="btn rezle" id="rezle" type="button" title="<? echo $sorus["soru_id"]; ?>">
<span>#Rezle <? echo $rez["rez_id"]; ?></span>
</button>
</span>
</div>
ajax.php
if($rezuscount > 0)
{
echo "<span style='color:black;'>#Rezle ", $rezcount-1,"</span>";
}
else
{
echo "<span style='color:green;'>#Rezle ", $rezcount+1,"</span>";
}
like.js
$('.rezle').click(function(){
var soruid = $(this).attr("title");
$.ajax({
type:"POST",
url:"ajax.php",
data:{"soru":soruid},
success:function(e)
{
$('.rezle').html(e);
}
})
});
My codes are like this.
Your problem here is that you're referring to all elements with the class rezle and updating them all with the ajax html response:
$('.rezle').html(e);
try this:
$('.rezle').click(function(){
const rezle_button = this;
var soruid = $(this).attr("title");
$.ajax({
type:"POST",
url:"rezle.php",
data:{"soru":soruid},
success:function(e)
{
$(rezle_button).html(e);
}
})
});
I cannot simply fetch any data on my table, I tried to clean my code but still not working here is my code
Controller - I am not sure if this is correct or am i still missing something
function fetch()
{
$data = $this->level_model->fetch_data();
echo $this->load->view('levels', ['data' => $data], TRUE );
}
View
<select class='form-input input-lg col-md-6' name="search_text" id="search_text">
<?php
foreach ($courses as $row) {
echo '<option value="'.$row->course.'">'.$row->course.'</option>';
}
?>
</select>
Table
<table class="table table-striped table-bordered" style="width:100%;">
<thead>
<tr>
<th scope="col">Course</th>
<th scope="col">Action</th>
</tr>
</thead>
<tbody id="showdata">
</tbody>
</table>
Model - so here where I query so i can match the data and fetch it.
function fetch_data($query)
{
$this->db->select("*");
$this->db->from("levels");
if($query != '')
{
$this->db->like('course_year', $query);
}
$this->db->order_by('id', 'ASC');
return $this->db->get();
}
AJAX / JAVASCRIPT I am not quite sure if my ajax is right please kindy check
<script>
$(document).ready(function(){
load_data();
function load_data(query)
{
$.ajax({
url:"<?php echo base_url(); ?>levels/fetch",
method:"POST",
data:{query:query},
success:function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+data[i].course_year+'</td>'+
'<td>'+
'<a class="btn btn-primary item-edit" data="'+data[i].id+'"" href="javascript:;"><i class="fa fa-edit"></i> Edit </a> | '+
'<a class="btn btn-danger item-delete" data="'+data[i].id+'"" href="javascript:;"><i class="fa fa-remove"></i> Delete</a></td>'+
'</tr>';
}
$('#showdata').html(html);
}
})
}
$('#search_text').change(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
Try changing your controller fetch method as below:
function fetch()
{
$data = $this->level_model->fetch_data(
$this->input->post('query');
);
echo $this->load->view('levels', ['data' => $data], TRUE );
}
When the document is loading you are calling the load_data function without passing value to the query parameter here.
<script>
$(document).ready(function(){
load_data();
You should give it a default value here like this
<script>
$(document).ready(function(){
load_data();
function load_data(query='')
{
$.ajax({
url:"<?php echo base_url(); ?>levels/fetch",
method:"POST",
data:{query:query},
success:function(data){
var html = '';
var i;
for(i=0; i<data.length; i++){
html += '<tr>'+
'<td>'+data[i].course_year+'</td>'+
'<td>'+
'<a class="btn btn-primary item-edit" data="'+data[i].id+'"" href="javascript:;"><i class="fa fa-edit"></i> Edit </a> | '+
'<a class="btn btn-danger item-delete" data="'+data[i].id+'"" href="javascript:;"><i class="fa fa-remove"></i> Delete</a></td>'+
'</tr>';
}
$('#showdata').html(html);
}
})
}
$('#search_text').change(function(){
var search = $(this).val();
if(search != '')
{
load_data(search);
}
else
{
load_data();
}
});
});
</script>
Also you are not passing any data to your model .
function fetch()
{
$data = $this->level_model->fetch_data(
$_POST['query'];
);
echo $this->load->view('levels', ['data' => $data], TRUE );
}
How to display the value in table..I am not able to display the value.In ajax i've sent all the value and after retrieving the records from database. I've to display the result in table
Output is
[{"ID":"15","patient_name":"Sangeeta","patient_email":"sangeetha#gmail.com","gender":"Female","age":"26","address":"Jayanagar","city":"Bangalore","laboratory_name":"Anand","laboratory_address":"bsk","laboratory_place":"bengaluru","referral_pat_id":"18","active":"1","created_on":"2017-06-13"}]
How to retrieve each value
AJAX Script
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
alert(rowid);
console.log(rowid);
$.ajax({
type : 'post',
url : 'url', //Here you will fetch records
data : 'id='+ rowid, //Pass $id
success : function(resp){
alert(resp);
var trHTML = '';
$.each(resp, function (i, userData) {
for (i = 0; i < resp.UserData.length; i++) {
alert(trHTML);
trHTML +=
'<tr><td>'
+ resp.userData[i].ID
+ '</td><td>'
+ resp.userData[i].patient_name
+ '</td><td>'
+ resp.userData[i].patient_email
+ '</td></tr>';
}
});
$('#tBody').append(trHTML);
}
});
});
});
Controller
public function fetch_records()
{
print_r($_POST);
$this->load->model('Physician_confirm_m');
$id = $_POST['id']; //escape string
print_r($id);
$result=$this->Physician_confirm_m->fetch_history_records($id);
echo json_encode($result);
}
Model
public function fetch_history_records($id)
{
$this->db->where('referral_pat_id',$id);
$this->db->from('referral_confirmation_details');
$q = $this->db->get();
return $q->result();
}
HTML
<button type="button" class="btn btn-info disablebtn" style="text-align: center;" data-toggle="modal" data-target="#myModal" data-id="<?php echo $post->referral_patient_id;?>"><i class="fa fa-history" aria-hidden="true"></i><strong> HISTORY</strong></button>
<div class="modal-body">
<table>
<tbody id="tBody"></tbody>
</table>
</div>
</div>
You can do in another way to achieve this.
$(document).ready(function(){
$('#myModal').on('show.bs.modal', function (e) {
var rowid = $(e.relatedTarget).data('id');
console.log(rowid);
$.ajax({
type : 'post',
url : 'url', //Here you will fetch records
data : 'id='+ rowid, //Pass $id
success : function(resp){
var obj = jQuery.parseJSON(resp);
$('#tBody').append(obj.ajaxPage);
}
});
});
});
In Your Controller should be like
public function fetch_records()
{
$this->load->model('Physician_confirm_m');
$id = $_POST['id']; //escape string
$data['result']=$this->Physician_confirm_m->fetch_history_records($id);
$result['ajaxPage'] = $this->load->view('pages/result_table', $data, true);
echo json_encode($result);
}
result_table view page should be
<?php if($result){ foreach($result as $row){ ?>
<tr>
<td><?=$row['ID']?></td>
<td><?=$row['patient_name'];?></td>
<td><?=$row['patient_email'];?></td>;
</tr>
<?php
}
}?>
I want to display a time in place of button when button clicked then
button fadein and time show in place of button but my script not
working button fadein only and time didn't show in place of button and
"Time shown when i refresh the whole page but not a particular part
refreshing."
foreach($leads as $val):?>
<tr><td class = 'account-name sort-value'>
<span style='float:left;width:47%;'>
<?php echo $val->first_name .' '. $val->last_name;?></span>
<?php
if($val->checkin_time != '') {?>
<span class = 'Dischktime' id = 'dischktime{<?php echo $val->lead_id;?>}' ><?php echo $val->checkin_time;?>
</span>
<?php
}
else {?>
<span style='float:left;width:26%;'>
<button id ='checkin{<?php echo $val->lead_id;?>}' class='btn btn-default' name='checkinbt' value='<?php echo$val->lead_id; type='button'>CheckIn
</button>
</span>
<?php }
?>
<span>
<?php echo $_SESSION['drivername'];?>
</span>
</td></tr>
<?php endforeach;
}
?>
<script type="text/javascript">
$(document).ready(function() {
$("[id=checkin]").on("click" , function() {
//$(this).fadeOut();
$(this).fadeOut('slow', function() {
//alert("it's working");
$("#dischktime").show();-------(Not working)
});
$('button').submit(function(e) {
e.preventDefault();
return false;
});
//alert($(this).attr('value'));
var lead_id = $(this).attr('value');
//alert(lead_id);
$.ajax({
method: "POST",
url: 'search_db_test.php',
data: 'checkinbt=' + lead_id,
success: function(data) {
$(this).html(data);
//$("#dischktime").show(data);-----(Not working)
}
});
});
});
</script>
You are creating element based on condition. When button is shown your Time span having id : dischktime is not created and thus its not available in DOM. Modify you foreach :
foreach($leads as $val):?>
<tr><td class = 'account-name sort-value'>
<span style='float:left;width:47%;'>
<?php echo $val->first_name .' '. $val->last_name;?></span>
<?php
if($val->checkin_time != '') {?>
<span class = 'Dischktime' id = 'dischktime{<?php echo $val->lead_id;?>}' ><?php echo $val->checkin_time;?>
</span>
<?php
}
else {?>
<span style='float:left;width:26%;'>
<button id ='checkin{<?php echo $val->lead_id;?>}' class='btn btn-default' name='checkinbt' value='<?php echo$val->lead_id; type='button'>CheckIn
</button>
</span>
**<span style="display:none;" class = 'Dischktime ' id = 'dischktime{<?php echo $val->lead_id;?>}' >**
</span>
<?php }
?>
<span>
<?php echo $_SESSION['drivername'];?>
</span>
</td></tr>
<?php endforeach;
}
Try Css Property
$("#dischktime").css('display', 'block') instead of $("#dischktime").show();
EDIT: thanks everyone for your help. It still doesn't work though.
This script works fine if i change the table to an ordered list. (tr = ol and td = li) That's why i really think the issue comes from the append part of the code. Any ideas?
Everything seems to work fine except for the "load more" button that disappears after it's been clicked. The new posts load but the users cannot click on the button again to load more posts. I've looked on SO but couldn't find a fix for my issue.
If I remove this line:
$("#more"+ID).remove();
then the button does show up but the gif image keeps on loading forever and the button is not clickable...
Here's the index.php code:
<table id="update_list" >
<tbody>
<?php
$result=query("SELECT * FROM postlist ORDER BY postid DESC LIMIT 9");
foreach ($result as $row)
{
$postid=$row['postid'];
$post=$row['post'];
?>
<tr id="<?php echo $postid ?>">
<td>
<?php echo $post; ?>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<div id="more<?php echo $postid; ?>" class="morebox">
more
</div>
</div>
</body>
<script type="text/javascript">
$(function() {
$('.more').live("click",function()
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).html('<img src="/loading.gif" />');
$.ajax({
type: "POST",
url: "loadmore.php",
data: "lastpost="+ ID,
cache: false,
success: function(html){
$("#update_list >tbody").append(html);
$("#more"+ID).remove();
}
});
}
else
{
$(".morebox").html('The End');
}
return false;
});
});
</script>
</html>
Here is the loadmore php page:
<?php
include("includes/config.php");
if(isset($_POST['lastpost']))
{
$lastpost=$_POST['lastpost'];
$result=query("SELECT * from postlist WHERE postid < '$lastpost' ORDER BY postid DESC
LIMIT 5");
foreach ($result as $row){
$postid=$row['postid'];
$post=$row['post'];
?>
<tr id="<?php echo update$postid ?>">
<td>
<?php echo $post; ?>
</td>
</tr>
<?php } ?>
<div id="more<?php echo $postid; ?>" class="morebox">
more
</div>
<?php }?>
Any tips on what i'm doing wrong here ?
Thanks !
Try this:
$(function() {
$('.more').live("click",function()
{
var ID = $(this).attr("id");
if(ID)
{
$("#more"+ID).find("a").hide().insertAfter('<img src="/loading.gif" />'); //hide a link and insert image after it
$.ajax({
type: "POST",
url: "loadmore.php",
data: "lastpost="+ ID,
cache: false,
success: function(html){
$("#update_list >tbody").append(html);
$("#more"+ID).find("img").remove(); // remove img
$("#more"+ID).find("a").show(); //and show link
}
});
}
else
{
$(".morebox").html('The End');
}
return false;
});
});
You are removing the load more button after button clicked
$("#update_list >tbody").append(html);
//$("#more"+ID).remove(); remove this line