This is not working in my CodeIgniter function. As I have the id and cannot get that id in my controller function. I am puzzled how to do it in Javascript and Ajax way.
This is my view script
<script>
function delete_img_fn(id)
{
alert(id); // It gives '1','2',etc on clicking every row in table
var r=confirm("Do you want to Delete");
if (r==true)
{
$.post("<?php echo site_url('/admincontent/delete_portfolioimg/');?>", {id:id},
function(data) {
alert(data+"a");
}, 'json');
}
else
{
x="You pressed Cancel!";
alert(x);
}
}
</script>
and i am trying to send my id through the controller function as follows:
public function delete_portfolioimg()
{
echo $this->input->post('id');exit('I am trying in controller function');
}
The above is not working. I also tried the below code:
window.location.href = "<?php echo site_url('/admincontent/delete_portfolioimg/'.id);?>'?id="+id;
window.location.href = "<?php echo site_url('/admincontent/delete_portfolioimg/'.id);?>";
But neither worked... It would also help me if someone guide me how to do in ajax way out!
View :-
<a><img src="<?php echo base_url();?>adminassets/images/icons/deletered.png" alt="Alternate Text" onclick="del(<?php echo $result['image_id'];?>)"/> </a>
ajax code is:
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js">
</script>
<script>
function del(id)
{
if (confirm("Are you sure?")) {
$.ajax({
url: "<?php echo site_url(); ?>/admincontent/delete_portfolioimg/" + id,
type: 'POST',
data: {"id":id},
dataType: 'json',
success: function(response) {
alert('Image is deleted successfully now');
},
error: function () {
alert('Could not delete image');
}
});
} else {
alert(id + " not deleted");
}
}
Controller code is:
public function delete_portfolioimg()
{
$id = $this->input->post('id');
//print_r($id);exit('Dell');
$res = $this->admin_model->ch_portfolioimg($id);
echo json_encode($res);
}
And model code is:
public function ch_portfolioimg($id)
{
$this->db->select('*');
$this->db->from('portfolio_img');
$this->db->where('image_id', $id); $res = $this->db->get()->row_array(); //echo '<pre>';print_r($res);exit('Change Portfolio Image');
if(!empty($res))
{
unlink($_SERVER['DOCUMENT_ROOT'].'/NetSach/assets/images/'.$res['image_url']);
}
$this->db->where('image_id', $id);
$this->db->delete('portfolio_img');
return true;
}