Ver Mensaje Individual
  #1 (permalink)  
Antiguo 18/06/2014, 10:10
vlademirss
 
Fecha de Ingreso: mayo-2007
Mensajes: 16
Antigüedad: 16 años, 11 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