Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Ajax json php dziwne zachowanie
rafcik
post 25.10.2014, 13:22:58
Post #1





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 25.10.2014

Ostrzeżenie: (0%)
-----


Witam mam problem z napisaniem skryptu który będzie sprawdzał przy rejestracji czy taki login już istnieje.

Oto kod:

plik: register.php
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <script language="JavaScript" type="text/javascript" src="jquery-2.1.1.min.js"></script>
  5. </head>
  6. <body>
  7. <?php
  8. if($_SERVER['REQUEST_METHOD'] == 'POST') {
  9.  
  10. } else {
  11. include_once('register_form.php');
  12. }
  13. ?>
  14. </body>
  15. </html>


plik: register_form.php
  1. $(document).ready(function(){
  2. $('#login').blur(function(){
  3. if($(this).val().length > 0) {
  4. var data = {
  5. "login": $(this).val()
  6. };
  7. data = $.param(data);
  8.  
  9. $.ajax({
  10. type: "GET",
  11. dataType: "json",
  12. url: "check_login_exist.php",
  13. data: data,
  14. beforeSend: (function() {
  15. console.log("loading");
  16. }),
  17. success: (function( msg ) {
  18. console.log(msg['type'] + ' ' + msg['message']);
  19. }),
  20. error: (function(xhr, desc, err) {
  21. console.log("error: "+xhr + "\n" + err);
  22. })
  23. });
  24. }
  25. });
  26. });
  27.  
  28. <div id="register_form">
  29. <form action="register.php">
  30. <div>
  31. <label for="login">Login</label>
  32. <input id="login" type="text" name="login" required/>
  33. <span id="login_check"></span>
  34. </div>
  35. <div>
  36. <label for="email">Adres email</label>
  37. <input id="email" type="text" name="email" required/>
  38. <span id="email_check"></span>
  39. </div>
  40. <div>
  41. <label for="password1">Hasło</label>
  42. <input id="password1" type="password" name="password1" required/>
  43. </div>
  44. <div>
  45. <label for="password2">Powtórz hasło</label>
  46. <input id="password2" type="password" name="password2" required/>
  47. </div>
  48. <div>
  49. <input type="submit" value="Rejestruj" disabled="disabled" />
  50. </div>
  51. </form>
  52. </div>


plik: connection.php
  1. <?php
  2. function checkLoginExist($login) {
  3. return true;
  4. }
  5. ?>


plik: check_login_exist.php
  1. <?php
  2. include_once('connection.php');
  3.  
  4. $array = array();
  5.  
  6. if(isset($_GET['login'])) {
  7. if(checkLoginExist($_GET['login'])) {
  8. $array['type'] = 'fail';
  9. $array['message'] = 'Ten login jest zajęty';
  10. } else {
  11. $array['type'] = 'success';
  12. $array['message'] = 'Ten login jest wolny';
  13. }
  14. } else {
  15. $array['type'] = 'fail';
  16. $array['message'] = 'Błędny login';
  17. }
  18.  
  19. header('Content-Type: application/json');
  20. echo json_encode($array);
  21. ?>


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:
  1. <?php
  2. function checkLoginExist($login) {
  3. return true;
  4. }
  5.  
  6. $array = array();
  7.  
  8. if(isset($_GET['login'])) {
  9. if(checkLoginExist($_GET['login'])) {
  10. $array['type'] = 'fail';
  11. $array['message'] = 'Ten login jest zajęty';
  12. } else {
  13. $array['type'] = 'success';
  14. $array['message'] = 'Ten login jest wolny';
  15. }
  16. } else {
  17. $array['type'] = 'fail';
  18. $array['message'] = 'Błędny login';
  19. }
  20.  
  21. header('Content-Type: application/json');
  22. echo json_encode($array);
  23. ?>


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?
Go to the top of the page
+Quote Post
Pyton_000
post 25.10.2014, 14:39:53
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

Ostrzeżenie: (0%)
-----


Odpal w przeglądarce poprawny url i jaki błąd wywala.
czylu np: localhost/check_login_exist.php?login=posladki
Go to the top of the page
+Quote Post
rafcik
post 25.10.2014, 14:47:03
Post #3





Grupa: Zarejestrowani
Postów: 2
Pomógł: 0
Dołączył: 25.10.2014

Ostrzeżenie: (0%)
-----


Wyświetla się coś takiego:

{"type":"fail","message":"Ten login jest zaj\u0119ty"}
Go to the top of the page
+Quote Post
tzm
post 25.10.2014, 22:19:23
Post #4





Grupa: Zarejestrowani
Postów: 675
Pomógł: 58
Dołączył: 17.12.2013

Ostrzeżenie: (10%)
X----


skoro wyświetla Ci loading jako before to znaczy że nie dochodzi nawet do success co z kolei znaczy przy otrzymaniu odpowiedzi wpisując prawidłowo akcje w gecie że - nie wysyłasz danych ajaxem

Ten post edytował tzm 25.10.2014, 22:19:45
Go to the top of the page
+Quote Post
trueblue
post 26.10.2014, 08:18:40
Post #5





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

Ostrzeżenie: (0%)
-----


Sprawdź czy plik check_login_exist.php jest kodowany w UTF-8 bez BOM.

P.S. data = $.param(data); - to nie jest potrzebne, choć może zostać.

Ten post edytował trueblue 26.10.2014, 09:16:45


--------------------
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 26.04.2024 - 13:30