in index.ctp my code is as follows :-
<?php
foreach( $teacher as $teachers )
{
echo "<tr>";
echo "<td>".$teachers['Teacher']['username']."</td>";
echo "<td>".$teachers['Teacher']['dgen']."</td>";
echo "<td>".$teachers['Teacher']['dqual']."</td>";
<?php
echo "<td>".$this->Form->postLink(__('Delete'), array('action' => 'delete',$teachers['Teacher']['id']), null, __('Are you sure you want to delete %s?', $teachers['Teacher']['id']))."</td>";
echo "</tr>";
}
?>
mycontroller code:-
public function delete()
{
$this->autoRender = false;
$this->layout = 'ajax';
$id = $_GET['id'];
$this->loadModel('Teacher');
$this->Teacher->delete($id);
//$this->Session->setFlash('The post with id: '.$id.' has been deleted.');
//$this->redirect(array('action'=>'index'));
}
Ajax call :
<script type="text/javascript">
$('a.deleteajax').click(function(event){
event.preventDefault();
var answer = confirm("Delete this record?")
if (answer){
$.ajax({
url:'/Teachers/TeachersController/delete/',
type:'GET',
data: $(this).attr("href")
});
}
return false;
});
</script>
please help me ... i want delete records without refresh page.
change ajax to this:Add id
as key in data
to access it in your controller.better add success part for confirmation msg of action.
$.ajax({
url:'/Teachers/TeachersController/delete/',
type:'GET',
data: {id:$(this).attr("href")},
success:function(resp){//reso is msg string returned from controller.
alert(resp);
}
});
Let give you a solution step by step
At first in your index.ctp
You can apply this code
foreach( $teacher as $teachers )
{
echo "<tr id='echo $teachers['Teacher']['id'];'>"; //used id for daynamic tr, for hide temporary your delete data.
echo "<td>".$teachers['Teacher']['username']."</td>";
echo "<td>".$teachers['Teacher']['dgen']."</td>";
echo "<td>".$teachers['Teacher']['dqual']."</td>";
echo $this->html->link('','#',
array(
'class'=>'del btn btn-sm btn-danger glyphicon glyphicon-remove',
'id'=>$teachers['Teacher']['id'] //important
));
echo "</tr>";
}
Then If you use jquery ajax get method you can use below code.
$('.del').click(function(){
var x=$(this).attr("id");
jQuery.get("<?php echo Router::url(array('controller'=>'teachers','action'=>'delete'));?>", {"id":x},
function(data,stat){
jQuery('#'+x).remove();
});
});
According to your controller code you can add this like below code
public function delete($id=NUll) {
$id=$_GET['id']; // it's not a good practices you can use $this->request->data/param I mean cakephp syntax.
$this->Teacher->id = $id;
if ($this->Teacher->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
}
return $this->redirect(array('action' => 'index'));
}
This code should work fine.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.