Jump to content

Variable value return problem in php


yeyene

Recommended Posts

Hello,
 
I start in PHP and after having worked 2 days on a problem, I come to see if someone would have a light to guide me on my calculator.
I'm developing a mini calculator to display the price of renting (with ajax) of nautical equipment.
The goal here is to display the right price depending on the length of the rental period, either in half day or full day. So if the selection is made on day, the price must be multiplied by 2.
 
The problem is that I can't return the correct value of the selections. I have a defined value that appears, but it does not change when I select another material. I'm a little lost for the job!
I lower the code portions further
Thank you in advance for your clarification!
 
index.html
<meta charset="utf-8">
<html lang="fr">
<script src="js/jquery-1.11.3.js"></script>
<script src="js/jquery-ui.js"></script>
<script src="js/jquery.timepicker.js"></script>
 
<link rel="stylesheet" href="css/jquery-ui.css">
<link rel="stylesheet" href="css/style.css">
<link rel="stylesheet" href="css/jquery.timepicker.css">
<link rel="stylesheet" href="css/print.css" type="text/css" media="print" />
 
<script>
    
   
  jQuery(window).load(function () {
     
    /* Code to disable enddate before the startdate, so user must select end date greater then start date */
    jQuery("#startdate").datepicker({
      minDate: 0,
      dateFormat: 'dd.mm.yy',
      onSelect: function(selected) {
        jQuery("#enddate").datepicker("option","minDate", selected)
        calculateprice();  
      }
    });
     
     
    /* Set the start date as today by default and end date as today +days */
    jQuery( "#startdate" ).datepicker('setDate', '+0');
     
     
     
    /* Do the first default calculation on page load */
    calculateprice();
     
     
    /* Recalculate price en fonction de la durée */
    jQuery( "#matduree" ).change(function() {
      calculateprice();
    });
     
    /* Recalculate price on category change */
    jQuery( "#matcategory" ).change(function() {
      calculateprice();
    });        
     
    /* Recalculate price on when start date get change */
    jQuery('#startdate').change(function() {
      calculateprice();
    });
     
     
    /* Recalculate price on when Car cleaning return gets change */
    jQuery( "#return_cleaning" ).change(function() {
      calculateprice();
    });
 
 
  });
  function validateDate(validationcall){
    var startdate = parseDate(jQuery('#startdate').val());
    var enddate = parseDate(jQuery('#enddate').val()); 
     
    var totaldays = (enddate-startdate)/(1000*60*60*24);
     
    if(totaldays < 3){
      if(validationcall == 'startdate'){
        jQuery( "#startdate" ).datepicker('setDate', newStartDate(jQuery('#enddate').val()));
        jQuery( "#startdate" ).blur();
      }
      else if(validationcall == 'enddate'){
        jQuery( "#enddate" ).datepicker('setDate', newEndDate(jQuery('#startdate').val()));
        jQuery( "#enddate" ).blur();
      }
      alert('Vous ne pouvez pas sélectionner moins de 3 jours de réservation.\r\nContactez nous au 0690 55 92 25 ou 0590 23 20 61');
    }
    return true;
  }
   
        
  function parseDate(str) {
    var mdy = str.split('.')
    return new Date(mdy[2], mdy[1]-1, mdy[0]);
  }
   
  function newStartDate(str) {
    var mdy = str.split('.')
    return new Date(mdy[2], mdy[1]-1, mdy[0]-3);
  }
   
  function newEndDate(str) {
    var mdy = str.split('.');
    var newdate = new Date(mdy[2], mdy[1]-1, mdy[0]);
    newdate.setDate(newdate.getDate() + 3);
    return newdate;
  }
   
 
   
  function calculateprice(){
      var matcategory = jQuery('#matcategory').val();
      var matduree = jQuery('#matduree').val();
      var startdate = jQuery('#startdate').val();
      var return_cleaning = jQuery('#return_cleaning').is(':checked');
             
             
            /* Definition des images basé sur la catégorie */
      if(matcategory == 'boat'){
        var catimg = 'images/boat.png';
      }
      else if(matcategory == 'scooter'){
        var catimg = 'images/scooter.png';
      }
      else if(matcategory == 'jetski'){
        var catimg = 'images/jetski.png';
      }
      else if(matcategory == 'kayak1'){
        var catimg = 'images/kayak1.png';
      }
      else if(matcategory == 'kayak2'){
        var catimg = 'images/kayak2.png';
      }
      else if(matcategory == 'planche'){
        var catimg = 'images/planche.png';
      }
      else if(matcategory == 'kite'){
        var catimg = 'images/kite.png';
      }
      jQuery("#catimg").attr("src",catimg);
       
       
         
      jQuery('#matcategory_print').html(jQuery("#matcategory option:selected").text());
      jQuery('#matduree_print').html(jQuery("#matduree option:selected").text());
      jQuery('#matdureeendemi_print').html(jQuery("#matdureeendemi option:selected").text());
      jQuery('#startdate_print').html(jQuery('#startdate').val());
      jQuery('#return_cleaning_print').html(return_cleaning_yesno);
       
       
       
      /* AJAX CALLING */
      var data = null;
      data = 'matcategory='+matcategory;
      data += 'matduree='+matduree;
      data += '&startdate='+startdate;
      data += '&return_cleaning='+return_cleaning;
       
       
      jQuery.ajax({
        type: "POST",  
        url: "calculate.php",
        data: data,
        success: function (result) {
          jQuery('#total').html(result);
        }
      });
  }
