• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
jquery success / error?

#1
[eluser]shenanigans01[/eluser]
Hello, I'm using the following piece of code to ajax some form data to a controller:

Code:
$('#username').blur(function(){
                    var form_data = {
                        username : $("input#username").val()
                    };
                    $.ajax({
                        url: "http://localhost/auth/checkUsername/",
                        type: 'POST',
                        data: form_data,
                        success: function(msg) {
                            $('#username').removeClass('inputError');
                            $('#username').addClass('inputSuccess');
                            $("#success_message").html(msg).appendTo("form");
                        },
                        error: function(msg) {
                            $('#username').removeClass('inputSuccess');
                            $('#username').addClass('inputError');
                            $("#error_message").html(msg).appendTo("form");
                        }
                    });
                    return false;
                });

In the controller, if the username is available it will Echo "GoodJob!", triggering the success function passing the string along and "GoodJob" will display in the div. If I want to trigger the Error function I have to change the output header to 4xx (406 for example). The problem with this is even if I echo "BadJob" it doesn't seem to be passed along. In firebug it still shows "badjob" as the response. Thoughts?

#2
[eluser]Crackz0r[/eluser]
If im not wrong, you will get a TRUE or FALSE response, unles you use JSON, i.e. to "echo" a response. Check this little example of how to fetch data using JSON, jQuery and PHP

http://www.electrictoolbox.com/json-data...php-mysql/

Regards.

#3
[eluser]vitoco[/eluser]


"success" and "error" are functions to reflect the proper communication with the server, not the logic of the result in the app, so don't mess with that.

To solve your question, first define which type of return you wanna send from the server to the client, like plain text, xml, json. I'll show you how to work with json.

Code:
$('#username').blur(function()
{
var form_data = {
  username : $("input#username").val()
};
$.ajax({
  url: "http://localhost/auth/checkUsername/",
  type: 'POST',
  data: form_data,
  dataType : 'json' , // <-- return a json object
  success: function(msg) {
   // msg it's parsed as a json object automatically
   if( msg.available == 1 )
   {
    alert('Good Job');
    $('#username').removeClass('inputError');
    $('#username').addClass('inputSuccess');
    $("#success_message").html(msg).appendTo("form");
   }
   else
   {
    alert('Bad Joob');
    $('#username').removeClass('inputSuccess');
    $('#username').addClass('inputError');
    $("#error_message").html(msg).appendTo("form");
   }
  },
  error: function(msg) {
   alert('Error sending data to the server, please try again');
  }
});
return false;
});

In the controller

Code:
function checkUsername()
{
$result = array(
  'available' => 0 , // DEFAULT 0
);

// .......
// SOME CODE TO CHECK THE USER ....
// .......

if( $user_is_available )
{
  $result['available'] = 1 ;
}

echo json_encode( $result ) ; // RETURN ARRAY AS JSON ENCODED OBJECT
}

Saludos


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.