简体   繁体   中英

How to call href function with ajax

I'm new here, i'm working with ajax and notify.js on my website. Where there are a slight problem, which is i can't call my function using href with ajax and the notify.js won't pop up and delete the file Let me show you my code

Views:

<tbody>
<?php foreach ($rows as $row) { ?>
    <tr>
        <td><?php echo $row ->description ?></td>
        <td><?php echo $row->updated_at ?></td>
        <td><a href="<?php echo base_url().'admin/edittipe/'.$row->id ?>">Edit</a></td>
        <td><a id="aDeleteOrderType" onclick="type_delete()" href="#.<?php echo $row->id ?>">Delete</a></td>
    </tr>
<?php } ?> 

Admin Footer

<script>
$("#aDeleteOrderType").click(function(e){
    e.preventDefault();
    $.ajax({
      url: '<?php echo base_url().'admin/type_delete' ?>',
      type: 'post',
      data: {
      },
      success: function(msg)
      {
          if (msg == 'valid')
        {
            $.notify('Data Has Been Deleted', 'error')
        }
      }
}); 

});

Controller

public function type_delete($id)
    {
        $is_logged_in1 = $this->session->userdata('is_logged_in');
        $type = $this->session->userdata('type');
        if(!isset($is_logged_in1) || $is_logged_in1 != true)
    {
        $data['error'] = '';
        $this->load->view('login-1', $data);
    }
    else                     
    {
        $this->load->model('listtipeorder_model');
        $this->listtipeorder_model->delete_list_type($id);
        echo 'valid';
    }
    }

Help me guys please:(

PS sorry for my bad english though.

HTML:

Notice data-id , and attribute we will get soon.

<td><a id="aDeleteOrderType" href="#" data-id="<?php echo $row->id ?>">Delete</a></td>

JS:

Since you aren't using get or post to get the $id var in your php function and instead are passing it as a url param you have to do the same via jquery.

<script>
    $(document).ready(function () {
        $("#aDeleteOrderType").click(function (e) {
            var id = $(this).attr('data-id');
            e.preventDefault();
            $.ajax({
                url: '<?php echo base_url() . '/admin/type_delete/'; ?>' + id,
                type: 'POST',
                success: function (msg) {
                    if (msg == 'valid') {
                        // success would be better suited than 'error'
                        // might confuse some
                        $.notify('Item deleted', 'success');
                    } else {
                        $.notify('Error occurred', 'error');
                    }
                }
            });
        });
    });
</script>

PHP:

There isn't anything wrong with your PHP, but it can be improved.

function type_delete($id = null) {
    $is_logged_in1 = $this->session->userdata('is_logged_in');
    //$type = $this->session->userdata('type');
    // isset not required with userdata as will return null if no data
    if ($is_logged_in1 != true || is_null($id)) {
        echo 'error';
        // don't load a view
        //$data['error'] = '';
        //$this->load->view('login-1', $data);
    } else {
        $this->load->model('listtipeorder_model');
        $this->listtipeorder_model->delete_list_type($id);
        echo 'valid';
    }
    exit;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM