im trying to implement the classic username availability checker with jquery and ajax. here’s my approach:
jquery, on client side:
$("#usrnameTBox").keyup(function(){
if ($(this).val().length > 2) {
$.ajax({
type: 'POST',
url: 'ajax/ajaxPerfil.php',
data: { function : "askForUsr", usrname: $(this).val() },
dataType: 'json',
success: function(data) {
alert(data);
if(data.exists) {
$(".usrSt").attr('style', '')
.attr('style', "color:red;")
.html('YA ESTÁ EN USO');
} else {
$(".usrSt").attr('style', '')
.attr('style', "color:green;")
.html('Disponible!!!');
}
},
error: function(XMLHttpRequest, textStatus, errorThrown){
alert("Error!!! =/");
}
});
} else {
$(".usrSt").attr('style', '')
.attr('style', "display:none;")
.html('');
}
});
php, on server side:
<?php
require_once("../src/ajax/ajaxPerfilController.php");
?>
ajaxPerfilController.php:
<?php
$srcFolder = "../src/";
$classes = array("mappers/Perfil.php",
"fachadas/PerfilFachada.php"
);
foreach ($classes as $class)
require_once($srcFolder.$class);
function getByAttr($attr, $value) {
if (strcmp($attr,"usrname") == 0) {
$fachada = PerfilFachada::singleton();
return $fachada->exists($value);
}
return NULL;
}
if (isset($_POST["function"])) {
if (strcmp($_POST["function"],"askForUsr") == 0) {
if (isset($_POST["usrname"])) {
if (getByAttr("usrname",$_POST["usrname"])) {
$return["exists"] = True;
echo json_encode($return);
} else {
$return["exists"] = False;
echo json_encode($return);
}
}
}
}
?>
The thing is, it keeps telling me server error 500!!! the require routes are ok since i’ve harcoded the request and tried it on the console… So, i dont know what else to do, i’ve tried with dataType ‘html’, ‘text’, ‘json’ in the ajax call, but got the same result. I gess its just a simple thing, but i just dont know what to do…
just in case, im using google-chorme
thanks for your help!!! =)
There are some reasons because you can get a 500 Error, including execution/compiling time errors.
The only error I see is that you’re not declaring your
$returnvar (well, php doesn’t need declaration, but needs an array when you access with brackets), i’d add$return = array();before using it.BUT, You should debug your code to find the error. Depending on your tools, the best would be to set a breakpoint and start debugging. If you can’t do this for any way, my advise is to use Firebug for Firefox and see what error are you receiving (in the Net panel, Response section).
Hope this helps. Cheers
PS: If you have logs in your server, you could find the error there.