views:

714

answers:

2

I'm trying to send a form with mootools and depending on the response from the php script do such or such thing. The thing is I fail to be able to use conditional statements inside of the onComplete part of the code.

I'm missing something really obvious probably, please help me :)

$('formName').addEvent('submit', function(e){
    e.stop();                      
    var req = new Request.HTML({
      url       : 'phpUrl.php',
      data      : $('formName'),
      update    : $('modify-me'),
      onComplete: function(){
            $('formName').reset();
            if($('modify-me').get('text') = "1"){
                alert("succeed");
                }else{
                alert("failure");
                }
            }  
    }).send();                    
  });

This was my lame attempt to use the php response in the code, but of course it didn't work. Needless to say I'm new to all this asynchronous client-server communication stuff, but I'm really intrigued by it.

+2  A: 

You are assigning in your if statement (single =), not checking equality (==). Change the line

if($('modify-me').get('text') = "1")

to

if($('modify-me').get('text') == "1")
Macros
Nice, +1 here.But isn't there a way to not have to assign the response to an element before using it's value in the conditional statements?I mean, shouldn't there be a way to discriminate from success and failure without doing that ugly thing?
johnnyArt
I'm not sure I understand - I've not used MooTools but a Google seems to suggest that onComplete will only fire once the response has completed succesfully?
Macros
completing successfully doesn't mean a particular response from the php script, that's what I fail to achieve. I would like to use the value of the response directly without adding it to an element before.
johnnyArt
From the MooTools API: http://mootools.net/docs/core/Request/Request.HTML. Looks like onComplete has been replaced with onSuccess, and you can access the reponseHTML (amongst other things) within the function
Macros
onSuccess is different from onComplete
Thorpe Obazee
Would onSuccess not be the correct event though? The request needs to return successfully in order to inspect the returned value(s)
Macros
@Macros, you should use the parameters of your callback, onComplete: function(data){if(data == 1)}
Thorpe Obazee
A: 

Sorry guys, maybe I'm late... I'm working on Mootools 1.2.4 for the client side and on PHP for the backend. This is the way I submit forms and get response from the server...

    $('myFormID').set('send', {
        noCache: true,
        onRequest: function(){
            // show some rotating loader gif...
        },
        onComplete: function(response) {
            // hide the loader gif...
            objJson = JSON.decode(response);
            if(objJson.success){
                // do your success stuff...
                alert(objJson.msg);
            } else {
                alert(objJson.msg);
            }
        },
        onFailure: function(){
            alert("Request Aborted.");
        }
    }).send();

In my case the form submit is triggered by a button, but could be whatever... let's look at the server side (I use PHP but any other language is good)

    if($somethingWentWrong){
    echo json_encode(array("success" => false, "msg" => "Check your form."));
} else {
    echo json_encode(array("success" => true, "msg" => "Data saved."));
}

after all the server-side checks and validations (and maybe an Update on a MySql batabase) I just return a Json array (that's whay I have a JSON.decode(response) on the client-side-earth) and then just check the "success" key to discover if the submitting succeded on the server-side-moon. I just add a small message that I display in an alert. Obviously I could use the JSON array to send back a lot more data to the client, but in this case is enough. Hope this helps and please let me know of better solutions.

Lester