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
<script>
$(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);
})
});
}
});
});
</script>
<div id="register_form">
<form action="register.php">
<div>
<label for="login">Login</label>
<input id="login" type="text" name="login" required/>
<span id="login_check"></span>
</div>
<div>
<label for="email">Adres email</label>
<input id="email" type="text" name="email" required/>
<span id="email_check"></span>
</div>
<div>
<label for="password1">Hasło</label>
<input id="password1" type="password" name="password1" required/>
</div>
<div>
<label for="password2">Powtórz hasło</label>
<input id="password2" type="password" name="password2" required/>
</div>
<div>
<input type="submit" value="Rejestruj" disabled="disabled" />
</div>
</form>
</div>
plik: connection.php
<?php
function checkLoginExist($login) {
return true;
}
?>
plik: check_login_exist.php
<?php
include_once('connection.php');
$array = array();
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;
}
$array = array();
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?