Witam, to znowu ja biggrin.gif

Szukałem w necie poradników jak zrobić żeby dane z formularza przechodziły przez php i wyświetlone zostały na stronie bez refreshu strony...
Znalazłem w necie kilka poradników i mam taką "sklejkę" wink.gif

Index.html

  1. <script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
  2. <link rel="stylesheet" type="text/css" href="style.css" />
  3. </head>
  4.  
  5. <div id="result"></div>
  6. <input type="text" id="name" name="name" class="name" placeholder="login..."/>
  7. <input type="submit" id="submit" name="submit" value="OK!"/>
  8. <script src="validation.js"></script>
  9.  
  10. </html>


Show.php

  1. <?php
  2. if($_POST) {
  3.  
  4. if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest') {
  5.  
  6. $answer = json_encode(array('result' => 'error', 'text' => 'To nie jest żądanie AJAX'));
  7. }
  8. else {
  9.  
  10. $login = filter_var($_POST["login"], FILTER_SANITIZE_STRING);
  11.  
  12. if(strlen($login) < 5) {
  13.  
  14. $answer = json_encode(array('result' => 'error', 'text' => 'Przynajmniej 5 znaków!'));
  15. }
  16. else {
  17.  
  18. $answer = json_encode(array('result' => 'ok', 'text' => 'Witaj,'. $login. ' !'));
  19. }
  20.  
  21. }
  22. }
  23.  




Javascript

  1. $(document).ready(function() {
  2.  
  3. $("#submit").click(function() {
  4.  
  5. var login = $('input[name=name]').val();
  6.  
  7. var next = true;
  8.  
  9. if(login==""){
  10.  
  11. $('input[name=name]').css('border-color','red');
  12. next = false;
  13. }
  14.  
  15. if(next){
  16.  
  17. post_data = {'login':login};
  18. $.post('show.php', post_data, function(response){
  19.  
  20. if(response.result == 'error'){
  21. output = '<div class="error">'+response.text+'</div>';
  22. }
  23. else{
  24. output = '<div class="success">'+response.text+'</div>';
  25.  
  26. $('#name').val('');
  27. }
  28. $("#result").hide().html(output).slideDown();
  29.  
  30. }, 'json');
  31. }
  32. });
  33.  
  34. $("#name").keyup(function() {
  35.  
  36. $("#name").css('border-color','grey');
  37.  
  38. });
  39.  
  40.  
  41. });


Dodałem też że jak się nie wypełni pola to się obramowanie zmieni na czerwone, to przynajmniej działa biggrin.gif Nakieruje ktoś coś? smile.gif