mardi 20 janvier 2015

How to use jQuery/Ajax to perform MySQL Query


Vote count:

0




I'm trying to use jQuery to check if the username that the user entered in a form is already taken. Below are the relevant codesnippets in Java, and existence.php.



*java*
var username = document.register.username.value;
usernameTaken = checkUserExistence(username, 'username');

function checkUserExistence(str, type){
var dataString = '?str=' + str + '&type=' + type;
if($.trim(str).length>0 && $.trim(type).length>0){
$.ajax({
type: "POST",
url: "existence.php",
data: dataString,
beforeSend: function(){ $("#submit").val('Sending...');},
success: function(data){
if(data){
$("#submit").val('Succes!');
return 1;
}else{
$("#submit").val('Failure!');
return 0;
}
}
});
}
return false;
}
*/java*

<?php

include("inc/connect.php");

$data = $_GET["data"];
$type = $_GET["type"];

switch($type){
case "username":
$resultUsers = mysql_query("SELECT * FROM users WHERE username = '$data' ") or die(mysql_error());
if( mysql_num_rows($resultUsers) == 1 ){
echo 1;
}
break;
}

?>


What am I doing wrong? My website is supposed to show live hints to the users, like 'your username is too short' etc. All hints are working, but the ones where it should say 'your username is already taken' won't show. The form gets processed to my PHP-register function, where usernames that are already taken get rejected, so somehow the checkUserExistence-function and the existence.php page are not working.



asked 37 secs ago







How to use jQuery/Ajax to perform MySQL Query

Aucun commentaire:

Enregistrer un commentaire