[SOLVED] Calc function

This is a discussion on "[SOLVED] Calc function" within the JavaScript Forum section. This forum, and the thread "[SOLVED] Calc function are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > JavaScript Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Nov 3rd, 2007, 13:03
Junior Member
Join Date: Nov 2007
Location: Norway
Age: 23
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Calc function

Hi please check my wepage www.ernis.no , click on a picture>scroll down>konfigurer>click on a picture>select a radio button. On the bottom right side is a calculator:
PHP: Select all

$totalpris total($maskin_id);
$mva =  $totalpris 0.23;
$umva $totalpris $mva
"mva is tax". What i need is a function that adds chosen radio button value($row[4]) to the price($totalpris) and update the price. I already have a onlick command that shows the value($row[4]) of selected button:
PHP: Select all

while($row mysql_fetch_row($result)) {
     if (
$i == 0){
      echo(
"<tr><td><input name='$row[4]' type='radio' onclick='alert(this.name)' checked value='$valgt'></td><td>$row[1]</td><td align='right' style='font-weight: bold;' colspan='2'>$row[4] ,-</td></tr>");
$tillegg $row[4];
$i 1;
 
     } else 
 
 
     echo(
"<tr><td width=10><input name='$row[4]' type='radio' onclick='alert(this.name)' value='$valgt'></td><td width=500>$row[1]</td><td width='60' align='right' style='color: green;' colspan='2'>+$row[4] ,-</td></tr>");
   
$valgt++;

I've tried to make several function but i really dont have a clue when it comes to javascript
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Nov 3rd, 2007, 14:32
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Okay so you want to do something like this -- assuming that you are echoing everything to the page with PHP

PHP: Select all



echo '
<script type="text/javascript">
var totalPrice = '
$totalPrice';

function workoutnewPrice(passedObject)
{
    if (passedObject.checked && !isNaN(parseInt(passedObject.value)) {
        totalPrice += parseInt(passedObjec.value);
        alert(totalPrice);
    }  
}
</script>'

Then on your radio buttons add onclick="workoutnewPrice(this)"

Hope that helps,
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Nov 4th, 2007, 13:43
Junior Member
Join Date: Nov 2007
Location: Norway
Age: 23
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Quote:
Originally Posted by Rakuli View Post
Okay so you want to do something like this -- assuming that you are echoing everything to the page with PHP
This is my code now. Page loads but gives syntax error. If I use double quotes the page won't load. Now every radio button i select stays checked but price doesn't update. I changed ur
PHP: Select all

var totalPrice ', $totalPrice, ' 

so it matches the calc variable
PHP: Select all

$totalpris total($maskin_id); 

.


PHP: Select all

while($row mysql_fetch_row($result)) {
     if (
$i == 0){
      echo(
"<tr><td><input name='$row[4]' type='radio' onclick='workoutnewPrice(this)' checked value='$valgt'></td><td>$row[1]</td><td align='right' style='font-weight: bold;' colspan='2'>$row[4] ,-</td></tr>");
$tillegg $row[4];
$i 1;
 
     } else 
 
 
     echo(
"<tr><td width=10><input name='$row[4]' type='radio' onclick='workoutnewPrice(this)' value='$valgt'></td><td width=500>$row[1]</td><td width='60' align='right' style='color: green;' colspan='2'>+$row[4] ,-</td></tr>");
   
$valgt++;
} echo(
"</table>");
   } else echo(
"ingen data i db: $valg ");
  }
 
