Witam mam problem z napisaniem skryptu który będzie sprawdzał przy rejestracji czy taki login już istnieje.
Oto kod:
plik: register.php
<!DOCTYPE html>
<html>
<head>
<script language="JavaScript" type="text/javascript" src="jquery-2.1.1.min.js"></script>
</head>
<body>
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
} else {
include_once('register_form.php');
}
?>
</body>
</html>
plik: register_form.php
$(document).ready(function(){
$('#login').blur(function(){
if($(this).val().length > 0) {
var data = {
"login": $(this).val()
};
data = $.param(data);
$.ajax({
type: "GET",
dataType: "json",
url: "check_login_exist.php",
data: data,
beforeSend: (function() {
console.log("loading");
}),
success: (function( msg ) {
console.log(msg['type'] + ' ' + msg['message']);
}),
error: (function(xhr, desc, err) {
console.log("error: "+xhr + "\n" + err);
})
});
}
});
});
<form action="register.php"> <input id="login" type="text" name="login" required/> <input id="email" type="text" name="email" required/> <input id="password1" type="password" name="password1" required/> <input id="password2" type="password" name="password2" required/> <input type="submit" value="Rejestruj" disabled="disabled" />
plik: connection.php
<?php
function checkLoginExist($login) {
return true;
}
?>
plik: check_login_exist.php
<?php
include_once('connection.php');
if(isset($_GET['login'])) { if(checkLoginExist($_GET['login'])) {
$array['type'] = 'fail';
$array['message'] = 'Ten login jest zajęty';
} else {
$array['type'] = 'success';
$array['message'] = 'Ten login jest wolny';
}
} else {
$array['type'] = 'fail';
$array['message'] = 'Błędny login';
}
header('Content-Type: application/json'); echo json_encode
($array); ?>
Po wpisaniu loginu dostaję taką odpowiedź w consoli:
loading register.php:22
error: [object Object]
SyntaxError: Unexpected token
A gdy przerobię plik check_login_exist.php tak aby wyglądał tak:
<?php
function checkLoginExist($login) {
return true;
}
if(isset($_GET['login'])) { if(checkLoginExist($_GET['login'])) {
$array['type'] = 'fail';
$array['message'] = 'Ten login jest zajęty';
} else {
$array['type'] = 'success';
$array['message'] = 'Ten login jest wolny';
}
} else {
$array['type'] = 'fail';
$array['message'] = 'Błędny login';
}
header('Content-Type: application/json'); echo json_encode
($array); ?>
To w consoli pojawia się:
loading register.php:22
fail Ten login jest zajęty
Czyli że teraz skrypt działa.
Mógłby mi ktoś powiedzieć co mam zrobić abym mógł includować pliki w plikach php gdy używam ajaxa?