Problems to remove a record from a table with ajax?

0

You see I am trying to delete the record of a table with Ajax, this table is made through DataTable, however the delete button does not want to work, it throws me the following error:

    ReferenceError: BorrarSeccion is not defined

Here I leave my code:

Javascript code to load table and Delete button

      $(document).ready(function() {

         listarSeccion();

          function BorrarSeccion(id)

       {

       bootbox.confirm("¿Esta Seguro que quiere eliminar esta Seccion 
          Electoral?", function(result){
          if(result){
           $.post("../Controlador/EliminarSeccion.php", {id : id}, function(e){
              console.log(e);


                }); //fin del ajax
            } //primer if

            });

          } //fin de la funcion




           });

           var listarSeccion = function() {
            var table = $("#tablaSeccion").DataTable({
            "ajax": {
           "method": "POST",
            "url": "../Controlador/ListarSecciones.php",

              },
             "columns": 
              [
              {"data": "Nombre_Sec"},
              {"data": "Acciones"}
                ]
               });

                }

php code that lists the records:

    session_start();

                        include_once "../Modelo/conexion2.php";
                        include_once "../Modelo/Secciones.php";

                $objSeccion = new Secciones();

                            $resultado = $objSeccion->Listar();

                    $arreglo = Array();

                    while ($data = $resultado->fetch_object()) 

                    {


                    $arreglo ["data"][] = array( 
                 "Nombre_Sec" => $data->Seccion,
                 "Acciones" => '<a href="EditarSeccionVista.php?idSec=' . $data->idsec . '"><i class="fa fa-edit" id="iconoEditar"></i></a>'.'<button type="button" class="btn btn-primary" onclick="BorrarSeccion('.$data->idsec.')" id="modalSeccion"><i class="fa fa-edit" id="iconoBorrar"></i> </button>');
                    }

                echo json_encode($arreglo);

                    mysqli_free_result($resultado);
                    mysqli_close($conexion); 

The data is loaded into the table perfectly. The functionality to edit the records works perfectly. The problem is when it comes to eliminating. The alert is not displayed and I get that error in console. I try to use a bootstrap modal, it opens but still shows me the same error that DeleteSection is not defined and does not delete anything since the idsec is never loaded on the button ... What I want is to be able to erase the registry without having to be jumping from page to page, but doing it with ajax.

Previously I have done this type of procedure, and it works well, without problems I delete records, but this time I get this error but I can not see where the problem is.

Could you give me a hand?

---------------------------------- SOLUTION ------------ -------------------------

The error was in the javascript code. The function DeleteSection (id) was moved out of $ (document) and the code works perfectly, thanks to the contribution of the user mentioned in the comments.

corrected javascript code

      $(document).ready(function() {

         listarSeccion();

           });

           var listarSeccion = function() {
            var table = $("#tablaSeccion").DataTable({
            "ajax": {
           "method": "POST",
            "url": "../Controlador/ListarSecciones.php",

              },
             "columns": 
              [
              {"data": "Nombre_Sec"},
              {"data": "Acciones"}
                ]
               });

                }

            function BorrarSeccion(id)

       {

       bootbox.confirm("¿Esta Seguro que quiere eliminar esta Seccion 
          Electoral?", function(result){
          if(result){
           $.post("../Controlador/EliminarSeccion.php", {id : id}, function(e){
              console.log(e);


                }); //fin del ajax
            } //primer if

            });

          } //fin de la funcion
    
asked by RolandF 30.05.2018 в 20:03
source

0 answers