Welcome Guest, Not a member yet? Register   Sign In
Yet another form validation question ... You know you can help! :)
#1

[eluser]winterain[/eluser]
Is it possible to pass a success/false from a controller back into the ajax.updater that calls it ?

This ajax.updater calls the controller upon form submit:
Code:
function comment() {
    var params = Form.serialize($('form_comment'));
    new Ajax.Updater ('comm_f_status', base_url+'/sendcomment', {method:'post', parameters:params});
    new Effect.Appear('comm_f_status');
    Form.reset($('form_comment'));
}

What I have in the controller is like this,
Code:
if ($this->form_validation->run() == TRUE)
    {
        $this->Songsdb->insertComment($data);
        return $success = TRUE;
        echo 'Your Comment has been added! :)';
    } else
        
    if ($this->form_validation->run() == FALSE)
    {
        return $success = FALSE;
        echo validation_errors();
    }

I want the Form.reset to only occur if the insert was successful, and not if there are errors.. I don't want to reload the page.

Is this the way to go about this type of functionality? Help ?
#2

[eluser]darkhouse[/eluser]
I think you may need to rework your ajax. Instead of calling updater, is there a way to just run a callback function? If so, you could pass the success result to the ajax callback and then reset the form if it's true.

This is prototype right? I'm sort of guessing because I don't use prototype myself. I normally use jQuery for everything, though my colleague found some issues with jQuery's ajax stuff. If you're returning xml, jQuery doesn't properly create the DOM object in Safari (I think that's what the issue was), so we ended up just creating our own ajax system that works how we want and allows us to write things however we want.
#3

[eluser]winterain[/eluser]
As of yesterday, I was using prototype.. as of today, I've switched to jquery completely because it is much simpler to use and noob-friendly. haha.. My problem persists though, this is how I'm handling the ajax..

Code:
var params = $("form#form_comment").serialize();
    $.ajax({
            type: "POST",
            url: base_url+"/music/sendcomment",
            data: params,
            success: function(a){
                $("form#form_comment")[0].reset();
                $("#comm_f_status").hide();
                $("#comm_f_status").html(a);
                $("#comm_f_status").fadeIn('slow');
            }
        });

i figured as long as I can sneak a variable out from Controller and reference it here, I can just add an if statement. Thing is my Controller is echoing the success/failure message.
if I return an object, how can I use it in jquery ?

ie:

Controller:
Code:
$data['msg'] = 'successful!!!';
$data['success'] = TRUE;

back in jquery, it will get that object as ' a ' but how do i get the values inside ?
#4

[eluser]srisa[/eluser]
Code:
if ($this->form_validation->run() == TRUE)
    {
        $this->Songsdb->insertComment($data);
        $ret = array();
        $ret['status'] = 0; // success.
        $ret['message'] = 'Your Comment has been added! :)';
        echo json_encode($ret);
    } else
        
    if ($this->form_validation->run() == FALSE)
    {
        $ret = array();
        $ret['status'] = -1; //error.
        $ret['message'] = validation_errors();
        echo json_encode($ret);  
    }
In your jQuery:
Code:
var params = $("form#form_comment").serialize();
    $.ajax({
            type: "POST",
            url: base_url+"/music/sendcomment",
            data: params,
            dataType: "json",
            success: function(a){
                if (a.status == 0){
                   //success.
                }
                else{
                   //failure.
                }
            }
        });
I had no problems using this approach on firefox but IE was caching the ajax responses. To get around that I sent the no-cache headers from the controller.




Theme © iAndrew 2016 - Forum software by © MyBB