Excluding by button

Asked

Viewed 43 times

1

good morning guys, I’m using js with php, the problem I’m trying is that I’m not getting to understand how to move to the button and the script, follows below to where I left off.

index php.

<button class="fa fa-times btn btn-danger right ml3" id="removerRegistro"></button>

removerRegistro.php

<?php
require "conexao.php";

if(isset($_GET["cli_id"]) && empty($_GET["cli_id"])){

    $cli_id = $_GET["cli_id"]; 

    $conex = new conexao();

    $conex->deleteDesc($_GET["id"]);          

}

Connexion.php

public function deleteDesc($cli_id){
        $sql = $this->conexao->prepare("DELETE FROM descricao WHERE cli_id = :cli_id");
        $sql->bindValue(':cli_id', $cli_id);
        $sql->execute();
    }

Ajax removerRegistro.php

<?php 
require_once "../database/conexao.php";

if(isset($_GET["id"]) && !empty($_GET["id"])){

        $conex = new conexao();

    retun json_encode($conex->deleteDesc($_GET["id"]));
}

script js.

$(document).on('click','#removerRegistro', function(event){
    event.preventDefault();
    var id = $(this).attr('data-id')
})

$.ajax({
    url: 'ajax/removerRegistro.php?id=' + id,
    type: 'GET',
    dataType: 'JSON'
}).done((e) => {

})
  • Your script.js doesn’t make much sense. Is it like that? Because at the click of the button you just assign the variable id, but does nothing with it; moreover you try to use the value of id out of the event to make the requisition and it made no sense.

  • In this condition, isset($_GET["cli_id"]) && empty($_GET["cli_id"]), I believe the ! in front of the empty. Moreover, it is not necessary to use isset and empty together, because the very empty checks if the variable is set. See documentation for details.

1 answer

0

You have to put AJAX inside the event in order to get the variable id and make the request, since, apparently, this should only occur at the click of the button. And left to put the attribute data-id on the button with the id value you want to pass.

The button would be:

<button data-id="VALOR DO ID" class="fa fa-times btn btn-danger right ml3" id="removerRegistro"></button>
         ↑↑↑↑↑

And the event code:

$(document).on('click','#removerRegistro', function(event){
    event.preventDefault();
    var id = $(this).data('id');

   $.ajax({
       url: 'ajax/removerRegistro.php?id=' + id,
       type: 'GET',
       dataType: 'JSON'
   }).done((e) => {

   })
})

No need to put type: 'GET',. GET is already the default AJAX method.

  • still nothing solved... I packed some things, but this not finding the id...

  • Hi Fernando. Where’s the id you want to pick up? No button has no attribute data-id.

  • <button class="fa fa-times btn btn-Danger right ml3" id="removerRegistro" v-bind:data-id="Description.cli_id"></button>

Browser other questions tagged

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