change value for calculation - help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cassbiz
    New Member
    • Oct 2006
    • 202

    change value for calculation - help

    I may be in the wrong forum so Ronald don't shoot :)

    In my code I have an option box to choose a number - works fine. I want to carry over the new value to another field to do a recalculation.

    Primarily the field "$preis1" to change with the javascript.

    Here is the code.

    Code:
            <script type="text/javascript">
                    <!--
                    function perscount()
                    {
                    if (document.daten.pers.value==1){
                    document.daten.preis.value = document.daten.preis1.value;}
                    if (document.daten.pers.value==2){
                    document.daten.preis.value = document.daten.preis2.value;}
                    if (document.daten.pers.value==3){
                    document.daten.preis.value = document.daten.preis3.value;}
                    if (document.daten.pers.value==4){
                    document.daten.preis.value = "";}
                    }
                            //-->
            </script>
    <?
    $zdaten = getzimmer();
    $i=0;
    while  (count ($zdaten) >= $i)
    {
            $i++;
            if ($zdaten[$i][1]==$zimmer)
            {
                    $preis1=$zdaten[$i][2];
                    $preis2=$zdaten[$i][3];
                    $preis3=$zdaten[$i][4];
                    $preis4=$zdaten[$i][5];
                    $preis5=$zdaten[$i][6];
            }
    }
    ?>
    <input type="hidden" name="preis1" value=<? echo "'$preis1'"?>>
    <input type="Hidden" name="preis2" value=<? echo "'$preis2'"?>>
    <input type="Hidden" name="preis3" value=<? echo "'$preis3'"?>>
    <input type="Hidden" name="preis4" value=<? echo "'$preis4'"?>>
    <input type="Hidden" name="preis5" value=<? echo "'$preis5'"?>>
    
     <!--took out the blah blah -->
    
    
            <tr id="tr2">
                    <td align = "center" class="t1">
                            <? echo $t_forms['pers'] ?> <br>
                            <select name="pers" size="1" onChange="perscount()">
                            <option <? if ($pers==1) echo 'selected'; ?>>1</option>
                            <option <? if ($pers==2) echo 'selected'; ?>>2</option>
                            <option <? if ($pers==3) echo 'selected'; ?>>3</option>
                            <option <? if ($pers==4) echo 'selected'; ?>>4</option>
                    </td>
            </tr>
            <tr id="tr2">
                    <td class="t1">
    <? echo $t_forms['price_day'] ?> <input name="preis" size="20" class="t1" maxlength="20" value=<?echo "'$preis1'" ?> >
    <?
            $nächte1 = ($abdatum-$andatum)/86400;
            $nächte2 = $nächte1 + 0.5;
            $nächte = round($nächte2);
    
    <!-- I would like to change the value of $pries1 to the new value given by the javascript to do the calculation. -->
    
            $sum1 = $nächte * $preis1;
            $sum = number_format($sum1, 2);
            $tax1 = $t_global['tax'];
            $tax2 = $sum1 * ($tax1 * .01);
            $tax = number_format($tax2, 2);
            $tot1 = $sum + $tax;
            $tot2 = $sum1 + $tax2;
            $tot = number_format($tot2, 2);
    ?>
                    </td>
            <tr id="tr2">
                    <td class="t1" colspan="2">
                            <? echo "{$t_forms['count']} : $nächte"; ?>
                    </td>
            <tr id="tr2">
                    <td class="t1" colspan="2">
                            <? echo "{$t_forms['sum']} = {$t_global['currency']} $sum"; ?>
                    </td>
            <tr id="tr2">
                    <td class="t1" colspan="2">
                            <? echo "{$t_forms['tax']} {$t_global['tax']}% = {$t_global['currency']} $tax"; ?>
                    </td>
            <tr id="tr2">
                    <td class="t1" colspan="2">
                            <? echo "{$t_forms['total']} {$t_global['currency']} $tot"; ?>
                    </td>
            </tr>
    Is there a real easy way to put that calculation into JavaScript so that it will change per the 'OnChange'? If so, I don't know very much about javascript.

    Thanks in Advance
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    No, there is no easy way. JavaScript runs client-side and PHP at the server. (the code being in one script does not mean that they run on the same machine!). So you'll have to set up a communication between client and server. There are ways to do that, but you'll have to redesign your script for that.

    You could start at the JS side and then put a httpRequest call to the backend PHP with the price passed. Only then could the php script execute.

    I wonder if that is what you meant by an easy way? If so, you can pursue this using httpRequest (or Ajax). Good luck!

    Ronald :cool:

    Comment

    • cassbiz
      New Member
      • Oct 2006
      • 202

      #3
      Do you know of a tutorial or possibly helping me out in changing the calculation over to js.

      The value is not stored in a db it is only a visual on the screen and for print.

      Comment

      Working...