Dethman Posted October 15, 2009 Share Posted October 15, 2009 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> Quote Link to comment Share on other sites More sharing options...
haku Posted October 15, 2009 Share Posted October 15, 2009 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. Quote Link to comment Share on other sites More sharing options...
Dethman Posted October 15, 2009 Author Share Posted October 15, 2009 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> Quote Link to comment Share on other sites More sharing options...
haku Posted October 15, 2009 Share Posted October 15, 2009 You didn't even bother to read my post did you. What a waste of time. Quote Link to comment 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.