Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Jak w wybrane miejsce wstawic wynik kodu php, XMLHttpRequest
kysiu.pl
post
Post #1





Grupa: Zarejestrowani
Postów: 42
Pomógł: 0
Dołączył: 24.10.2004

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


plik: add.php
  1. <script src="xmlhttp.js" type="text/javascript"></script>
  2.  
  3. function calc() {
  4. frm=document.forms[0]
  5.  
  6. DataToSend = "z=ce"+frm.elements['a'].value;
  7.  
  8.  
  9. url="test.php"
  10.  
  11. xmlhttp.open("POST",url,true);
  12. xmlhttp.onreadystatechange=function() {
  13. if (xmlhttp.readyState==4) {
  14. document.forms[0].elements['total'].value=xmlhttp.responseText
  15. }
  16. }
  17.  
  18. xmlhttp.setRequestHeader('Accept','message/x-formresult')
  19. xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  20. xmlhttp.send(DataToSend);
  21.  
  22. return false
  23. }
  24. <form action="add.php" method="POST" onsubmit="return calc()">
  25. <input type=text name=a value=""> <input type=text name=total value="">
  26. <input type=submit value="Calculate">
  27. </form>
  28. <b>Tutaj wynik</b>



plik test.php
  1. <?php
  2.  
  3. if(!empty($_POST['z'])) {
  4. $_SESSION['data'] .= $_POST['z'];
  5. echo $_SESSION['data'];
  6. }
  7.  
  8. ?>



Nie znam sie kompletnie na JS wiec mam pytanie:
Jak zrobic żeby wynik pojawiał się w "Tutaj wynik" a nie w <input type=text name=total value="">.

help
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 1)
revyag
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 2 258
Pomógł: 16
Dołączył: 21.09.2004
Skąd: Kielce

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


Najlepiej zamknąć to jakimś elementem blokowym i użyć innerHTML.
Kod
function calc() {
    frm=document.forms[0]
    DataToSend = "z=ce"+frm.elements['a'].value;
    url="test.php"
    xmlhttp.open("POST",url,true);
    xmlhttp.onreadystatechange=function() {
    if (xmlhttp.readyState==4) {
        document.getElementById("wynik").innerHTML=xmlhttp.responseText
    }
}

  1. <form action="add.php" method="POST" onsubmit="return calc()">
  2. <input type=text name=a value=""> <input type=text name=total value="">
  3. <input type=submit value="Calculate">
  4. </form>
  5. <div id="wynik">Tutaj wynik</div>


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

------
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 Aktualny czas: 21.08.2025 - 10:10