vendredi 10 avril 2020

Adding delivery price with checkbox

I have prepared a simple order form. Total cost counting works, but I can't add a checkbox feature that selects the cost of shipping ("calc"). I always get "if = 300". Please help, I don't know JavaScript very well, I am still learning.

File index.php:

    <script type="text/javascript">
    function prepocitat() {      
      var http_request = false;
      var request = "akce=prepocet&";
      if (window.XMLHttpRequest) {
         http_request = new XMLHttpRequest();
      }
      else if (window.ActiveXObject) {
        try {
          http_request = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (error) {
          http_request = new ActiveXObject("Microsoft.XMLHTTP");
        }
      }          
      var pocetpolozek = <?php $obchod = simplexml_load_file('zbozi.xml'); print $obchod->count(); ?>;  
      for(var i=1; i <= pocetpolozek; i++)
      {
        if (document.getElementById('pocet_' + i) != null)
        {
          request = request + 'pocet_' + i + '=' + document.getElementById('pocet_' + i).value + '&';                              
        }
      }      
      http_request.onreadystatechange = function() { prepocitej(http_request); };
      http_request.open('POST', 'prepocitej.php', true);            
      http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');                        
      http_request.send(request);
    }

    function prepocitej(http_request) {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            document.getElementById('rekapitulace').innerHTML = http_request.responseText;            
         }
         else {
           alert('Chyba');
         }
      }
    }

    function objednat() {  
        var http_request = false;
        var request = "akce=objednavka&";
        if (window.XMLHttpRequest) {
          http_request = new XMLHttpRequest();
        }
        else if (window.ActiveXObject) {
          try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
          } 
          catch (error) {
            http_request = new ActiveXObject("Microsoft.XMLHTTP");
          }
        }                               
        request = request + 'jmeno=' + document.getElementById('jmeno').value + '&';
        request = request + 'email=' + document.getElementById('e-mail').value + '&';
        request = request + 'telefon=' + document.getElementById('telefon').value + '&';
        request = request + 'ulice=' + document.getElementById('ulice').value + '&';
        request = request + 'psc=' + document.getElementById('psc').value + '&';
        request = request + 'mesto=' + document.getElementById('mesto').value + '&';
        request = request + 'e-mail=' + document.getElementById('e-mail').value + '&';
        request = request + 'fakturacni=' + document.getElementById('fakturacni').value + '&';
        request = request + 'dodaci=' + document.getElementById('dodaci').value + '&';
        request = request + 'celkem=' + document.getElementById('celkem').value + '&';
        request = request + 'zprava=' + document.getElementById('zprava').value + '&';
        http_request.onreadystatechange = function() { objednej(http_request); };
        http_request.open('POST', 'prepocitej.php', true);            
        http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');                        
        http_request.send(request);
   }

   function objednej(http_request) {
      if (http_request.readyState == 4) {
        if (http_request.status == 200) {
          document.getElementById('rekapitulace').innerHTML = http_request.responseText;            
        }
        else {
          alert('Chyba');
        }
      }
   }  
  </script>

  <h1>Objednávka produktů</h1>
  <form method="post" action="javascript:prepocitat()">        
    <table>
      <tr>
        <td>Kód</td>
        <td>Název</td>
        <td>Cena</td>
        <td>Počet kusů</td>
      </tr>        

  <?php
    $obchod = simplexml_load_file('zbozi.xml');     
    foreach ($obchod->zbozi as $zbozi)
      {
          print "<tr>\n";
          print "<td>". strip_tags($zbozi->id->asXML())."</td>\n";
          print '<td>'. strip_tags($zbozi->nazev->asXML())."</td>\n";
          print "<td>". strip_tags($zbozi->cena->asXML())."</td>\n";
        print '<td><input name="pocet_'. strip_tags($zbozi->id->asXML()).'" id="pocet_'. strip_tags($zbozi->id->asXML()).'" type="text" /></td>'."\n";
          print "</tr>";
    }            
  ?>

   </table>
   <input type="checkbox" value="1" name="calc">Doprava</input><br/>
   <input class="button-objednavka" type="submit" name="submit" value="Přepočítat" title="Přepočítat" />
  </form>

<div id="rekapitulace"></div>

File prepocitej.php:

    <?php

$obchod = simplexml_load_file('zbozi.xml'); 
$pocetpolozek = $obchod->count();

if ($_POST['akce'])
{
  //ini_set('session.save_path','tmp');
  //ini_set("session.cookie_domain",".domena.cz");

  session_start();
  if ($_POST['akce'] == 'prepocet')
  { 
      for ($i = 1; $i <= $pocetpolozek+1; $i++) {
          if ($_POST['pocet_'.$i.'']){
              $_SESSION['pocet_'.$i.''] = $_POST['pocet_'.$i.''];
          }
          else {
            unset($_SESSION['pocet_'.$i.'']);
          }
      }
      print '<h2>Rekapitulace objednávky</h2>';
      print '<table>';
      print '<tr><th>Zboží</th><th>Cena celkem</th></tr>';
      $obchod = simplexml_load_file('zbozi.xml');
      $castka = 0; 
      $hmotnost = 0; 
      $zprava = '';     
      foreach ($obchod->zbozi as $zbozi)
              { 
                if (isset($_SESSION['pocet_'.strip_tags($zbozi->id->asXML()).'']))
                {
              if(!empty($_POST["calc"]))
                $doprava = 300;
              else
                $doprava = 200;  
              $castka_ = 0;
              $polozka = "";        
              $polozka .= "<tr>\n";
                  $polozka .= "<td>".$_SESSION['pocet_'.strip_tags($zbozi->id->asXML()).''].'x '. strip_tags($zbozi->nazev->asXML())."</td>\n";
                  $castka_ = $_SESSION['pocet_'.strip_tags($zbozi->id->asXML()).''] * strip_tags($zbozi->cena->asXML());
              $polozka .= '<td>'. $castka_ ." Kč</td>\n";
                  $polozka .= "</tr>";
                  $zprava .= $_SESSION['pocet_'.strip_tags($zbozi->id->asXML()).''].'x '. strip_tags($zbozi->nazev->asXML()) . ', ' . $castka_." Kč\n"; 
              $castka = $castka + $castka_;
              $celkem= $castka + $doprava;
                  print $polozka; 
            }            
          }
      print '<tr><td>Celkem za výrobky</td><td>'.$castka .' Kč</td></tr>';
      print '<tr><td>Celkem vč. dopravy</td><td>'.$celkem .' Kč</td></tr>';
      print '</table>';
      ?>

The "$ doprava" variable is defined in the file "prepocitej.php".

if(!empty($_POST["calc"]))
  $doprava = 300;
else
  $doprava = 200;  

I apologize for my English and ask for help.

Thank you!




Aucun commentaire:

Enregistrer un commentaire