Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> 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
 
Start new topic
Odpowiedzi
diamondking
post 17.01.2018, 16:45:36
Post #2





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

Posty w temacie


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

 



RSS Wersja Lo-Fi Aktualny czas: 28.04.2024 - 07:01