Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [js] zmienne tablicowe, w polaczeniu z php
grzegorz_g
post
Post #1





Grupa: Zarejestrowani
Postów: 259
Pomógł: 0
Dołączył: 26.10.2004

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


Mój skrócony formularz:


  1. <form action="index.php" method="post" id="Formularz" name="Formularz">
  2.  
  3. <input type="checkbox" name="lista[]" value="<? print $row['miejsce'] ?>" />
  4. <input type="hidden" name="cena_k[]" value="<? print $row['cena_s'] ?>" />
  5. <input type="checkbox" name="lista[]" value="<? print $row['miejsce'] ?>" />
  6. <input type="hidden" name="cena_k[]" value="<? print $row['cena_s'] ?>" />
  7. <input type="checkbox" name="lista[]" value="<? print $row['miejsce'] ?>" />
  8. <input type="hidden" name="cena_k[]" value="<? print $row['cena_s'] ?>" />
  9. </form>


w php umiem odzyskac dane ze zmiennej cena_k[], ale chce tą zmienną wykorzystać w funkcji js :


  1. function przelicz2()
  2. {
  3. var sum=0;
  4. var x = 0;
  5.  
  6. var x=document.getElementsByName("cena_k[]");
  7.  
  8.  
  9. for (i=0;i<(x.length);i++)
  10. {
  11.  
  12. if (document.Formularz.lista[i].checked==true) {
  13.  
  14.  
  15. sum=((sum)+parseInt(((x[i].value))));
  16.  
  17. }
  18.  
  19. }
  20.  
  21.  
  22. document.getElementById("wartosc_k").innerHTML = sum;
  23.  
  24.  
  25. }



funkcja ma za zadanie zliczyć wartości zmiennej cena_k tylko dla input przy którym jest zaznaczony checkbox

CZekam na info. Chyba, że wykorzystujecie inne funkcje .


--------------------
www.wettradar.com
Go to the top of the page
+Quote Post
kamil4u
post
Post #2





Grupa: Zarejestrowani
Postów: 2 350
Pomógł: 512
Dołączył: 4.01.2009
Skąd: Wrocław / Świdnica

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


Coś ala:
Kod
<html>
<body>
<script>
function przelicz2(x,y,z)
{
sum=0;
x=document.getElementsByName("cena_k[]");
y=document.getElementsByName("lista[]");
for (i=0, z=(x.length);i<z;i++)
if (y[i].checked==true)
  sum += ~~x[i].value;

document.getElementById("wartosc_k").innerHTML = sum;
}
</script>

<form action="index.php" method="post" id="Formularz" name="Formularz">

<input type="checkbox" name="lista[]" value="<? print $row['miejsce'] ?>" />
<input type="hidden" name="cena_k[]" value="10" />

<input type="checkbox" name="lista[]" value="<? print $row['miejsce'] ?>" />
<input type="hidden" name="cena_k[]" value="8" />

<input type="checkbox" name="lista[]" value="<? print $row['miejsce'] ?>" />
<input type="hidden" name="cena_k[]" value="5" />

<input type="button" onClick="przelicz2();" value="test">
</form>
<span id="wartosc_k">Tu się pojawi wynik</span>
</body>
</html>


--------------------
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 - 08:18