I have a form which I need add together a number from a previous calculation with a number that the user inputs but I can't seem to get CapEx recognised as a number! Can anyone help me please! I have put the code below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script></script>
<script type="text/javascript">
<!--
function resettoggle() {
var e = document.getElementById('foo');
e.style.display = 'none';
}
function toggle_visibility(id) {
var e = document.getElementById(id);
if(e.style.display == 'none')
e.style.display = 'block';
else
e.style.display = 'none';
}
//-->
<a href="#" onclick="toggle_visibility('foo');">Click here if the Total Cost of Change is less than £5k</a><br />
<div id="foo">
<tr>
<td colspan="5"><h3>If the Total Cost of Change is less than £5k, please complete the following fields...</h3></td>
<td></td>
</tr>
<tr>
<td><div class="fieldlabel">Reasons for Change:</div></td>
<td></td>
<td><textarea name="userstringvalue(8requestfcreasons)" rows="3" class="datafield"></textarea></td>
<td></td>
<td><div class="errormsg"></div></td>
</tr>
<tr>
<td><div class="fieldlabel">Known RIDAs:</div></td>
<td></td>
<td><textarea name="userstringvalue(9requestfcrida)" rows="3" class="datafield"></textarea></td>
<td></td>
<td><div class="errormsg"></div></td>
</tr>
<tr>
<td><div class="fieldlabel">Resource Requirements:</div></td>
<td></td>
<td><textarea name="userstringvalue(10requestfcresource)" rows="3" class="datafield"></textarea></td>
<td></td>
<td><div class="errormsg"></div></td>
</tr>
<tr>
<td><div class="fieldlabel">Additional Info:</div></td>
<td></td>
<td><textarea name="userstringvalue(11requestfcaddinfo)" rows="2" class="datafield"></textarea></td>
<td></td>
<td><div class="errormsg"></div></td>
</tr>
<tr><td colspan="5"><hr/></td></tr>
<tr>
<td colspan="5">
</div>
<br />
<tr>
<td colspan="56"><h3>If the Total Cost of Change is more than £5k, please attach a Project Brief to this request.<br><a title="Project Brief" href="file:///S:/!TBPM%20Templates/1.%20Selecting%20Business%20Change/Project%20Brief%20Template%20v1.0.doc" target="_blank">Click here</a> to view the Project Brief template from the <a title="Processes and Controls" href="http://nrfour/files/Intranet%20Spec%20V3.htm" target="_blank">Processes & Controls area</a> of the intranet.</h3></td>
<td></td>
</tr>
<br />
<input type="submit" name="LogEvent" value="Submit" onclick="setDispatchTarget('logEvent');" class="button">
1. I don't see any javascript functions other then the toggle visibility ones
2. If you use [code] tags it is much easier to read
3. There is no need for table based layouts
4. Why are you using XHTML if your not going to a) write valid XHTML and b) not going to serve it as application/xhtml-xml?
Now that I said that, post some of the javascript functions and maybe we can see whats up.
I was given this code to modify and I believe the form sits within a jsp page! The total man days calc. works fine but I can't seem to get a total when I try to add the two vaues together! see below! It doesn't seem to add it as a number? I'm now off to cry!
[code]
<tr>
<td><div class="fieldlabel">Total Man Days:</div></td>
<td></td>
<td><textarea name="manDays" rows="1" class="datafield" onBlur="Resource.value= '£' +(manDays.value * 400)"></textarea></td>
<td></td>
<td><div class="errormsg"></div></td>
</tr>
Its ok I'm just letting you know for future reference, so you know this is not the right way to accomplish this. Also, when using [code], you need a [ / code] (no spaces). Here's how I would do it.
PHP Code:
//all this needs to be in <script> tags at the top
function setResource (val) {
var resource = document.getElementById("resource"); //get the input field
val *= 400; //multiply the value by 400;
resource.value = "$" +val; //set the value
//dont know how to make a pound sign lol
}
function setTotal(val) {
var resourceVal = document.getElementById("resource").value; //get the value
var total = document.getElemetnById("total");
val = val.replace(/$/, ''); //remove the "pound" sign
val = parsefloat(val) //convert to a floating point number (with decimals)
//if its only integers, you can use "parseint(val)"
val += resourceVal; //add the values
total.value = '$' +val;
}
Then change the following in your HTML:
HTML Code:
<textarea name="manDays" rows="1" class="datafield" onblur="setResource(this.value)"></textarea><!-- xhtml requires all lowercase letters and the use of "id" instead of "name"--><input type="text" id="resource" size="16"><textarea name="CapEx" rows="1" class="datafield" onblur="setTotal(this.value)"></textarea><input type="text" id="total" size="16">
There are much better ways for this whole thing to be set up, but since you got handed a mess I figured I'd just show you how I would make it work.
Most of the errors had to do with letter casing being wrong. Make both input id's all lowercase. Also for some reason the replace wasn't replacing the $ sign, so I put in a splice instead.
Code:
function setResource (val) {
var resource = document.getElementById("resource"); //get the input field
val *= 400; //multiply the value by 400;
resource.value = "$" +val; //set the value
}
function setTotal(val) {
var resourceVal = document.getElementById("resource").value; //get the value
var total = document.getElementById("total");
var rVal = resourceVal.slice(1); //remove the "pound" sign
alert(rVal)
resourceVal = parseInt(rVal) //convert to a floating point number (with decimals)
//if its only integers, you can use "parseint(val)"
val += rVal; //add the values
alert (val)
total.value = '$' +val;
}
Yes, this works great but I'm still encountering the same problem with what I started with, it seems to be adding the numbers as strings and not adding them together as numbers? sorry if I'm being a pain, I do really appreciate your help!
Oh I see the problem. For some reasone the code I copied and pasted wasn't the same as the one I had. Fixed:
[code]
function setTotal(val) {
var resourceVal = document.getElementById("resource").value; //get the value
var total = document.getElementById("total");
var rVal = resourceVal.slice(1); //remove the "pound" sign
alert(rVal)
rVal = parseInt(rVal) //convert to a floating point number (with decimals)
//if its only integers, you can use "parseint(val)"
val += rVal; //add the values
alert (val)
total.value = '$' +val;
}
You didn't use the updated code I posted. In the code you posted the parseInt(rVal) is being stored as resourceVal, which is incorrect. It should be rVal=parseInt(rVal)
Bookmarks