Search Data-id and Remove from Database

Asked

Viewed 249 times

0

I got this foreach()

<?php foreach($fotos_ingresso as $valor){ ?>
    <div id="fotos-listagem-imagem">
        <a href="javascript:;" data-toggle="modal" data-target="#confirma-deletar-imagem" data-href="#" data-id="<?php echo $valor->inf_id; ?>" id="deletar-imagem"><img src="<?php echo base_url('assets/uploads/interno_fotos/'.$valor->inf_tipo.'/'.$valor->inf_imagem); ?>" class="img-responsive corte-imagem"></a>
    </div>
<?php } ?>

Inside it, I am passing id="delete-image" and also the data-id.

How do I get the data-id back? I did it this way:

$('#confirma-deletar-imagem').on('show.bs.modal', function(e) {         
    var inf_id = $(this).attr('data-id');
});     

In this case it is returning null. When you click on the image, I open a confirmation via modal... And when you click, you must call Ajax to remove. However, I don’t even know how to call ajax to remove, after clicking on the button called delete-image-modal.

How can I do that?

  • 3

    The attribute id defines a unique element on the page, so it makes no sense to have inside a foreach. See if you can use classes.

  • Foreach() is for me to display the images from the database, so I’m using foreach()...

  • 2

    Yes, the problem is not in using the foreach, but in defining the attribute id of the element within the loop. This would create several elements on the page with the same id and that is not possible because id is unique.

  • But with Class did not give either... I applied the class and put to search in the class but does not return, always returns 1, which is the first ID

  • You can update the question code if you want. Also search to do a [mcve].

  • I suggest also including the ajax code you are using

Show 1 more comment

1 answer

1


I changed your function, and I entered the ajax calling part.

<?php foreach($fotos_ingresso as $valor){ ?>
    <div id="fotos-listagem-imagem">
        <a href="javascript:void(0);" data-toggle="modal" data-target="#confirma-deletar-imagem" 
            data-href="#" data-id="<?php echo $valor->inf_id; ?>">
            <img src="<?php echo base_url('assets/uploads/interno_fotos/'.$valor->inf_tipo.'/'.$valor->inf_imagem); ?>" 
            class="img-responsive corte-imagem">
        </a>
    </div>
<?php } ?>

<hidden id="hiddenValue" />

<script>
    $(function()){
        $('#confirma-deletar-imagem').on('show.bs.modal', function(e) {         
            $("#hiddenValue").val() = $(this).attr('data-id');
        );

        $("#btnRemover").click(function(){
            var data = {
                id_img: $("#hiddenValue").val()
            };

            $.post( "ajax/delete.php", function( data ) {
                //aplicar seu retorno
            });
        });
    };
</script> 
  • Hello, this confirm appears so that I can confirm whether I really want to delete or not. Then clicking the yes button, it will go to the delete action. By following his example, he would go right through, or not?

  • This, I would delete without asking.. I edited my answer, and put a confirm to make the question of actually deleting.

  • This ('show.bs.modal', Function(e) already opens the exclusion modal...

  • Got it, so just put a Hidden in your document to set the temporary value and put the ID of your button so when clicked it runs the post. I already changed the answer.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.