candid Posted October 6, 2014 Share Posted October 6, 2014 hi, I'm new to php. I tried to get sum of two variables using java script. Value of one variable come from a php variable, Other one is a static value. here is the code $price_per_day=100; it displays correctly. but when i try to add it with another variable it displays nothing. Please help me. <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>test</title> <script> var total = 0; var total_extra =<?php echo $price_per_day; ?> var sum1=0; function test(item){ if(item.checked){ total+= parseInt(item.value); }else{ total-= parseInt(item.value); } //alert(total); sum1=parseInt(total)+parseInt(total_extra); document.getElementById('Totalcost').innerHTML = total + " /-"; document.getElementById('car_extra_total').innerHTML=sum1 ; } </script> </head> <body> <div class="checkbox"> <label> <input type="checkbox" name="channelcost" value="10" onClick="test(this);">Excess Protection<span class="right grey">10</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="channelcost" value="50" onClick="test(this);">Additional Driver<span class="right grey">50</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="channelcost" value="30"onClick="test(this);">GPS<span class="right grey">30</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="channelcost" value="30" onClick="test(this);">Child Booster Seat <span class="right grey">30</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="channelcost" value="20" onClick="test(this);">Child Seat <span class="right grey">20</span> </label> </div> <div class="checkbox"> <label> <input type="checkbox" name="channelcost" value="10" onClick="test(this);">Baby Seat <span class="right grey">10</span> </label> </div> <div class="padding20"> <table class="wh100percent size12 bold dark"> <tr> <td>Extras total</td> <td class="textright">$<span id="Totalcost"> </span></td> </tr> </table> <div class="fdash mt10"></div><br/> <span class="size14 dark bold">Total price:</span> <span class="size24 green bold right margtop-5">$<span id="car_extra_total"></span></span> </div> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/291465-get-sum-of-two-variables-using-javascript-not-working/ Share on other sites More sharing options...
Cyto Posted October 6, 2014 Share Posted October 6, 2014 (edited) It sounds like your content file(.html) is not a .php file? Or you forgot to include the php code ($price_per_day=100;) where you called your variable inside your Javascript code. Edited October 6, 2014 by Cyto Quote Link to comment https://forums.phpfreaks.com/topic/291465-get-sum-of-two-variables-using-javascript-not-working/#findComment-1492856 Share on other sites More sharing options...
cyberRobot Posted October 7, 2014 Share Posted October 7, 2014 The code works for me after changing this: var total_extra =<?php echo $price_per_day; ?> To this: var total_extra =<?php echo $price_per_day; ?>; Note the semi-colon at the end. Quote Link to comment https://forums.phpfreaks.com/topic/291465-get-sum-of-two-variables-using-javascript-not-working/#findComment-1492941 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.