rhog23
rhog23

Reputation: 321

How to use jquery-confirm in codeigniter?

I've been trying to use jquery-confirm that is provided by https://craftpip.github.io/jquery-confirm/#ajaxloading . There's no error, but once I tried to delete an, item, the jquery pops up an alert, but when I click "okay" it doesn't delete the item..I don't know where the mistake is. Thanks for the help

This is the php code:

<div class="box">
    <div class="box-body">
        <a href="<?php echo base_url(); ?>admin/inputrequest" class="btn bg-green btn-flat">Input Request</a>
        <br><br>
        <table id="tabelrequest" class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Item Code</th>
                    <th>Description</th>
                    <th>Qty</th>
                    <th>Delete</th>
                    <th>Edit</th>
                    <th>Photo</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    $no = 1;
                    foreach($allrequest as $request)
                    {
                        $delete_url = base_url().'admin/deleterequest/'.$request->id_request;
                        $update_url = base_url().'admin/updaterequest/'.$request->id_request;
                        $add_url = base_url().'admin/datafotorequest/'.$request->id_request;
                ?>
                        <tr>
                            <td><?php echo $no; ?></td>
                            <td><?php echo $request->item_code; ?></td>
                            <td><?php echo $request->description; ?></td>
                            <td><?php echo $request->qty; ?></td>
                            <td class="col-xs-1" style="text-align: center;"><a class="btn bg-olive btn-flat" class="delete" data-title="Are you sure you want to delete this item?" name="delete" href="<?php echo $delete_url; ?>"><i class="glyphicon glyphicon-trash"></i></a></td>
                            <td class="col-xs-1" style="text-align: center;"><a class="btn bg-teal btn-flat" name="update" href="<?php echo $update_url; ?>"><i class="glyphicon glyphicon-pencil"></i></a></td>
                            <td class="col-xs-1" style="text-align: center;"><a class="btn bg-red btn-flat" name="add" href="<?php echo $add_url; ?>"><i class="glyphicon glyphicon-camera"></i></a></td>
                        </tr>
                <?php
                        $no++;
                    }            
                ?>
            </tbody>
        </table>    
    </div>
</div>

And this is the jquery I took from jquery-confirms.js

<script>

    $('#delete').confirm({
        content: "",
    });
    $('#delete').confirm({
        buttons: {
            hey: function(){
                location.href = this.$target.attr('href');
            }
        }
    });

</script>

Upvotes: 1

Views: 368

Answers (3)

Abdulla Nilam
Abdulla Nilam

Reputation: 38584

Instead of this

<td class="col-xs-1" style="text-align: center;">
    <a class="btn bg-olive btn-flat" class="delete" data-title="Are you sure you want to delete this item?" name="delete" href="<?php echo $delete_url; ?>">
        <i class="glyphicon glyphicon-trash"></i>
    </a>
</td>

I'll use (onclick="return confirm()")

<td class="col-xs-1" style="text-align: center;">
    <a class="btn bg-olive btn-flat" class="delete" onclick="return confirm('Are you sure you want to delete this item?');"  href="<?php echo $delete_url; ?>">
        <i class="glyphicon glyphicon-trash"></i>
    </a>
</td>

Upvotes: 1

Swapnil Kumbhar
Swapnil Kumbhar

Reputation: 460

You need to create ajax request

HTML

<td class="col-xs-1" style="text-align: center;"><a class="btn bg-olive btn-flat" class="delete" data-title="Are you sure you want to delete this item?" name="delete" href="#" onclick="deleteRecord('<?php echo $request->id_request;?>');"><i class="glyphicon glyphicon-trash"></i></a></td>

AJAX

function deleteRecord(id)
{

            jQuery.ajax({
                type: "GET",
                url: '/admin/deleterequest/'+id,
                async: false,
                success: function (data)
                {
                    alert('success');
                }
            });

}

Upvotes: 0

Jinesh
Jinesh

Reputation: 1580

I think you have to use like this

<div class="box">
    <div class="box-body">
        <a href="<?php echo base_url(); ?>admin/inputrequest" class="btn bg-green btn-flat">Input Request</a>
        <br><br>
        <table id="tabelrequest" class="table table-bordered table-striped">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Item Code</th>
                    <th>Description</th>
                    <th>Qty</th>
                    <th>Delete</th>
                    <th>Edit</th>
                    <th>Photo</th>
                </tr>
            </thead>
            <tbody>
                <?php
                    $no = 1;
                    foreach($allrequest as $request)
                    {
                        $delete_url = base_url().'admin/deleterequest/'.$request->id_request;
                        $update_url = base_url().'admin/updaterequest/'.$request->id_request;
                        $add_url = base_url().'admin/datafotorequest/'.$request->id_request;
                ?>
                        <tr>
                            <td><?php echo $no; ?></td>
                            <td><?php echo $request->item_code; ?></td>
                            <td><?php echo $request->description; ?></td>
                            <td><?php echo $request->qty; ?></td>
                            <td class="col-xs-1" style="text-align: center;"><a class="btn bg-olive btn-flat delete"  data-title="Are you sure you want to delete this item?" name="delete" href="<?php echo $delete_url; ?>"><i class="glyphicon glyphicon-trash"></i></a></td>
                            <td class="col-xs-1" style="text-align: center;"><a class="btn bg-teal btn-flat" name="update" href="<?php echo $update_url; ?>"><i class="glyphicon glyphicon-pencil"></i></a></td>
                            <td class="col-xs-1" style="text-align: center;"><a class="btn bg-red btn-flat" name="add" href="<?php echo $add_url; ?>"><i class="glyphicon glyphicon-camera"></i></a></td>
                        </tr>
                <?php
                        $no++;
                    }            
                ?>
            </tbody>
        </table>    
    </div>
</div>

<script>

    $('.delete').confirm({
        content: "",
    });
    $('.delete').confirm({
        buttons: {
            hey: function(){
                location.href = this.$target.attr('href');
            }
        }
    });

</script>

Upvotes: 1

Related Questions