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

#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


Messages In This Thread
jquery success / error? - by El Forum - 07-14-2012, 12:11 AM
jquery success / error? - by El Forum - 07-14-2012, 07:46 AM
jquery success / error? - by El Forum - 07-14-2012, 06:27 PM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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