echo(
"
<script language='JavaScript' type='text/javascript'>
var totalpris = ', $totalpris, ';
function workoutnewPrice(passedObject)
{
    if (passedObject.checked && !isNaN(parseInt(passedObject.value)) {
        totalpris += parseInt(passedObjec.value);
        alert(totalpris);
    }  
}
</script>
"
); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Nov 4th, 2007, 18:34
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

It doesn't make sense to me

this line

var totalPrice = ', $totalPrice, '

if this is in PHP it is wrong and it it is not in PHP it is still wrong in javascript....

In the code I gave you, you have to use
echo '
var totalPrice = ', $totalPrice, ';'; if you are using PHP or

var totalPrice = '<?php echo $totalPrice;?> ';

if you are coming in and out of PHP.


The whole code for the javascript function should be


PHP: Select all

echo '

<script language="JavaScript" type="text/javascript">
var totalpris = '
$totalpris';
function workoutnewPrice(passedObject)
{
    if (passedObject.checked && !isNaN(parseInt(passedObject.value)) {
        totalpris += parseInt(passedObjec.value);
        alert(totalpris);
    }  
}
</script>'

Don't use parenthesis around the echo statement as it is not a function.
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Nov 5th, 2007, 08:14
Junior Member
Join Date: Nov 2007
Location: Norway
Age: 23
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Quote:
Originally Posted by Rakuli View Post
It doesn't make sense to me

this line

var totalPrice = ', $totalPrice, '

if this is in PHP it is wrong and it it is not in PHP it is still wrong in javascript....

In the code I gave you, you have to use
echo '
var totalPrice = ', $totalPrice, ';'; if you are using PHP or

var totalPrice = '<?php echo $totalPrice;?> ';

if you are coming in and out of PHP.
I replaced with ur code, does the same as before. Every button i click stays checked but price doesn't change. Expected object line 147 is the error message. Line 147 is only <tr>. Your function is now called nyPris. Here is complete code:

PHP: Select all

<?php
require_once("fro.php");
if(
$_GET['maskin_id'])$maskin_id $_GET['maskin_id'];
else 
header('Location: index.php');
  
$Choo = new mysql();
  
$Choo->connect();
  
$result $Choo->query("SELECT * FROM `maskin` WHERE maskin_id=$maskin_id");
  
$row mysql_fetch_row$result );
  
$text str_replace(chr(13).chr(10),"<br />\n",$row[6]);
 
 function 
listDBvalg($valg) {
   if(
$_GET['maskin_id'])$maskin_id $_GET['maskin_id'];
   
$Choo = new mysql();
   
$Choo->connect();
 
   
$result $Choo->query("SELECT * FROM `$valg` WHERE maskin_id=$maskin_id ORDER BY pris");
   if(
mysql_num_rows($result)) {
    echo (
"<table width='420' cellspacing='0' cellpadding='0' align='center'>");
    
$i 0;
    
$valgt 0;
 
while(
$row mysql_fetch_row($result)) {
     if (
$i == 0){
      echo(
"<tr><td><input name='$row[4]' type='radio' onclick='nyPris(this)' checked value='$valgt'></td><td>$row[1]</td><td align='right' style='font-weight: bold;' colspan='2'>$row[4] ,-</td></tr>");
$tillegg $row[4];
$i 1;
 
     } else 
 
 
     echo(
"<tr><td width=10><input name='$row[4]' type='radio' onclick='nyPris(this)' value='$valgt'></td><td width=500>$row[1]</td><td width='60' align='right' style='color: green;' colspan='2'>+$row[4] ,-</td></tr>");
   
$valgt++;
} echo(
"</table>");
   } else echo(
"ingen data i db: $valg ");
  }
 
echo 
'
<script language="JavaScript" type="text/javascript">
var totalpris = '
$totalpris';
function nyPris(passedObject)
{
    if (passedObject.checked && !isNaN(parseInt(passedObject.value)) {
        totalpris += parseInt(passedObjec.value);
        alert(totalpris);
    }  
}
</script>'

 
 
 function 
komponent($valg){
  if(
$_GET['maskin_id'])$maskin_id $_GET['maskin_id'];
  
$Choo = new mysql();
  
$Choo->connect();
  
$result $Choo->query("SELECT * FROM `$valg` WHERE maskin_id=$maskin_id ORDER BY pris");
  if(
mysql_num_rows($result)){
   
$row mysql_fetch_row$result );
   echo(
$row[1]);
  } 
 }
 
 
$totalpris total($maskin_id);
$mva =  $totalpris 0.23;
$umva $totalpris $mva;
topp();
echo(
"
<script language='JavaScript' type='text/javascript'>
 
function toggleLayer( whichLayer ){  
  layersOff();
  if (document.getElementById( whichLayer ).style.display == 'block')
   document.getElementById( whichLayer ).style.display = 'none';
  else document.getElementById( whichLayer ).style.display = 'block';
 }
 function layersOff(){
  document.getElementById('cpu').style.display = 'none';
  document.getElementById('os').style.display = 'none';
  document.getElementById('harddisk').style.display = 'none';
  document.getElementById('ram').style.display = 'none';
  document.getElementById('skjermkort').style.display = 'none';
 
 
 }
 function konfig(){
  document.getElementById('konfigellersalg').style.display = 'none';
  document.getElementById('knapper').style.display = 'block';
  document.getElementById('vinduer').style.display = 'block';
  layersOff();
 }
</script>
"
);
topp_meny();
echo(
"<form name='form1'>
<table width='100%' height='100%' cellpadding='0' cellspacing='0' border='0' bgcolor='white'  style='border: 1px solid black; font-size: small;'>
  <tr>
    <td align='center'>&nbsp;</td>
    <td align='left'><b style='font-size: xx-large;'>$row[1]</b></td>
   <td align='center'>&nbsp;</td>
  </tr>
   <tr>
    <td width='30%'>&nbsp;</td>
    <td width='50%'><p align='left'>$text</p></td>
 <td widht='20%'><img src='$row[5]'></td>
  </tr>
   <tr>
    <td width='30%' align='right'>Hovedkort:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("hovedkort"); echo("</td>
  </tr>
   <tr>
    <td width='30%' align='right'>Prosessor/CPU:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("cpu"); echo("</td>
  </tr>
 
  <tr>
    <td width='30%' align='right'>Optisk enhet:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("optisk"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Harddisk:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("harddisk"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Ram:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("ram"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Skjermkort:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("skjermkort"); echo("</td>
  </tr>
   <tr>
    <td width='30%' align='right'>Tvkort:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("tvkort"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Kabinett:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("kabinett"); echo("</td>
  </tr>
   <tr>
    <td width='30%' align='right'>Operativsystem:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("os"); echo("</td>
  </tr>
   <tr>
    <td width='30%' align='right'>Strømforsyning:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("strømforsyning"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Høyttaler:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("hoyttaler"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Tastatur og mus:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("tastaturogmus"); echo("</td>
  </tr>
    <tr>
    <td width='30%' align='right'>Skjerm:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("skjerm"); echo("</td>
  </tr>
      <tr>
    <td width='30%' align='right'>Kortleser:&nbsp;&nbsp;&nbsp;&nbsp;</td>
    <td width='50%' colspan='2' align='left'>"
);komponent("kortleser"); echo("</td>
  </tr>
 
 
  <tr>
    <td colspan='3'>
  <div id='komponentwrapper'>
   <div id='konfigellersalg'>
    <div id='konfig'><a href='#' onclick='javascript: konfig();'>[Konfigurer]</a></div><div id='salg'><a href='kjøp.php'>[Kjøp]</a></div>
   </div>
   <div id='knapper' style='display: none'>
    <div id='knapp_os'><img src='http://www.cfm-guadalajara.net/dotclear/images/Logiciels_Microsoft/VistaLogo.jpg' onClick='javascript:toggleLayer(\"os\");'></div>
    <div id='knapp_cpu'><img src='http://www.cyberzone-net.jp/images/cpu.gif' onClick='javascript:toggleLayer(\"cpu\");'></div>
    <div id='knapp_harddisk'><img src='img/disk.jpg' onClick='javascript:toggleLayer(\"harddisk\");'></div>
    <div id='knapp_ram'><img src='img/ram.jpg' onClick='javascript:toggleLayer(\"ram\");'></div>
    <div id='knapp_skjermkort'><img src='img/skjermkort.jpg' onClick='javascript:toggleLayer(\"skjermkort\");'></div>
   </div>
 
 
 
 <div id='vinduer' style='display: none'>
  <div id='os'> "
); 
  
listDBvalg("os");
  echo(
"</div><div id='cpu'>");
   
listDBvalg("cpu");
  echo(
"</div><div id='harddisk'>");
   
listDBvalg("harddisk");
  echo(
"</div><div id='ram'>");
   
listDBvalg("ram");
  echo(
"</div><div id='skjermkort'>");
   
listDBvalg("skjermkort");
 
  echo(
"</div>
  </div>
  </td>
</tr>
<tr valign='bottom' align='center' colspan='1'>
 <td></td>
 <td></td>
 <td><b style='color: black; font-size: x-medium'>U/Mva : $umva ,-</b></td>
</tr>
<tr valign='bottom' align='center' colspan='1'>
 <td></td>
 <td></td>
 <td><b style='color: black; font-size: x-medium'>Mva : $mva ,-</b></td>
</tr>
<tr valign='bottom' align='center' colspan='1'>
 <td></td>
 <td></td>
 <td><b style='color: green; font-size: x-medium'>Totalpris : $totalpris ,-</b></td>
</tr>
</table>
</form>
"
);
bunn();
?>

Last edited by Pugger; Nov 5th, 2007 at 08:20.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Nov 5th, 2007, 09:20
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Every radio button needs to have the same name if you want only one to remain selected..

There was also a typo in the Javascript code.

PHP: Select all

while($row mysql_fetch_row($result)) {

     if (
$i == 0){
      echo(
"<tr><td><input name='totalIncreaser' type='radio' onclick='nyPris(this)' checked value='$valgt'></td><td>$row[1]</td><td align='right' style='font-weight: bold;' colspan='2'>$row[4] ,-</td></tr>");
$tillegg $row[4];
$i 1;
 
     } else 
 
 
     echo(
"<tr><td width=10><input name='totalIncreaser' type='radio' onclick='nyPris(this)' value='$valgt'></td><td width=500>$row[1]</td><td width='60' align='right' style='color: green;' colspan='2'>+$row[4] ,-</td></tr>");
   
$valgt++;
} echo(
"</table>");
   } else echo(
"ingen data i db: $valg ");
  }
 
echo 
'
<script language="JavaScript" type="text/javascript">
var totalpris = '
$totalpris';
function nyPris(passedObject)
{
    if (passedObject.checked && !isNaN(parseInt(passedObject.value))) {
        totalpris += parseInt(passedObject.value);
        alert(totalpris);
    }  
}
</script>'

Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Nov 5th, 2007, 09:47
Junior Member
Join Date: Nov 2007
Location: Norway
Age: 23
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Quote:
Originally Posted by Rakuli View Post
Every radio button needs to have the same name if you want only one to remain selected..
Ok now selection problem is gone but price still won't change .. hmm. Expected object line 148( <td width='30%' align='right'>Kabinett:&nbsp;&nbsp;&nbsp;&nbsp;</td>). The error only occurs when i select a button. If i remove ur function i get no errors even when i select so there is something else wrong in the function/buttons.

Last edited by Pugger; Nov 5th, 2007 at 10:03.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Nov 5th, 2007, 10:26
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Try this

PHP: Select all

echo '

<script language="JavaScript" type="text/javascript">
var totalpris = '
$totalpris';
function nyPris(passedObject)
{
    if (passedObject.checked){
        totalpris += passedObject.value;
        alert(totalpris);
    }  
}
</script>'

Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Nov 5th, 2007, 10:32
Junior Member
Join Date: Nov 2007
Location: Norway
Age: 23
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Same error, no change..Is there any other way to pass the value to php? I tried to google and found out that either i need to pass the js var in a js cookie or refresh the page.

Last edited by Pugger; Nov 5th, 2007 at 10:48.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Nov 5th, 2007, 10:48
Rakuli's Avatar
SuperMember

SuperMember
Join Date: Sep 2007
Location: Australia
Age: 24
Posts: 956
Blog Entries: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

What does $totalpris actually contain? There are no errors in that code unless totalprice has an messy value.. Is it just a straight up integer?
Last Blog Entry: The wannabe juggler's quest (Oct 27th, 2007)
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Nov 5th, 2007, 11:15
Junior Member
Join Date: Nov 2007
Location: Norway
Age: 23
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Re: Calc function

Quote:
Originally Posted by Rakuli View Post
What does $totalpris actually contain? There are no errors in that code unless totalprice has an messy value.. Is it just a straight up integer?
Totalpris should be integer yes. Complete calculator function is included from another php file. I should have posted that code earlier:
PHP: Select all

function total($maskin_id$valg2){
 
   
$hovedkort pris("hovedkort"$valg2);
   
$optisk pris("optisk"$valg2);
   
$tastaturogmus pris("tastaturogmus"$valg2);
   
$kabinett pris("kabinett"$valg2);
   
$strømforsyning pris("strømforsyning"$valg2);
   
$tvkort pris("tvkort"$valg2);
   
$kortleser pris("kortleser"$valg2);
   
$hoyttaler pris("hoyttaler"$valg2);
 &