Foros del Web » Programando para Internet » Jquery »

Ajax anidado ejecuta doble trile ves php

Estas en el tema de Ajax anidado ejecuta doble trile ves php en el foro de Jquery en Foros del Web. Hola amigos tengo un problema no se donde esta la falla tengo cuatro archivos para una tabla profesion, un modelo, un controlador , un js ...
  #1 (permalink)  
Antiguo 18/06/2014, 10:10
 
Fecha de Ingreso: mayo-2007
Mensajes: 16
Antigüedad: 16 años, 10 meses
Puntos: 0
Exclamación Ajax anidado ejecuta doble trile ves php

Hola amigos tengo un problema no se donde esta la falla
tengo cuatro archivos para una tabla profesion, un modelo, un controlador , un js con ajax y un archivo listar

mi ajax lo hice anidado es este:

Código:
 //INSERTAR REGISTRO
    $('#enviar').on('click', function() {
        $('#frmagregar').on('submit', function(event) {
            event.preventDefault();
            var nombre = $('#nombre').val();
            $.ajax({
                type: 'POST',
                /*dataType: 'json',*/
                url: '../../controller/acciones_profesion.php',
                data: {accion: 1, nombre: nombre},
                success: function(data) {
                    if (data != true)
                    {
                        $('.bad').fadeIn('slow');
                        setTimeout(function() {
                            $('.bad').fadeOut(500);
                        }, 2000);
                    }
                    else
                    {
                        $('.good').html("Editado Correctamente...!!!");
                        $('.good').fadeIn('slow');
                        $('.datos,.option').show();
                        $('#agregar').hide();
                        $('.good').fadeOut(600);
                            
                        $.ajax({
                            type: 'POST',
                            url: 'listarprofesion.php',
                            data:{tablas:1},
                            success: function(data) {
                                $('#datagrid').html(data);
                                $('#datatable').dataTable({
                                    "sPaginationType": "full_numbers",
                                    "aaSorting": [[2, "desc"]],
                                    "bJQueryUI": false
                                });
                            }
                        });
                        event.preventDefault();
                    }

                }
            });
        });
    });
como ven primero llamo a un controlador "acciones profesion php" que se encarga de insertar

archivo acciones_profesion.php, tiene este codigo cuando "accion"=1, le digo que inserte
Código PHP:
include('../model/profesion.model.php');
$p = new Profesion();
if (
$_POST != null && $_GET == null) {
    
$accion $_POST['accion'];

    if (
$accion == 1) {//INSERTAR
        
$nombre $_POST['nombre'];
        
$result $p->registrar(array($nombre));
        if (
$result) {
            echo 
true;
        } else
            echo 
false;
    }
}
?> 
si se inserto correctamente, entonces segun mi codigo ajax de arriba, quiero que lo muestre otra ves con ajax y llamo al archivo listar

listar.php es esto:
Código PHP:
<?php
    
require '../../model/profesion.model.php';
    
$profesion = new Profesion();
    
$lista $profesion->listar();
    
?>

<table class="table hovered border myClass" id="datatable">
            <thead>
                <tr>
                    <th>ID de profesion</th>
                    <th>Nombre de profesion</th>
                    <th>Editar</th>
                    <th>Eliminar</th>
                </tr>
            </thead>
            <tbody>
                <?php
                
