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
Related
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.
I have datatables contain input box and button for each row data, for sure form was added outside table tag, but i have issue while clicking on button submit inside that table, the button did not work well and no appear anything error logs
My Controller.php for displaying row:
public function tagihankost()
{
$list = $this->tagihan->get_datatables();
$data = array();
foreach ($list as $tagih) {
$row = array();
$format = number_format($tagih->harga, '0', '', '.');
$row[] = "<input type='text' value='$tagih->bulan' id='bulan' class='rowtagihan' form='payment-form' readonly>";
$row[] = "<input type='number' value='$format' id='harga' class='rowtagihan' form='payment-form' readonly>";
if ($tagih->status == 200) {
$row[] = "<span class='badge bg-success' style='color:white'>Lunas</span>";
} elseif ($tagih->status == 201) {
$row[] = "<span class='badge bg-warning' style='color:black'>Pending</span>";
} else {
$row[] = "<span class='badge bg-danger' style='color:white'>Belum Bayar</span>";
};
$row[] = "<button id='pay-button'>Pay!</button>";
$data[] = $row;
}
$output = array(
"draw" => $_POST['draw'],
"recordsTotal" => $this->tagihan->count_all(),
"recordsFiltered" => $this->tagihan->count_filtered(),
"data" => $data,
);
echo json_encode($output);
}
My View.php :
<div class="card bodycontent">
<div class="card-body">
<div class="tab-pane table-responsive">
<table id="tablekost" class="table table-striped table-bordered" width="100%" cellspacing="0">
<thead>
<tr>
<td>Bulan</td>
<td>Tagihan</td>
<td>Status</td>
<td>#</td>
</tr>
</thead>
<form id="payment-form" method="post" action="<?= site_url() ?>/snap/finish">
<input type="hidden" name="result_type" id="result-type" value="">
<input type="hidden" name="result_data" id="result-data" value="">
</form>
<tbody>
</tbody>
</table>
</div>
</div>
</div>
And the last thing is my JS:
<script type="text/javascript">
$('#pay-button').click(function(event) {
event.preventDefault();
$(this).attr("disabled", "disabled");
$.ajax({
url: '<?= site_url() ?>/snap/token',
cache: false,
success: function(data) {
//location = data;
console.log('token = ' + data);
var resultType = document.getElementById('result-type');
var resultData = document.getElementById('result-data');
function changeResult(type, data) {
$("#result-type").val(type);
$("#result-data").val(JSON.stringify(data));
//resultType.innerHTML = type;
//resultData.innerHTML = JSON.stringify(data);
}
snap.pay(data, {
onSuccess: function(result) {
changeResult('success', result);
console.log(result.status_message);
console.log(result);
$("#payment-form").submit();
},
onPending: function(result) {
changeResult('pending', result);
console.log(result.status_message);
$("#payment-form").submit();
},
onError: function(result) {
changeResult('error', result);
console.log(result.status_message);
$("#payment-form").submit();
}
});
}
});
});
So what i have missed?
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
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 working on this project that uses a live table edit style (works perfectly) but trying to include the twitter style follow and unfollow in one of the columns (The last one precisely) the php works well but I've having problems returning the data to ajax to post to another php script.
Below shows the major part of the script (The php and ajax). I included a php comment on the important area of interest.
<?php $query_pag_data = "SELECT * FROM applicant_result WHERE year='$year' AND
class='$class' ORDER by candidate_no "; $uid=strip_tags($id);
$result_pag_data = mysql_query($query_pag_data) or die('MySql Error' . mysql_error());
$finaldata = "";
$tablehead= '';
$tablehead= "<tr>
<th class='data'>#</th>
<th class='data'>Applicant ID</th>
<th class='data'>Year</th>
<th class='data'>Class</th>
<th class='data'>$subject_1</th>
<th class='data'>$subject_2</th>
<th class='data'>$subject_3</th>
<th class='data'>$subject_4</th>
<th class='data'>$subject_5</th>
<th class='data'>Total</th>
<th class='data'>Status</th>
<th class='data'>Interview</th>
</tr>";
while($row = mysql_fetch_array($result_pag_data)) {
$id=htmlentities($row['candidate_no']);
$subject_1=htmlentities($row['subject_1']);
$subject_2=htmlentities($row['subject_2']);
$subject_3=htmlentities($row['subject_3']);
$subject_4=htmlentities($row['subject_4']);
$subject_5=htmlentities($row['subject_5']);
$total=htmlentities($row['total']);
$status=htmlentities($row['interview']);
$uid= strip_tags($row['candidate_no']);
/* HELLO FORUMITES, THIS IS THE MAJOR AREA OF FOCUS HERE */
if($status!=0){$button="
<span id='loading<?php echo $uid; ?>'></span>
<span class='button following' id='following<?php echo $uid; ?>' `onClick='follow_or_unfollow(<?php echo $uid; ?>,'following');'>Following</span>`
<span style='display:none;' class='button follow' id='follow<?php
echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
";}
else{
$button="
<span id='loading<?php echo $uid; ?>'></span>
<span class='button follow' id='follow<?php echo $uid; ?>'
onClick='follow_or_unfollow(<?php echo $uid; ?>,'follow');'>Follow</span>
<span class='button following' style='display:none;'`id='following<?php echo $uid; ?>' onClick='follow_or_unfollow(<?php echo $uid;` ?>,'following');'>Following</span>
";}
$tabledata.="<tr id='$id' class='edit_tr'>
<td class='edit_td' ><span class='text'>$counter</span></td>
<td class='edit_td' ><span class='text'>$id</span></td>
<td class='edit_td' ><span class='text'>$year</span>
<input type='hidden' value='$year' class='editbox' id='six_input_$id' /></td>
<td class='edit_td' ><span class='text'>$class</span>
<input type='hidden' value='$class' class='tbox' id='seven_input_$id' /></td>
<td class='edit_td' >
<span id='one_$id' class='text'>$subject_1</span>
<input type='text' value='$subject_1' class='editbox' id='one_input_$id' /></td>
<td class='edit_td' ><span id='two_$id' class='text'>$subject_2</span>
<input type='text' value='$subject_2' class='editbox' id='two_input_$id'/></td>
<td class='edit_td' ><span id='three_$id' class='text'>$subject_3 </span>
<input type='text' value='$subject_3' class='editbox' id='three_input_$id'/></td>
<td class='edit_td' ><span id='four_$id' class='text'>$subject_4</span>
<input type='text' value='$subject_4' class='editbox' id='four_input_$id' /></td>
<td class='edit_td' ><span id='five_$id' class='text'>$subject_5</span>
<input type='text' value='$subject_5' class='editbox' id='five_input_$id' /></td>
<td class='edit_td' ><span class='text'>$total</span></td>
<td class='edit_td' ><span class='text'>$status</span></td>
<td>$button </td>
</tr>";
$counter++;
}
$finaldata = "<table width='100%'>".$tablehead." ".$tabledata. "</table>";
echo $finaldata;
NOW THE AJAX
<script type="text/javascript">
$(document).ready(function() {
$('.following').hover(function() {
$(this).text('Unfollow');
}, function() {
$(this).text("Following");
});
});
function follow_or_unfollow(id, action) {
var dataString = "id=" + id;
$.ajax({
type: "POST",
url: "follow_or_unfollow.php",
data: dataString,
beforeSend: function() {
if (action == "following") {
$("#following" + id).hide();
$("#loading" + id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
}
else if (action == "follow") {
$("#follow" + id).hide();
$("#loading" + id).html('<img src="loading.gif" align="absmiddle" alt="Loading...">');
}
},
success: function(response) {
if (action == "following") {
$("#loading" + id).html('');
$("#follow" + id).show();
}
else if (action == "follow") {
$("#loading" + id).html('');
$("#following" + id).show();
}
}
});
}
</script>
Try changing your ajax type to GET type: "GET",
Also how are you receiving your global at the other end of the php
Did you used GET or POST?