Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

PHP and Ajax form submissions. Succes and Error details - SOLVED ! :D

Hi everyone,

I just got a problem on ajax, I'm a newbie so I think it's a problem of a non-experienced developer more than a big deal. I have this code on my controller:

public function agregartarea(){ //evaluamos los usuarios

        permisos::evaluar('admin');  

        //datos que serán insertados en el array  
        $tareas ['nombre'] = input::post('nombre');
        $tareas ['id_maquina'] = input::post('id_maquina');
        $tareas ['descripcion'] = input::post('descripcion');
        $tareas ['comentarios'] = input::post('comentarios');
        $tareas ['estado'] = input::post('estado');
        $tareas ['fecha_inicio'] = input::post('fecha_inicio');
        $tareas ['fecha_fin'] = input::post('fecha_fin'); 

        //cargamos el modelo y la función del mismo modelo 
        $inserttarea = load::model('tareas');
        if($inserttarea->insertartarea($tareas)) {
            url::redirect('tareas/planning/'.input::post('categoria')); 
        } else {
             echo "There was an error inserting the data"; 
        }
    }

And I have this code into my view :

$("#submit_editar").click(function(e){ if($("#id").val() == ''){ e.preventDefault(); alert("Debes hacer click en alguna tarea"); } else {

                    jQuery.post('http://'+ url +'/prototipo/tareas/editar_tarea_new',                       
                            {
                            id : $("#id").val(),
                            nombre : $("#nombre").val(),
                            id_maquina: $("#maquina").val(),
                            estado : $("#estado").val(),
                            descripcion : $("#descripcion").val(),
                            comentarios : $("#comentarios").val(),
                            fecha_inicio : $("#dia_inicio").val(),
                            fecha_fin: $("#dia_fin").val()
                            }, function(res){}).done(function(){
                                alert("Success")
                            }).fail(function(){
                                alert ("Failure")
                            });                                     
            }           
        });

Well, I got no problem inserting the data into the DB but what I want is ajax to show the correspondent messages for each case: if there is an error updating the data I want Javascript showing the "echo" message from the php script. I've tested it in both ways and, despite php does' not insert a new data into the DB if there is an error, it still show the "success" alert. I've tried to solve it by using $.ajax and its correspondant success and fail options, but it did not work. So if any of you could give a hand? Many thanks! best regards,

Ricardo. p.d. the input::post("value") is because I'm using Dingo Framework. It's the same as $_POST('value'); www.dingoframework.com

Did you solve your problem?

1 Answer