Jump to content

[SOLVED] Dynamic Values in Function


dfowler

Recommended Posts

Hey guys, I'll get right down to it.  Here is the code:

function refreshPrice() {
  var selectedradio;
  for (i=0;i<document.getElementById('appform').elements.length;i++) {
    ele = document.getElementById('appform').elements[i];
    if (ele.type == 'radio' && ele.name == 'coverage' && ele.checked) {
      selectedradio = ele;
      break;
    }
  }
  price = (document.getElementById('equipment').checked ? 225 : 0) + (document.getElementById('technical').checked ? 0 : 0) + (document.getElementById('personal').checked ? 48 : 0) + pricekeys[selectedradio.value];
  document.getElementById('totalprice').innerHTML = '$'+price.toString();
}

 

I need the equipment(225), technical(0), and personal(48) to be modified depending on what their value is in a database.  Anybody know how to do this?

Link to comment
https://forums.phpfreaks.com/topic/81384-solved-dynamic-values-in-function/
Share on other sites

In your php file

price = (document.getElementById('equipment').checked ? <?PHP echo $EquipValue;?> : 0) + (document.getElementById('technical').checked ? <?PHP echo $TechValue;?> : 0) + (document.getElementById('personal').checked ? <?PHP echo $PersValue;?> : 0) + pricekeys[selectedradio.value];

 

given the $EquipValue, $TechValue and $PersValue is the value from your db.

You can include your "price" variable in your PHP page and then link to your External JavaScript or you can convert you JS file to a PHP file and set the header to be a JavaScript file; then link it like you would any other external JS file - except your script extension would no longer be "js"; it would be "php". It's up to you how you want to do it; the first way, I mentioned, is probably the easiest way.

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.