Function JS runs only once

0

My problem is that my JS function only works for me the first time I run it and if I try to execute it a second time it gives me this error:

<script type="text/javascript">

        $(document).ready(function(){

            $('#btnActualizar').click(function(){
                actualizarHabitacion();
            });

        });

    </script>
function actualizarHabitacion(){

    id=$('#txtIdUp').val(); 
    tip=$('#editTipo').val();
    capadu=$('#editCapAdu').val();
    capnin=$('#editCapNin').val();
    capmax=$('#editCapMax').val();
    valadu=$('#editValAdu').val();
    valnin=$('#editValNin').val();
    des=$('#editDescripcion').val();

    actualizarHabitacion = "";
    $.ajax({
        type:"POST",
        url:"../php/updates.php",
        data: {id,tip,capadu,capnin,capmax,valadu,valnin,des,actualizarHabitacion},
        success:function(r){
            if(r==1){
                $('#tabla').load('componentes/tabla_habitaciones.php');
                alertify.success("Habitacion Actualizada con exito");
            }else{
                alertify.error("Fallo el servido");
            }
        }
    });
}

The only way to re-execute it is to reload the page, which I do not want. It would be appreciated if you could give me some advice:)

    
asked by martinfcamposc 16.12.2018 в 05:30
source

2 answers

0

The error is not in the sending of data but in the overwriting of the function actualizarHabitacion , since the function is defined in the global scope and you do not define a variable actualizarHabitacion when you do this actualizarHabitacion = ""; you say to your program that now the function actualizarHabitacion is a string and the second time you call the function no longer exists as a function but as a string and actualizarHabitacion() is not valid.:

function actualizarHabitacion(){

    id=$('#txtIdUp').val(); 
    tip=$('#editTipo').val();
    capadu=$('#editCapAdu').val();
    capnin=$('#editCapNin').val();
    capmax=$('#editCapMax').val();
    valadu=$('#editValAdu').val();
    valnin=$('#editValNin').val();
    des=$('#editDescripcion').val();

    actualizarHabitacion = ""; <---------cambio en la definicion de la funcion
    $.ajax({
        type:"POST",
        url:"../php/updates.php",
        data: {id,tip,capadu,capnin,capmax,valadu,valnin,des,actualizarHabitacion},
        success:function(r){
            if(r==1){
                $('#tabla').load('componentes/tabla_habitaciones.php');
                alertify.success("Habitacion Actualizada con exito");
            }else{
                alertify.error("Fallo el servido");
            }
        }
    });
}
    
answered by 16.12.2018 / 18:17
source
0

Solved ... the error was caused by the data sent by ajax, I put them all in a chain and the problem was fixed:

function actualizarHabitacion(){

    id=$('#txtIdUp').val(); 
    tip=$('#editTipo').val();
    capadu=$('#editCapAdu').val();
    capnin=$('#editCapNin').val();
    capmax=$('#editCapMax').val();
    valadu=$('#editValAdu').val();
    valnin=$('#editValNin').val();
    des=$('#editDescripcion').val();

    cadena= "id=" + id +
            "&tip=" + tip +
            "&capadu=" + capadu + 
            "&capnin=" + capnin +
            "&capmax=" + capmax + 
            "&valadu=" + valadu + 
            "&valnin=" + valnin +
            "&des=" + des +
            "&actualizarHabitacion=" + "";

    $.ajax({
        type:"POST",
        url:"../php/updates.php",
        data: cadena,
        success:function(r){
            if(r==1){
                $('#tabla').load('componentes/tabla_habitaciones.php');
                alertify.success("Habitacion Actualizada con exito");
            }else{
                alertify.error("Fallo el servido");
            }
        }
    });
}
    
answered by 16.12.2018 в 08:56