Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Odbiór danych PHP w AJAX
diamondking
post 17.01.2018, 10:26:03
Post #1





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

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


Witajcie

znalazłem kod:

  1. <script>
  2. function checkAvailability() {
  3. $("#loaderIcon").show();
  4. jQuery.ajax({
  5. url: "check_availability.php",
  6. data:'username='+$("#username").val(),
  7. type: "POST",
  8. success:function(data){
  9.  
  10. $("#user-availability-status").html(data);
  11. $("#loaderIcon").hide();
  12. },
  13. error:function (){}
  14. });
  15. }
  16. </script>


Ogólnie działa.
Ale chcę uzależnić dalsze działanie AJAXA od tego czy PHP zwróci wartość "jest" lub "niema"

pomoże ktoś?
Go to the top of the page
+Quote Post
viking
post 17.01.2018, 10:35:32
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Do poczytania https://prophp.pl/advice/show/17/jak_przygo...dan_xhr_json%3F


--------------------
Go to the top of the page
+Quote Post
diamondking
post 17.01.2018, 16:45:36
Post #3





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

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


Mam kod:

  1. <script>
  2. $(function() {
  3. $('#userdelete').on('submit', function(event) {
  4. event.preventDefault();
  5. $.ajax({
  6. url: "kasuj.php",
  7. method: "POST",
  8. data: $(this).serialize()
  9. })
  10. .done(function(data) {
  11. $('#message').html(data.message);
  12. })
  13. .fail(function(data) {
  14. $('#message').html(data.message);
  15. });
  16. });
  17. });
  18. </script>
  19.  
  20.  
  21.  
  22.  
  23.  
  24. <form method="POST" id="userdelete">
  25. <input type="text" name="userid">
  26. <input type="submit">
  27. </form>
  28.  
  29. <div id="message"></div>


a plik kasuj.php wyglada tak:

  1. // POŁĄCZ Z BAZĄ DANYCH
  2. $conn = @mysql_connect ($cfg['db_server'], $cfg['db_user'], $cfg['db_pass']);
  3. $select = @mysql_select_db ($cfg['db_name'], $conn);
  4.  
  5. $id = $_POST['userid'];
  6.  
  7. if (isset($_POST["userid"]) && !empty($_POST["userid"])) {
  8.  
  9. $sprawdz="select user_id from users where user_id='$id'";
  10. $rekordy = mysql_query($sprawdz);
  11. if(mysql_num_rows($rekordy)==0)
  12. {
  13. echo json_encode([
  14. 'message' => 'Nieprawidłowa akcja'
  15. ]);
  16. }
  17. else
  18. {
  19.  
  20. $kas1 = "DELETE FROM users WHERE user_id='$id'";
  21.  
  22. if(mysql_query($kas1)){
  23.  
  24. echo json_encode([
  25. 'message' => 'Ok'
  26. ]);
  27.  
  28. }
  29.  
  30. }
  31. }


kasuje z bazy lecz nie wyświetla komunikatu ... już nie wiem co robić biggrin.gif
Go to the top of the page
+Quote Post
viking
post 17.01.2018, 16:54:12
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Przede wszystkim to https://prophp.pl/advice/show/3/korzystam_z...ed._dlaczego%3F
Zrób to porządnie jak było pokazane. Pewnie jakiś notice leci. Sprawdź w narzędziach deweloperskich jak było opisane.


--------------------
Go to the top of the page
+Quote Post
diamondking
post 17.01.2018, 17:06:50
Post #5





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

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


Przekopiowałem wszystko z tej strony - uzupełniłem o swoją baze danych i nadal nie działą
Go to the top of the page
+Quote Post
viking
post 17.01.2018, 17:14:40
Post #6





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Co mówi konsola przeglądarki, co masz a w zakładce sieć. Nie działa to nie odpowiedź.


--------------------
Go to the top of the page
+Quote Post
diamondking
post 18.01.2018, 09:54:31
Post #7





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

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


Pawie działa.
W sensie jak wyślę formularz to komunikat "bad" lub "ok" wyświetli się na stronie przez ułamek sekundy i znika ... sad.gif

  1. ipt>
  2. $(document).ready(function(){
  3. $('#usun').click(function(){
  4. var login= $('#login').val();
  5. if(login != '')
  6. {
  7. $.ajax({
  8. url:"fetch2.php",
  9. method:"POST",
  10. data:{login:login},
  11. dataType:"JSON",
  12. success:function(data)
  13. {
  14. $('#employee_age').text(data.age);
  15. }
  16. })
  17. }
  18. else
  19. {
  20. alert("Please Select Employee");
  21.  
  22. }
  23. });
  24. });
  25. </script>
  26.  
  27. <form method="POST">
  28. <b>Login:</b> <input type="text" name="login" id="login"><br>
  29. <input type="submit" value="Usuń" name="usun" id="usun">
  30. </form>
  31.  
  32. <span id="employee_age"></span>
  33.  
  34. </body>



  1. <?php
  2. //fetch.php
  3. if(isset($_POST["login"]))
  4. {
  5.  
  6. $login = $_POST["login"];
  7.  
  8. $cfg['db_server'] = 'localhost'; // Serwer bazy danych
  9. $cfg['db_user'] = '25870472_abc'; // Nazwa użytkownika
  10. $cfg['db_pass'] = '****************'; // Hasło
  11. $cfg['db_name'] = '25870472_abc'; // Nazwa bazy danych
  12.  
  13. // POŁĄCZ Z BAZĄ DANYCH
  14. $conn = @mysql_connect ($cfg['db_server'], $cfg['db_user'], $cfg['db_pass']);
  15. $select = @mysql_select_db ($cfg['db_name'], $conn);
  16.  
  17.  
  18. $sql = mysql_query("SELECT * FROM users WHERE name='".$login."'");
  19. $licz = mysql_num_rows($sql);
  20.  
  21. if($licz>0){
  22.  
  23. mysql_query("DELETE FROM users WHERE name='".$login."'")
  24. or die('Błąd zapytania: '.mysql_error());
  25.  
  26. $ok = "OK";
  27. $data["age"] = $ok;
  28.  
  29. }
  30.  
  31. else {
  32. $bad = "bad";
  33. $data["age"] = $bad;
  34. }
  35.  
  36. echo json_encode($data);
  37. }
  38. ?>

Go to the top of the page
+Quote Post
nospor
post 18.01.2018, 09:57:16
Post #8





Grupa: Moderatorzy
Postów: 36 440
Pomógł: 6289
Dołączył: 27.12.2004




Bo podpiales AJAX do guzika ktory tez normalnie sle forma to cie strona przeladowuje...
Daj w onclick
return false;

i formularz ci sie nie wysle


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
diamondking
post 18.01.2018, 10:03:35
Post #9





Grupa: Zarejestrowani
Postów: 100
Pomógł: 0
Dołączył: 7.02.2014

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


Dziękuję!

TEMAT DO ZAMKNIĘCIA
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: 28.03.2024 - 09:14