while ($fila mysql_fetch_row($lista)) {
                    
?>
                    <tr id="fila-<?php echo $fila[0?>">
                        <td class="text-center"><?php echo $fila[0?></td>
                        <td class="text-center"><?php echo $fila[1?></td>
                        <td class="text-center">
                            <a href="../../controller/acciones_profesion.php?accion=3&id=<?php echo $fila[0?>" title="Editar" data-hint-position="top" class="editar_cat" >
                                <img src="../../images/database_edit.png" width="16" height="16">
                            </a>
                        </td>
                        <td class="text-center">
                            <a href="#"
                               onClick="EliminarDato(<?php echo $fila[0?>);
                                       return false" >
                                <img src="../../images/delete.png" width="16" height="16">
                            </a>
                        </td>
                    </tr>
                    <?php
                
}
                
?>
            </tbody>
        </table>
para que se entienda mejor, también dejare el código del modelo:
profesion_model.php, la parte de registrar

Código PHP:
function registrar($data) {
        if (
$this->estado) {
            
$result mysql_query("insert into profesion (nombre) values ('$data[0]')");

            if (
$result)
                return (
$result);
            else
                return 
true;
        }
    } 
El problema es que en el formulario, la primera que escribo algo lo inserta bien, pero la segunda ves inserta doble, la tercera triple, la cuarta 4 veces, todo es sin recargar la web. Pero cuando doy recargar la web, empieza nuevamenete lo mismo desde 1 ves, la segunda inserta doble, la tercera triple y así sucesivamente.

Alguien podria ayudarme porfavor, no se donde esta mi error. Gracias
  #2 (permalink)  
Antiguo 18/06/2014, 10:57
 
Fecha de Ingreso: mayo-2007
Mensajes: 16
Antigüedad: 16 años, 10 meses
Puntos: 0
Respuesta: Ajax anidado ejecuta doble trile ves php

Veo que mi problema no es el ajax anidado, por que teniendo solo esto

Código:
$('#enviar').on('click', function() {
        $('#frmagregar').on('submit', function(event) {
            event.preventDefault();
            var nombre = $('#nombre').val();
            $.ajax({
                type: 'POST',
                url: '../../controller/acciones_profesion.php',
                data: {accion: 1, nombre: nombre},
                success: function(data) {
                    if (data=true)
                    {
                        $('.good').html("Registro Exitoso...!!!");
                        $('.good').fadeIn('slow');
                        $('.datos,.option').show();
                        $('.good').fadeOut('slow');
                        $('#agregar').hide();
                        
                    }
                    else
                    {
                        $('.bad').fadeIn('slow');
                        setTimeout(function() {
                            $('.bad').fadeOut(500);
                        }, 2000);
                    }
                }
            });
        });
    });
igual registrad odble, triple, cudruple etc...
  #3 (permalink)  
Antiguo 18/06/2014, 11:35
 
Fecha de Ingreso: mayo-2007
Mensajes: 16
Antigüedad: 16 años, 10 meses
Puntos: 0
Respuesta: Ajax anidado ejecuta doble trile ves php

Veo que mi problema no es el ajax anidado, por que teniendo solo esto

Código:
$('#enviar').on('click', function() {
        $('#frmagregar').on('submit', function(event) {
            event.preventDefault();
            var nombre = $('#nombre').val();
            $.ajax({
                type: 'POST',
                url: '../../controller/acciones_profesion.php',
                data: {accion: 1, nombre: nombre},
                success: function(data) {
                    if (data=true)
                    {
                        $('.good').html("Registro Exitoso...!!!");
                        $('.good').fadeIn('slow');
                        $('.datos,.option').show();
                        $('.good').fadeOut('slow');
                        $('#agregar').hide();
                        
                    }
                    else
                    {
                        $('.bad').fadeIn('slow');
                        setTimeout(function() {
                            $('.bad').fadeOut(500);
                        }, 2000);
                    }
                }
            });
        });
    });
igual registrad odble, triple, cudruple etc...
  #4 (permalink)  
Antiguo 18/06/2014, 11:40
 
Fecha de Ingreso: mayo-2007
Mensajes: 16
Antigüedad: 16 años, 10 meses
Puntos: 0
Respuesta: Ajax anidado ejecuta doble trile ves php

Ya lo soluciones, el problema es que por jquery hacia Onclik en el boton del formulario y también, onsubmit, es decir esccuhava cada evento, pero simplemente cambie el boton de submit a button y elimine el jquery submit, quedando asi el codigo:

POr si alguien le interese, o en algun momento tenga el mismo problemilla:

Código HTML:
$('#enviar').on('click', function() {
        var nombre = $('#nombre').val();
        $.ajax({
            type: 'POST',
            url: '../../controller/acciones_profesion.php',
            data: {accion: 1, nombre: nombre},
            success: function(data) {
                if (data = true)
                {
                    $('.good').html("Registro Exitoso...!!!");
                    $('.good').fadeIn('slow');
                    $('.datos,.option').show();
                    $('.good').fadeOut('slow');
                    $('#agregar').hide();
                    
                    $.ajax({
                            type: 'POST',
                            url: 'listarprofesion.php',
                            data:{tablas:1},
                            success: function(data) {
                                $('#datagrid').html(data);
                                
                                $('#datatable').dataTable({
                                    "sPaginationType": "full_numbers",
                                    "aaSorting": [[2, "desc"]],
                                    "bJQueryUI": false
                                });
                            }
                        });
                }
                else
                {
                    $('.bad').fadeIn('slow');
                    setTimeout(function() {
                        $('.bad').fadeOut(500);
                    }, 2000);
                }
            }
        });
    });

Etiquetas: ajax, ajax-php-mysql, php
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 02:42.