By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
462,095 Members | 788 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 462,095 IT Pros & Developers. It's quick & easy.

change value for calculation - help

cassbiz
100+
P: 202
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.

Expand|Select|Wrap|Line Numbers
  1.         <script type="text/javascript">
  2.                 <!--
  3.                 function perscount()
  4.                 {
  5.                 if (document.daten.pers.value==1){
  6.                 document.daten.preis.value = document.daten.preis1.value;}
  7.                 if (document.daten.pers.value==2){
  8.                 document.daten.preis.value = document.daten.preis2.value;}
  9.                 if (document.daten.pers.value==3){
  10.                 document.daten.preis.value = document.daten.preis3.value;}
  11.                 if (document.daten.pers.value==4){
  12.                 document.daten.preis.value = "";}
  13.                 }
  14.                         //-->
  15.         </script>
  16. <?
  17. $zdaten = getzimmer();
  18. $i=0;
  19. while  (count ($zdaten) >= $i)
  20. {
  21.         $i++;
  22.         if ($zdaten[$i][1]==$zimmer)
  23.         {
  24.                 $preis1=$zdaten[$i][2];
  25.                 $preis2=$zdaten[$i][3];
  26.                 $preis3=$zdaten[$i][4];
  27.                 $preis4=$zdaten[$i][5];
  28.                 $preis5=$zdaten[$i][6];
  29.         }
  30. }
  31. ?>
  32. <input type="hidden" name="preis1" value=<? echo "'$preis1'"?>>
  33. <input type="Hidden" name="preis2" value=<? echo "'$preis2'"?>>
  34. <input type="Hidden" name="preis3" value=<? echo "'$preis3'"?>>
  35. <input type="Hidden" name="preis4" value=<? echo "'$preis4'"?>>
  36. <input type="Hidden" name="preis5" value=<? echo "'$preis5'"?>>
  37.  
  38.  <!--took out the blah blah -->
  39.  
  40.  
  41.         <tr id="tr2">
  42.                 <td align = "center" class="t1">
  43.                         <? echo $t_forms['pers'] ?> <br>
  44.                         <select name="pers" size="1" onChange="perscount()">
  45.                         <option <? if ($pers==1) echo 'selected'; ?>>1</option>
  46.                         <option <? if ($pers==2) echo 'selected'; ?>>2</option>
  47.                         <option <? if ($pers==3) echo 'selected'; ?>>3</option>
  48.                         <option <? if ($pers==4) echo 'selected'; ?>>4</option>
  49.                 </td>
  50.         </tr>
  51.         <tr id="tr2">
  52.                 <td class="t1">
  53. <? echo $t_forms['price_day'] ?> <input name="preis" size="20" class="t1" maxlength="20" value=<?echo "'$preis1'" ?> >
  54. <?
  55.         $nächte1 = ($abdatum-$andatum)/86400;
  56.         $nächte2 = $nächte1 + 0.5;
  57.         $nächte = round($nächte2);
  58.  
  59. <!-- I would like to change the value of $pries1 to the new value given by the javascript to do the calculation. -->
  60.  
  61.         $sum1 = $nächte * $preis1;
  62.         $sum = number_format($sum1, 2);
  63.         $tax1 = $t_global['tax'];
  64.         $tax2 = $sum1 * ($tax1 * .01);
  65.         $tax = number_format($tax2, 2);
  66.         $tot1 = $sum + $tax;
  67.         $tot2 = $sum1 + $tax2;
  68.         $tot = number_format($tot2, 2);
  69. ?>
  70.                 </td>
  71.         <tr id="tr2">
  72.                 <td class="t1" colspan="2">
  73.                         <? echo "{$t_forms['count']} : $nächte"; ?>
  74.                 </td>
  75.         <tr id="tr2">
  76.                 <td class="t1" colspan="2">
  77.                         <? echo "{$t_forms['sum']} = {$t_global['currency']} $sum"; ?>
  78.                 </td>
  79.         <tr id="tr2">
  80.                 <td class="t1" colspan="2">
  81.                         <? echo "{$t_forms['tax']} {$t_global['tax']}% = {$t_global['currency']} $tax"; ?>
  82.                 </td>
  83.         <tr id="tr2">
  84.                 <td class="t1" colspan="2">
  85.                         <? echo "{$t_forms['total']} {$t_global['currency']} $tot"; ?>
  86.                 </td>
  87.         </tr>
  88.  
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
Dec 26 '06 #1
Share this Question
Share on Google+
2 Replies

ronverdonk
Expert 2.5K+
P: 4,258
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:
Dec 26 '06 #2

cassbiz
100+
P: 202
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.
Dec 26 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.