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> <input id="login" type="text" name="login" required/> </div> <div> <input id="email" type="text" name="email" required/> </div> <div> <input id="password1" type="password" name="password1" required/> </div> <div> <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'); 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'; } ?>
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(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'; } ?>
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?