Suppose we have a table name player and in player table we have field like id , image,player_name, status,  etc. Suppose data inside fields are id=1, name=ram, image=abcd.jpg and status=0, Now we want to change status to 1, and again we have to change the status to 0. So we uses ajax for the process because we save our time .
HTML:
<div class="container">
<div class="col-md-10" style="margin-top:20px;">
		<div class="heading" style="background:#EAEAEA;height:45px;margin-bottom:15px;padding:4px;">
			<span style="border:1px solid #EAEAEA;margin-top:8px;"><strong><h3 style="margin:0px;color:grey;float:left;">Players Table</h3><strong></span>
			<span style="float:right;margin-top:4px;font-size:21px;"><?php echo $this->Html->link('Add Players',array('action'=>'player')); ?></span>
			<!--<span style="float:right;margin-top:14px;"><?php echo $this->Html->link('Player Gallery',array('action'=>'playergalleryview')); ?></span>-->
		</div>
		<table class="table table-striped">
			<tr>
				<th>Id</th>
				<th>Player image</th>
				<th>Player Name</th>
				<th>Category</th>
				<th>Gallery</th>
				<th>Status</th>
				<th>Action</th>
			</tr>
			
    <?php $i=1; foreach ($Player as $post){if($post['Player']['category'] == 1) $link = 'img_1.jpg'; else if($post['Player']['category'] == 2) $link = 'img_2.jpg'; else if($post['Player']['category'] == 3) $link = 'img_1.jpg'; else $link = 'img_4.jpg';
	if($post['Player']['status'] == 1) $abcd = 'img8.jpg';else $abcd='img7.png';
	?>
    <tr>
	
        <td><?php 	
		echo $i;
		$i++;
		//echo $post['Player']['id']; ?></td>
		<td><?php echo $this->Html->image('player/'.$post['Player']['profile_pic'],array('style'=>'width:150px;height:100px;'));?></td>
		<td>
            <?php echo $this->Html->link($post['Player']['player_name'],
					array('controller' => 'Admin', 'action' => 'player_view', $post['Player']['id'])); ?>
        </td>
		<td> <?php echo $this->Html->image('/img/player/'.$link,array('style'=>'width:30px;height:30px;'));?></td>
		 
          
			<td>
			            <?php
                echo $this->Html->link(
                    'Player gallery',
                    array('action' => 'playerimagegallery', $post['Player']['id'])
                );
				
            ?></td>
			<!--<td>
				<?php echo $this->Form->PostLink($post['Player']['status']?'active':'inactive',array($post['Player']['status']));?>
			</td>
			-->
			
			
					<td id="load<?php echo $post['Player']['id'];?>">
								<i data="<?php echo $post['Player']['id'];?>" id="status_<?php echo $post['Player']['id'];?>"  class="status_checks btn <?php echo ($post['Player']['status'])?'btn-success': 'btn-danger'?>">
							<?php echo ($post['Player']['status'])? 'Active' : 'Inactive'?><img src="#" style="width:40px;height:30px;display:none;" />
								</i>
							</td>			
		
    </tr>
 <?php } ?>
</table>
</div>
</div>
Ajax:
$(document).on('click','.status_checks',function(){
							
  var status = ($(this).hasClass("btn-success")) ? '0' : '1';
  var msg = (status=='0')? 'Deactivate' : 'Activate';
 
	var current_element = $(this);
	var id= $(current_element).attr('data');
	
	url = webURL+"admin/playerstatus";
	
	$.ajax({
	  type:"POST",
	  url: url,
	  data: {
		id:id,
		status:status
	},
	  success: function(data)
	  {
	
		if(data)
		{
		
		$("#status_"+id).html();
		
			if(status== 1)
			{ 
			
					
				$("#status_"+id).html('Active').attr('class','status_checks btn btn-success');
				
				
			}else {
			
				$("#status_"+id).html('Inactive').attr('class','status_checks btn btn-danger');
			}
		
		}
		
	  }
	});
						        
						
});
Controller:
function playerstatus()
{
	$status=$_POST['status'];
	$id=$_POST['id'];
	$this->loadModel('Player');
	$this->layout='';
	if(!empty($this->data))
		{
		     $this->Player->id=$id;
		     $this->Player->status=$status;
		     $this->Player->save($this->request->data);			
		}
		die;
}
Hope this will help you..
                       
                    
0 Comment(s)