Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [ajax] i formularze
Forum PHP.pl > Forum > XML, AJAX
hhg
to moje pierwsze wykorzystanie ajax w formularzach, w zasadzie przypadek jest błahy ale chodzi tu raczej o aspekty edukacyjne

celem jest pokazania na stronie komunikatu zwróconego przez skrypt php zamiast formularza - czyli podmiana zawartosci div-a o id = "wynik". Narazie niestety wszystkie komunikaty pojawiaja sie na czystej stronie zamiast w pliku .html

oto kod
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2.  
  3. <meta http-equiv="Content-Language" content="pl">
  4. <meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-2">
  5. <link rel="stylesheet" href="style.css" type="text/css">
  6. <title>kontakt via e-mail</title>
  7.  
  8.  
  9. <script type="text/javascript" src="advajax.js"></script>
  10.  
  11. <script type="text/javascript">
  12.  
  13. advAJAX.assign(document.getElementById("the_form"), {
  14.  
  15. onSuccess : function(obj) {
  16. var element = document.getElementById("wynik");
  17. element.innerHTML = obj.responseText;
  18. }
  19. });
  20.  
  21. </script>
  22.  
  23. </head>
  24. <body bgcolor="black"><br>
  25. <div id = "wynik">
  26.  
  27. <p class="para">Czekam na pytania, uwagi, sugestie dot. strony i Isengard.';
  28.  
  29. <form action="send.php" method="post" id ="the_form">
  30. <table cellspacing="0" cellpadding="1" border="0" align="center" width="95%">
  31. <tr>
  32. <td width="20%" valign="bottom" nowrap>
  33. <p align="right" class="para">temat:</p>
  34. </td>
  35. <td width="80%" align="left">
  36. <input type="text" size="30" maxlength="50" name="temat" style="width:200px;">
  37. </td>
  38. </tr>
  39. <tr>
  40. <td width="20%" valign="bottom" nowrap>
  41. <p align="right" class="para">e-mail nadawcy:</p>
  42. </td>
  43. <td width="80%" align="left">
  44. <input type="text" size="30" maxlength="50" name="email" style="width:200px;">
  45. </td>
  46. </tr>
  47. <tr>
  48. <td width="20%" valign="bottom" nowrap class="underline">
  49. <p align="right" class="para">treść wiadomości:<br><br><br><br><br><br><br><br><br><br></p>
  50. </td>
  51. <td width="80%" align="left" class="underline">
  52. <textarea name="tresc" style="width:431px; " rows="10" cols="50"></textarea>
  53. </td>
  54. </tr>
  55. <tr>
  56. <td align="center" colspan="2">
  57. <input type="submit" value="wyślij" name="submit"> <INPUT TYPE="reset" VALUE="wyczyść">
  58. </td>
  59. </tr>
  60. </table>
  61. </form>
  62. </div>
  63.  
  64. <p class="para" align="left">&nbsp;<a href="java script:window.close();" class="genmed">Zamknij Okno</a></p>
  65. </body>
  66. </html>


  1. <?php
  2.  
  3. #Pobieranie adresu IP
  4. $ip=$_SERVER['REMOTE_ADDR'];
  5. #Pobieranie serwera
  6. $host=gethostbyaddr($ip);
  7. #Pobieranie adresu strony
  8. $document=$_SERVER['HTTP_REFERER'];
  9.  
  10.  
  11. # temat
  12. if (empty($_POST['temat'])) {
  13. $te = FALSE;
  14. $message .= 'Zapomniałeś podać temat wiadomości.';
  15. $bg[1] = ' background-color: #C0C0C0; color: black';
  16. } else {
  17. $te = $_POST['temat'];
  18. }
  19.  
  20.  
  21. # e-mail
  22. if ( !(strstr($_POST['email'], '@')) || strlen($_POST['email'])<) {
  23. $em = FALSE;
  24. $message .= 'Popraw swój adres e-mail.';
  25. $bg[2] = ' background-color: #C0C0C0; color: black';
  26. } else {
  27. $em = $_POST['email'];
  28. }
  29.  
  30.  
  31. # tresc
  32. if (empty($_POST['tresc'])) {
  33. $tr = FALSE;
  34. $message .= 'Zapomniałeś podać treść wiadomości.';
  35. $bg[3] = ' background-color: #C0C0C0; color: black';
  36. } else {
  37. $tr = $_POST['tresc'];
  38. }
  39.  
  40.  
  41. if ($te && $em && $tr) {
  42.  
  43. $wiadomosc = "
  44. Wiadomość od $em: <br/><br/>
  45.  
  46. $tr <br/><br/>
  47.  
  48. -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- <br/>
  49. IP nadawcy: $ip <br/>
  50. HOST nadawcy: $host <br/>
  51. wysłano z: $document <br/>
  52. ";
  53.  
  54. $naglowki = "MIME-Version: 1.0rn";
  55. $naglowki .= "Content-type: text/html; charset=iso-8859-2rn";
  56. $naglowki .= "From: $emrn";
  57.  
  58. if ( mail('moj@email.pl',$te,$wiadomosc,$naglowki) ) {
  59. echo '<div align="center"><p class="para"><b>Wiadomość wysłana.</b></p></div>';
  60. } else { echo 'Wiadomość nie mogła zostać wysłana z przyczyn technicznych.'; }
  61.  
  62. } else {
  63. echo '<div align="center"><p class="error">';
  64. if (isset($message)) {
  65. echo $message;
  66. }
  67. echo '<br><b>Spróbuj jeszcze raz.</b>';
  68. echo '</p></div>';
  69. }
  70.  
  71.  
  72.  
  73. ?>


a to link jak to obecnie działa:
php programmer
Spróbuj wyświetlić komunikat funkcją alert


Kod
alert('Coś tam źle wpisałeś');
skowron-line
po 1 wrzuc sobie ten ajax w funkcje np.
  1. <script type="text/javascript">
  2. function sprawdz()
  3. advAJAX.assign(document.getElementById("the_form"), {
  4.  
  5. onSuccess : function(obj) {
  6. var element = document.getElementById("wynik");
  7. element.innerHTML = obj.responseText;
  8. }
  9. });
  10. }

no i oczywiscie jakos trzeba sobie to wywolac
  1. <input type="submit" value="wyślij" name="submit" onclick="sprawdz();">

i tutaj
zamiast
  1. <form action="send.php" method="post" id ="the_form">

daj
  1. <form action="" method="post" id ="the_form">

i powinno zadzialac... pzdr
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.