I tried making a username check code using JavaScript / php, now I can't really figure out how to do it.
This is my html:
<script type="text/javascript" src="js/jQuery.js"></script>
<script type="text/javascript" src="js/register.js"></script>
...
<input id="username" type="text">
this is my JavaScript:
$(function() {
$("#username").change(function() {
// getting the value that user typed
var checkString = $("#username").val();
if(checkString.indexOf('<') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf('>') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf('(') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf(')') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf("'") >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf('/') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf('[') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if(checkString.indexOf(']') >=0){
document.getElementById('return').innerHTML="Userame contains illigal characters.";
return;
}
if($("#Username").val().length < 6){
document.getElementById('return').innerHTML="Your chosen name is too short.";
}
// forming the queryString
var data = 'user='+ checkString;
// if checkString is not empty
if(checkString) {
// ajax call
$.ajax({
type: "POST",
url: "validate.php",
data: data,
beforeSend: function(html) { // this happen before actual call
},
success: function(html){
if(html == 'free'){
document.getElementById('return').innerHTML="Username "+ checkString +" is available";
}
else if(html == 'taken'){
document.getElementById('return').innerHTML="Username "+ checkString +" is taken";
}
}
});
}
return false;
});
});
and this is my php file which gets the data (located in /js/validate.php:
<?php
$user = $_POST['user'];
$user = mysql_real_escape_string($user);
$con = mysqli_connect("localhost","USER","PASS","DB");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$query = mysqli_query($con,"SELECT `Naam` FROM `main` WHERE `Naam` ='$user';");
$row = mysqli_num_rows($query);
if($row == 0) {
echo 'free';
} else {
echo 'taken';
}
?>
check url url: "validate.php",
, if you run code from root folder and your php file located in /js/validate.php, then you didn't receive any data. Look real adress in crhome inspector.
check data, what you actually receive in $_POST array.
may be mysqli_query($con,"SELECT
Naam FROM
main WHERE
Naam ='$user';");
$user
interprete as $user string and not variable, then you need concat string solution like ='"+$user';"
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.