0

i get something problem with send two variable to controller:

this my delete button :

<a href="javascript:void(0);" class="btn btn-danger" data-name="<?php echo $data->opsi_color?>" data-id="<?php echo $data->id_opsi_color?>" onclick="hapus_warna(this)"><i class="glyphicon glyphicon-remove"></i></a>

this javascript :

function hapus_warna(obj)
{
    var id = $(obj).attr('data-id');
    var name = $(obj).attr('data-name');

    if(confirm('Apa anda yakin ingin menghapus '+name+'?')) {
          // ajax delete data from database
          $.ajax({
            url : baseURL + "trueaccon2194/opsional/warna_dihapuskan/" +id +name,
            type: "GET",
            success: function(data) {
               location.reload();
            },
            error: function (jqXHR, textStatus, errorThrown) {
               alert('Error deleting data');
            }
        });
     }
}

i will send var id and var name to controller for I manage this my controller :

function warna_dihapuskan($id,$name){
    $target = $this->input->get('data-name');
    $this->opsional_adm->warna_telah_dihapus($id);
    log_helper("warna", "menghapus warna ".$name."");
}
Maulik Savaliya
  • 1,262
  • 7
  • 17

4 Answers4

1

Instead of this

$.ajax({
        url : baseURL + "trueaccon2194/opsional/warna_dihapuskan/" +id +name,

You should send like this

$.ajax({
        url : baseURL + "trueaccon2194/opsional/warna_dihapuskan/?data-id=" +id +'&data-name='+name,
Saravanan Sampathkumar
  • 3,201
  • 1
  • 20
  • 46
0

Try this

$.ajax({
        url : baseURL + "trueaccon2194/opsional/warna_dihapuskan/?id=" +id +"&name="+name,

Retrieve the parameters like this in your controller function

function warna_dihapuskan(){
    $name = $this->input->get('name');
 $id = $this->input->get('id');

}
Gaya
  • 32
  • 3
0

In your HTML write this

<a href="javascript:void(0);" class="btn btn-danger" data-name="<?php echo $data->opsi_color?>" data-id="<?php echo $data->id_opsi_color?>" onclick="hapus_warna(<?php echo $data->id_opsi_color?>,<?php echo $data->opsi_color?>)"><i class="glyphicon glyphicon-remove"></i></a>

and in the function this

function hapus_warna(id,name)
{


    if(confirm('Apa anda yakin ingin menghapus '+name+'?')) {
          // ajax delete data from database
          $.ajax({
            url : baseURL + "trueaccon2194/opsional/warna_dihapuskan/" +id+"/" +name,
            type: "GET",
            success: function(data) {
               location.reload();
            },
            error: function (jqXHR, textStatus, errorThrown) {
               alert('Error deleting data');
            }
        });
     }
}
jotasi
  • 5,077
  • 2
  • 29
  • 51
0

I really prefer to do

method: 'POST'

Because then I can just do this inside the $ajax() method:

data: {
amount: id,
name: name
}

I do believe in this case it would be more interesting to use POST GET vs. POST ajax requests: When and how to use either?

and in stead of doing an onclick via inline code it is better to separate it from the view:

$(".my_class").on('click', function(){
...
});
Community
  • 1
  • 1
qwertzman
  • 784
  • 1
  • 9
  • 23