Jump to content

Price Display by How many going to be bought...


Dethman

Recommended Posts

Hello,

 

  I have been working on this for an hour and am getting frustrated, when the user types into the input box the amount of levels he/she wishes to advance the script will take the price per level and multiply by the amount typed, and then display it in the value of a submit box, please help with the code, I am confuzed and puzzled by why it is not working, my code is below, thank you to whomever counsels this code.

 

Javascript:
<SCRIPT language=javascript type=text/javascript>

function addCovertLevel()
{
var levelsUp, price, fullPrice;

levelsUp = document.getElementById('lvlup');
price    = document.getElementById('price');

    fullPrice = ( levelsUp * price );
    
    
    document.getElementById('spyUpgrade').value = ""+fullPrice+" Gold";

    
    

}
	</SCRIPT>

 

HTML/PHP:
<TR>
          <TD style="PADDING-RIGHT: 25px" vAlign=top>
            <FORM action=train.php method=post>
            <TABLE class=table_lines cellSpacing=0 cellPadding=6 width="100%" 
            border=0>
              <TBODY>
              <TR>
                <TH align=middle colSpan=4>Upgrade Spy Skill</TH></TR>
              <TR>
                <TR><TD class=subh>Current Spy Skill</TD><TD>Level <? echo $user->currentSpySkill?></TD></TR>
                <input type="hidden" name="price" value="<? echo $pris = ($user->currentSpySkill*12000);?>" id="price">
                <TR><TD class=subh>Ammount</TD> <TD><input type="text" name="lvlup" value="0" id="lvlup" size="5" onchange="addCovertLevel();"></TD></TR>
                <TR><TD class=subh>Upgrade</TD> <b><TD><INPUT type=submit id="spyUpgrade" size=5 style="color:green;" value="0 Gold" name=spyupgrade></TD></TR>
                </TR>

 

 

Link to comment
Share on other sites

I'll point you in the right direction. First change this:

 

<SCRIPT language=javascript type=text/javascript>

 

to this:

 

<script type="text/javascript">

 

The 'language' attribute is deprecated, and you need quotes around the attribute values. Also, if you are using an XHTML doctype, you cannot use capital letters for the tag name, though if you are using an HTML doctype it is fine.

 

Next:

 

   price    = document.getElementById('price');

 

First, you need to use:

 

var price = .....

 

Until then price isn't anything. It may work without the 'var', but some browsers probably won't like it. Finally:

 

var price    = document.getElementById('price');

 

In this case, 'price' will not be a number, it will be an input tag. You have found the input tag using getElementById, but you need to get the value from that element tag. You can use getAttribute() for this.

 

Good luck.

Link to comment
Share on other sites

Update on Code, it still does not work but I belive I am getting closer to a solution the problem with this code is that it creates another input box on a close but different part of the page worse yet it says "NaN Gold" Instead of a Number like it should say, well anyhow here is the code:

 

Javascript:
<SCRIPT language=javascript type=text/javascript>
function addCommas(nStr)
{
nStr += '';
x = nStr.split('.');
x1 = x[0];
x2 = x.length > 1 ? '.' + x[1] : '';
var rgx = /(\d+)(\d{3})/;
while (rgx.test(x1)) {
	x1 = x1.replace(rgx, '$1' + ',' + '$2');
}
return x1 + x2;
}
function addCovertLevel()
{
var levelsUp, price, fullPrice, goldOnhand;

levelsUp   = document.getElementById('lvlup');
goldOnhand = document.getElementById('goldOnhand');
price      = document.getElementById('price');

    fullPrice = ( levelsUp * price );
    
    if(fullPrice > goldOnhand){
     
	document.getElementById('spyUpgradeDiv').innerHTML = "<TD><INPUT type=submit id='spyUpgrade' size='5' style='color:red;' value='Not Enough Gold' name=nogo></TD>";

}else{
		document.getElementById('spyUpgradeDiv').innerHTML = "<TD><INPUT type=submit id='spyUpgrade' size='5' style='color:green;' value='"+addCommas(fullPrice)+" Gold' name='spyupgrade'></TD>";

}
    

    
    

}
	</SCRIPT>

 

HTML/PHP:
<TR>
          <TD style="PADDING-RIGHT: 25px" vAlign=top>
            <FORM action=train.php method=post>
            <TABLE class=table_lines cellSpacing=0 cellPadding=6 width="100%" 
            border=0>
              <TBODY>
              <TR>
                <TH align=middle colSpan=4>Upgrade Spy Skill</TH></TR>
              <TR>
                <TR><TD class=subh>Current Spy Skill</TD><TD>Level <? echo $user->currentSpySkill?></TD></TR>
                
                <TR><TD class=subh>Ammount</TD> <TD><input type="text" name="lvlup" value="1" id="lvlup" size="5" onchange="addCovertLevel()"></TD></TR>
                <TR><TD class=subh>Upgrade</TD> <b>
			<?php
			$goldOnhand = $user->gold;
			$prise = ($user->currentSpySkill*12000);
			?>
<input type="hidden" name="price" value="<? echo $prise;?>" id="price">
<input type="hidden" name="goldOnhand" value="<? echo $goldOnhand;?>" id="goldOnhand">

   <div id="spyUpgradeDiv">
			<TD>
<INPUT type=submit id="spyUpgrade" size=5 style="color:<?if($price > $goldOnhand){echo 'red';}else{echo 'green';}?>" value="<? echo numecho($prise);?> Gold" name=spyupgrade>

                 </TD>
			</div>
			</TR>
                </TR>

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.