</script>
 
<!-- Entête société -->
<div class="ste">
<img src="images/logoazn.png">
 
</div>
 
 
 
 
<h1 class="center">DEVIS LOCATION DE MATÉRIELS NAUTIQUE</h1>
<form name="adminForm" id="adminForm" method="post" action="#">
<fieldset>
<legend>INFORMATIONS DE LOCATION</legend>
 
<table>
 
  <tr>
    <td width=25% align=right><span>Type de matériels : </span></td>
         
        <select name="matcategory" id="matcategory">
            <option value="boat">BATEAU SEMI-RIGIDE (caution de 3000€)</option>
            <option value="scooter">SCOOTER DES MERS  (caution de 3000€)</option>
            <option value="jetski">JET A BRAS (caution de 3000€)</option>
            <option value="kayak1">KAYAK DES MERS MONO-PLACES (caution de 1000€)</option>
            <option value="kayak2">KAYAKS DES MERS BI-PLACES (caution de 1000€)</option>
            <option value="planche">PLANCHES A VOILE (caution de 1500€)</option>
      <option value="kite">KITE-SURF (caution de 1500€)</option>
        </select>
    </span>
   
  </tr>
         
    <tr>
        <td width=25% align=right><span>Date de récupération : </span></td>
         
            <input type="text" name="startdate" id="startdate" size="30">
        </span>
        <span class="print" id="startdate_print"></span></td>
        <td rowspan="2" width=50% align=center>
            <span><img src="" id="catimg" style="padding-top: 10px; width: 100px;"/></span>
             
        </td>
    </tr>
   
  <tr>
    <td width=25% align=right><span>Durée : </span></td>
        <td width=25% align=left><span class="no-print">
        <select name="matduree" id="matduree">
            <option value="demi">Demi journée</option>
            <option value="full">Journée entière</option>
        </select>
    </span>
  </tr>
   
 
     
</table>
</fieldset>
 
 
</form>
<div class="total">
  Total :  <label id="total" name="total">Calcul en cours...</label> €
</div>
<div class="divrow no-print">
  <button onclick="window.print(); return false;">IMPRIMER LE DEVIS</button>
</div>

calculate.php

<?php
 
#Retrive the post data
$post = $_POST;
$matcategory = $post['matcategory'];
$matduree = $post['matduree'];
 
$return_cleaning = $post['return_cleaning'];
 
 
 
 
#Variables
 
$total = calculatePrice($matcategory, $matduree);
 
$grandtotal = 0;
 
 
#Création Totale final
$grandtotal += $total;
 
 
 
#IMPRIMER LE PRIX FINAL / GRAND TOTAL
echo $grandtotal;exit;
 
 
 
 
 
function calculatePrice($matcategory, $matduree){
   
  $prixmat = 0;
   
  #prix pour boat
    if($matcategory = 'boat'){
      if($matduree = 'demi'){
        $prixmat += 180;   
      }
      else {
        $prixmat += 360;
      }
      return $prixmat;
    }
   
  #prix pour scooter
    if($matcategory = 'scooter'){
      if($matduree = 'demi'){
        $prixmat += 280;   
      }
      else {
        $prixmat += 560;
      }
      return $prixmat;
    }
     
  #prix pour scooter
    if($matcategory = 'jetski'){
      if($matduree = 'demi'){
        $prixmat += 250;   
      }
      else {
        $prixmat += 500;
      }
      return $prixmat;
    }
}
 
 
?>
Link to comment
Share on other sites

Not sure how much it will help your issue, it may. Anyway, you're missing an ampersand in front of matduree

 

      data += 'matduree='+matduree;
      data += '&startdate='+startdate;
      data += '&return_cleaning='+return_cleaning;
change to this

 

      data += '&matduree='+matduree;
      data += '&startdate='+startdate;
      data += '&return_cleaning='+return_cleaning;
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.