Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP][JavaScript] Koszyk na zakupy zwraca złą wartośc przy przeliczaniu produktów
Forum PHP.pl > Forum > Przedszkole
amii
Mam koszyk na zakupy gdzie widoczny jest przycisk przelicz, który powinien przeliczać zmodyfikowaną ilość produktów * cenę. Problem w tym, że po kliknięciu tego przycisku skrypt zwraca cenę dla ilości 1 bez względu na to co zmieni klient. Poniżej fragment skryptu koszyka:

  1. $html .= '<form name="cart_recount" method="POST" action="index.php">';
  2. $html .= '<input type="HIDDEN" name="p" value="cart">';
  3. $html .= '<input type="HIDDEN" name="recount" value="true">';
  4. $html .= '<tr>';
  5. $html .= '<td style="width:25px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">Lp.</td>';
  6. $html .= '<td style="width:145px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">Produkt</td>';
  7. $html .= '<td style="width:40px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">Ilość</td>';
  8. $html .= '<td style="width:80px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">Cena</td>';
  9. $html .= '<td style="width:40px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="ccart_main_top">Rabat</td>';
  10. $html .= '<td style="width:80px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">Cena z Rabatem</td>';
  11. $html .= '<td style="width:80px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">Wartość</td>';
  12. $html .= '<td style="width:25px; height:30px; background-color:#ececec; border-style:solid; border-color:#dddddd; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_top">&nbsp;</td>';
  13. $html .= '</tr>';
  14.  
  15. $lp = 1;
  16. $sum = 0;
  17. // pozycje
  18. $query = mysql_query("SELECT *,cart.id AS item FROM cart LEFT JOIN product ON cart.pid = product.id WHERE session='".session_id()."' LIMIT 0,10;");
  19.  
  20.  
  21. while($cartitem = mysql_fetch_assoc($query))
  22. {
  23. $html .= '<tr>';
  24. $html .= '<td style="width:25px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_item">'.$lp++.'</td>';
  25. $html .= '<td style="width:145px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:left; vertical-align:middle; font-weight:bold;" class="cart_main_item">&nbsp;<a href="http://strona.pl/'.$cartitem['pid'].','.$cartitem['seo'].'">'.$cartitem['name'].'</a>';
  26. if($cartitem['discount_code']=="PROMOCJA") $html .= '<font style="color:#dd0000; font-size:7.5pt; font-weight:normal;"><br>&nbsp;CENA PROMOCYJNA</font>';
  27. elseif($cartitem['discount_code']) $html .= '<font style="color:#0000dd; font-size:7.5pt; font-weight:normal;"><br>&nbsp;KOD RABATOWY</font>';
  28. $html .= '</td>';
  29. $html .= '<td style="width:40px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_item">
  30.  
  31. <input type="TEXT" onFocus="this.select();" name="quantity_'.$cartitem['id'].'" value="'.$cartitem['quantity'].'" class="cart_main_item" style="border-style:none; background-color:#ffffff; width:40px; padding:0px 0px 0px 0px; text-align:center;"></td>'; //pole tekstowe gdzie klient może zmodyfikować ilość produktów
  32.  
  33.  
  34. $html .= '<td style="width:80px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:right; font-size:7.5pt;" class="cart_main_item">'.number_format($cartitem['pricewithtax'],2,',',' ').'zł z VAT<br>('.number_format($cartitem['price'],2,',',' ').'zł netto)</td>';
  35. $html .= '<td style="width:40px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_item">'.round(((1-($cartitem['pricewithtax_discount']/$cartitem['pricewithtax']))*100),0).'%</td>';
  36. $html .= '<td style="width:80px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:middle; font-size:7.5pt;" class="cart_main_item">'.number_format($cartitem['pricewithtax_discount'],2,',',' ').'zł z VAT<br>('.number_format($cartitem['price_discount'],2,',',' ').'zł netto)</td>';
  37.  
  38.  
  39. $html .= '<td style="width:80px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:middle; font-weight:bold;" class="cart_main_item">'.number_format(($cartitem['quantity']*$cartitem['pricewithtax_discount']),2,',',' ').'zł<br>z VAT</td>'; //tutaj po wysłaniu formularza powinna pojawiać się zmodyfikowana wartość cena*ilość produktów, pole jednak cały czas zwraca wartość dla ilości=1
  40.  
  41.  
  42. $html .= '<td style="width:25px; height:30px; background-color:#ffffff; border-style:solid; border-color:#ececec; border-width:1px; text-align:center; vertical-align:middle;" class="cart_main_item"><A href="index.php?p=cart&delete='.$cartitem['item'].'"><img src="layout/'.$style.'/delete.png" alt="" title="" border="0" style="width:20px; height:20px;"></a></td>';
  43. $html .= '</tr>';
  44.  
  45. $sum += ($cartitem['quantity']*$cartitem['pricewithtax_discount']);
  46. }
  47. // podusmowenie cen
  48. $html .= '</form>';


Następnie w dalszej części strony pojawia się taki fragment odpowiedzialny za przeliczanie i uaktywnienie powyższego kodu. Poprzez kliknięcie na grafikę

  1. $html .= '<img onClick="document.cart_recount.submit();" src="layout/'.$style.'/cart_recount_off.png" onMouseOver="this.src='."'layout/".$style."/cart_recount_on.png'".';" onMouseOut="this.src='."'layout/".$style."/cart_recount_off.png'".';" alt="" title="" border="0" style="cursor:pointer;">';
  2. $html .= '<img src="layout/'.$style.'/cart_recount_on.png" alt="" title="" border="0" style="display:none;">';
  3. $html .= '</td>';

nospor
No a gdzie masz to liczenie?
amii
Skrypt powinien pobierać wartość z pola z linii nr 31 tyle, że tam wyświetla się zła wartość -> $_POST[quantity_'.$cartitem['id'].'"] tu jest zczytywana ilość produktów w koszyku z bazy a nie o to chodzi.

Trzeba by zrobić coś takiego, że kiedy użytkownik zmieni tą wartość (onChange) jest ona następnie przekazywana do dalszej części skryptu. Problem jest taki, że sam PHP sobie z tym nie poradzi bo dane nie są wysyłane do serwera to się dzieje dopiero po odpaleniu procedury onClick w drugim fragmencie kodu.
Trzeba jakoś połączyć PHP z JS i niestety nie wiem jak to obejść.
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.