Welcome Guest, Not a member yet? Register   Sign In
Ajax if no result return empty
#1

When I need to delete a file I delete it via ajax.

Lets say I have two files I can delete the second one OK. But then when I want to delete the last file it deletes it from the database table and uploads folder OK.

However it still shows the very last result on list even though there is no record of it any more.

How can I make sure that when I delete the last result that the ajax will remove the last result 

I did think about using $('#file-attachments tbody').html(""); But not sure where to place it if can not find any more results.


Code:
<script type="text/javascript">
$(document).ready(function() {

    $(document).on('click', '#delete_button', function () {
         $.ajax({
            url: "<?php echo base_url('extensions/attachments/delete');?>",
            type: 'post',        
            dataType: 'json',
            data: {
                attachment_id: $(this).attr("data-id"),
                posthash: $('#posthash').val()
            },
            success: function(json) {
                if (json['success'] == true) {
                    $.each(json['attachments'], function( key, value ) {
                        info = '<tr>';
                        info += '<td>';
                        info += value['orig_name'];
                        info += '</td>';
                        info += '<td>';
                        info += value['file_size'] + 'KB';
                        info += '</td>';
                        info += '<td class="text-center">';
                        info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                        info += '</td>';
                        info += '</tr>';
                    });    

                    $('#file-attachments tbody').html(info);
                }
            },
        });
    });


    $('#add_attachment').on('click', function() {
        
    $('#form-upload').remove();

    $('body').prepend('<form enctype="multipart/form-data" id="form-upload" style="display: none;"><input type="file" name="file" /></form>');

    $('#form-upload input[name=\'file\']').trigger('click');
        
    $('#form-upload input[name=\'file\']').on('change', function() {
            
        var formData = new FormData($(this).parent()[0]);
        formData.append('posthash', $("#newreply #posthash").val());

        $.ajax({
            url: "<?php echo base_url('extensions/attachments/upload');?>",
            type: 'post',        
            dataType: 'json',
            data: formData,
            cache: false,
            contentType: false,
            processData: false,        
            success: function(json) {
                if (json['error']) {
                    alert(json['error']);
                }
                            
                if (json['success']) {
                        
                    $.each(json['attachments'], function( key, value ) {
                        info = '<tr>';
                        info += '<td>';
                        info += value['orig_name'];
                        info += '</td>';
                        info += '<td>';
                        info += value['file_size'] + 'KB';
                        info += '</td>';
                        info += '<td class="text-center">';
                        info += '<button type="button" id="delete_button" class="btn btn-danger" data-id="' + value['attachment_id'] +'"><i class="fa fa-trash-o" aria-hidden="true"></i></button>';
                        info += '</td>';
                        info += '</tr>';
                    });    

                    $('#file-attachments tbody').append(info);
                }
            },            

            });
        
        });
    });    
});

</script>


Controller Function
PHP Code:
public function delete()
{

    
$data = array('success' => false'attachments' => '');

 
   if ($this->input->post('attachment_id'))
 
   {
 
       $data['success'] = true;

 
       $get $this->attachment_model->getattachment($this->input->post('attachment_id'));

 
       if (unlink(FCPATH 'uploads/' $get['path']))
 
       {
 
          $this->attachment_model->deleteattachment($this->input->post('attachment_id'));
 
       }

 
       $attachments_results $this->attachment_model->getattachmentsfornewreply($get['posthash']);

 
       foreach ($attachments_results as $attachment)
 
       {
 
           $data['attachments'][] = array(
                
'attachment_id' => $attachment['attachment_id'],
                
'post_id' => $attachment['post_id'],
                
'posthash' => $attachment['posthash'],
                
'file_name' => $attachment['file_name'],
                
'orig_name' => $attachment['orig_name'],
                
'file_size' => $attachment['file_size'],
 
               'path' => $attachment['path'
 
           );
 
       }
 
   }

 
   echo json_encode($data);

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply


Messages In This Thread
Ajax if no result return empty - by wolfgang1983 - 06-28-2017, 03:26 AM
RE: Ajax if no result return empty - by PaulD - 06-28-2017, 11:20 AM
RE: Ajax if no result return empty - by Wouter60 - 06-28-2017, 11:36 AM



Theme © iAndrew 2016 - Forum software by © MyBB