Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 845
  • Last Modified:

Why can't the submit button be an image?

Can anyone tell me why this code needs an input button, and not an image?

This works:
<input name="cmdCalc" type="button" id="cmdCalc" value="Calculate Benefit" onClick="CalcThis();">

This doesn't!
<input name="cmdCalc" src="calculate.gif" type="image" id="cmdCalc" onClick="CalcThis();">

Thanks for your help ;-)
// JavaScript Document
var err;
function CalcThis() {
 
	err = "";
	var taxRate = document.frmStockCalculation.slcTaxRate.value;
	var marketValue = document.frmStockCalculation.txtMarketValue.value;
	var originalCost = document.frmStockCalculation.txtOriginalCost.value;
	
	//Check empty or not number fields
	if (!taxRate) {err = "Please select your marginal tax rate.";}
	numCheck(marketValue,"current market value of your stock");
	numCheck(originalCost,"original cost of the stock");
	
	if(err) {
		alert(err);
	} else {
		taxRate = parseInt(taxRate);
		marketValue = parseInt(marketValue);
		originalCost = parseInt(originalCost);
		
		var taxCredit = marketValue * taxRate * 0.01;
		var capitalGain = (marketValue - originalCost) * 0.5 * taxRate * 0.01;
		var donation_direct = marketValue - taxCredit;
		var donation_sell = marketValue - taxCredit + capitalGain;
		
		//1. Amount of your donation
		document.getElementById("lblAmount_Direct").innerHTML = formatCurrency(marketValue);
		document.getElementById("lblAmount_Sell").innerHTML = formatCurrency(marketValue);
		
		//2. Less your tax credit
		document.getElementById("lblTaxCredit_Direct").innerHTML = formatCurrency(taxCredit * (-1));
		document.getElementById("lblTaxCredit_Sell").innerHTML = formatCurrency(taxCredit * (-1));
		
		//3. Plus tax on the capital gain
		document.getElementById("lblCapitalGain_Direct").innerHTML = formatCurrency(0);
		document.getElementById("lblCapitalGain_Sell").innerHTML = formatCurrency(capitalGain);
		
		//4. After tax cost of your donation
		document.getElementById("lblAfterTaxCost_Direct").innerHTML = formatCurrency(donation_direct);
		document.getElementById("lblAfterTaxCost_Sell").innerHTML = formatCurrency(donation_sell);
		
		//5. Net Benefit
		document.getElementById("lblNetBenefit_Direct").innerHTML = formatCurrency(donation_sell - donation_direct);
	}
}
function numCheck(num,att) {
	
	if (!err) {
		if (!num) {err = "Please enter "+att+".";}
		else if (isNaN(num)) {err = "Please enter number for "+att+".";}
	}
}
function formatCurrency(num) {
	
	var sign, cents;
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.round(num/100).toString();
	//num = Math.floor(num/100).toString();
	if(cents<10)cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num);
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
}

Open in new window

0
seanpowell
Asked:
seanpowell
1 Solution
 
contactkarthiCommented:
<A HREF="javascript:document.myform.submit()"
onmouseover="document.myform.sub_but.src='butdown.gif'"
onmouseout="document.myform.sub_but.src='butup.gif'"
onclick="return val_form_this_page()">

<IMG SRC="butup.gif"
WIDTH="143" HEIGHT="39" BORDER="0" ALT="Submit this form"
NAME="sub_but">

</A>
0
 
seanpowellAuthor Commented:
That throws an error -
Can anyone tell me why the code needs an input button, and not an image?
0
 
hieloCommented:
if <input type="button"...> works, then try changing  this:
<input name="cmdCalc" src="calculate.gif" type="image" id="cmdCalc" onClick="CalcThis();">

to this:
<input name="cmdCalc" src="calculate.gif" type="image" id="cmdCalc">
And put the funtion call on an onsubmit instead:
<form ... onsubmit="return CalcThis()">
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
hieloCommented:
type="button" IS a button - meaning it does not submit the form. type="submit" AND type="image" ARE submit buttons. So you altered the functionality of your form. If you want the image then you will need to use an onsubmit event handler on your form.
0
 
seanpowellAuthor Commented:
Well that makes perfect sence although it's not funtioning. Attached is the html if anyone could tell me where I'm going wrong...
cao-sec-button-calculate.gif
0
 
seanpowellAuthor Commented:
So much for attaching html...

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>

<script language="javascript" src="cao-sec-calculator.js"></script>

<form name="frmStockCalculation" method="post" id="frmStockCalculation" onsubmit="return CalcThis()">
            <table cellspacing="0" class="sec-contenttable">
                  <tr>
                        <td class="secmainimg">&nbsp;</td>
                        <td class="secmaindata">
                        <p><b>Calculate your net benefit of donating securities</b></p>
                        <table width="480" border="0" cellspacing="0" cellpadding="2">
                              <tr>
                                    <td>Your combined marginal tax  rate:</td>
                                    <td>
                                    <select name="slcTaxRate">
                                    <option value="">Please select</option>
                                    <option value="26">26%</option>
                                    <option value="30">30%</option>
                                    <option value="34">34%</option>
                                    <option value="38">38%</option>
                                    <option value="46">46%</option>
                                    </select>
                                    </td>
                              </tr>
                              <tr>
                                    <td>Current fair market value  of your securities:</td>
                                    <td>$ <input name="txtMarketValue" type="text" size="15" maxlength="15"></td>
                              </tr>
                              <tr>
                                    <td>Original cost of your securities:</td>
                                    <td>$ <input name="txtOriginalCost" type="text" size="15" maxlength="15"></td>
                              </tr>
                        </table>
                        <input name="cmdCalc" src="cao-sec-button-calculate.gif" type="image" id="cmdCalc">
                        </td>
                  </tr>
            </table>
            <table cellspacing="5">
                  <tr>
                        <td style="padding:5px;vertical-align:bottom;">Results</td>
                        <td style="padding:5px;vertical-align:bottom;text-align:center;"><b>Donate Securities</b></td>
                        <td style="padding:5px;vertical-align:bottom;text-align:center;"><b>Sell Securities<br>and Donate Cash</b></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Value of your donation</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAmount_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAmount_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Subtract: charitable tax credit</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblTaxCredit_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblTaxCredit_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Add: capital gains tax</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblCapitalGain_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblCapitalGain_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Net cost of donation</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAfterTaxCost_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAfterTaxCost_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#ccc;padding:5px;border:1px solid #999;"><b>Net Benefit of Your Securities Donation</b></td>
                        <td style="background:#ccc;padding:5px;border:1px solid #999;" colspan="2"><div id="lblNetBenefit_Direct"><b></b></div></td>
                  </tr>
            </table>
            </form>

</body>
</html>
0
 
hieloCommented:
Your html looks OK. Open your page throught Firefox and cheed if Firebug reports any errors.
BTW: we don't know what's in
cao-sec-calculator.js
0
 
seanpowellAuthor Commented:
The js is at the top of this page...

Beats me what this means...
Error: uncaught exception: [Exception... "Cannot modify properties of a WrappedNative"  nsresult: "0x80570034 (NS_ERROR_XPC_CANT_MODIFY_PROP_ON_WN)"  location: "JS frame :: chrome://global/content/bindings/autocomplete.xml :: onxblpopuphiding :: line 834"  data: no]
0
 
hieloCommented:
Your function does NOT have a return value. It need to return true if evrything is OK, false otherwise. Add this at the end:

return !(true && err);
0
 
seanpowellAuthor Commented:
Sorry - still no good.

The script works fine - I just need to use an image button  - why is this so difficult :-(
0
 
DauheeCommented:
how does it not work? - does it not do anything or do you get an error (need to check error console in firefox or enable error visibility in IE).
0
 
hieloCommented:
The attached code is what I tried and it works. If by "not working" you mean it is not seen on your server, then it's because your <form> is missing the action attribute - the url where the data should be submitted to.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
</head>
<body>
 
<!-- <script language="javascript" src="cao-sec-calculator.js"></script> -->
<script type="text/javascript">
// JavaScript Document
var err;
function CalcThis() {
 
	err = "";
	var taxRate = document.frmStockCalculation.slcTaxRate.value;
	var marketValue = document.frmStockCalculation.txtMarketValue.value;
	var originalCost = document.frmStockCalculation.txtOriginalCost.value;
	
	//Check empty or not number fields
	if (!taxRate) {err = "Please select your marginal tax rate.";}
	numCheck(marketValue,"current market value of your stock");
	numCheck(originalCost,"original cost of the stock");
	
	if(err) {
		alert(err);
	} else {
		taxRate = parseInt(taxRate);
		marketValue = parseInt(marketValue);
		originalCost = parseInt(originalCost);
		
		var taxCredit = marketValue * taxRate * 0.01;
		var capitalGain = (marketValue - originalCost) * 0.5 * taxRate * 0.01;
		var donation_direct = marketValue - taxCredit;
		var donation_sell = marketValue - taxCredit + capitalGain;
		
		//1. Amount of your donation
		document.getElementById("lblAmount_Direct").innerHTML = formatCurrency(marketValue);
		document.getElementById("lblAmount_Sell").innerHTML = formatCurrency(marketValue);
		
		//2. Less your tax credit
		document.getElementById("lblTaxCredit_Direct").innerHTML = formatCurrency(taxCredit * (-1));
		document.getElementById("lblTaxCredit_Sell").innerHTML = formatCurrency(taxCredit * (-1));
		
		//3. Plus tax on the capital gain
		document.getElementById("lblCapitalGain_Direct").innerHTML = formatCurrency(0);
		document.getElementById("lblCapitalGain_Sell").innerHTML = formatCurrency(capitalGain);
		
		//4. After tax cost of your donation
		document.getElementById("lblAfterTaxCost_Direct").innerHTML = formatCurrency(donation_direct);
		document.getElementById("lblAfterTaxCost_Sell").innerHTML = formatCurrency(donation_sell);
		
		//5. Net Benefit
		document.getElementById("lblNetBenefit_Direct").innerHTML = formatCurrency(donation_sell - donation_direct);
	}
return !(true && err);
}
function numCheck(num,att) {
	
	if (!err) {
		if (!num) {err = "Please enter "+att+".";}
		else if (isNaN(num)) {err = "Please enter number for "+att+".";}
	}
}
function formatCurrency(num) {
	
	var sign, cents;
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	sign = (num == (num = Math.abs(num)));
	num = Math.floor(num*100+0.50000000001);
	cents = num%100;
	num = Math.round(num/100).toString();
	//num = Math.floor(num/100).toString();
	if(cents<10)cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+','+
	num.substring(num.length-(4*i+3));
	return (((sign)?'':'-') + '$' + num);
	//return (((sign)?'':'-') + '$' + num + '.' + cents);
}
 
</script>
<form action="http://www.yoursite.com/" name="frmStockCalculation" method="post" id="frmStockCalculation" onsubmit="return CalcThis()">
            <table cellspacing="0" class="sec-contenttable">
                  <tr>
                        <td class="secmainimg">&nbsp;</td>
                        <td class="secmaindata">
                        <p><b>Calculate your net benefit of donating securities</b></p>
                        <table width="480" border="0" cellspacing="0" cellpadding="2">
                              <tr>
                                    <td>Your combined marginal tax  rate:</td>
                                    <td>
                                    <select name="slcTaxRate">
                                    <option value="">Please select</option>
                                    <option value="26">26%</option>
                                    <option value="30">30%</option>
                                    <option value="34">34%</option>
                                    <option value="38">38%</option>
                                    <option value="46">46%</option>
                                    </select>
                                    </td>
                              </tr>
                              <tr>
                                    <td>Current fair market value  of your securities:</td>
                                    <td>$ <input name="txtMarketValue" type="text" size="15" maxlength="15"></td>
                              </tr>
                              <tr>
                                    <td>Original cost of your securities:</td>
                                    <td>$ <input name="txtOriginalCost" type="text" size="15" maxlength="15"></td>
                              </tr>
                        </table>
                        <input name="cmdCalc" src="cao-sec-button-calculate.gif" type="image" id="cmdCalc">
                        </td>
                  </tr>
            </table>
            <table cellspacing="5">
                  <tr>
                        <td style="padding:5px;vertical-align:bottom;">Results</td>
                        <td style="padding:5px;vertical-align:bottom;text-align:center;"><b>Donate Securities</b></td>
                        <td style="padding:5px;vertical-align:bottom;text-align:center;"><b>Sell Securities<br>and Donate Cash</b></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Value of your donation</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAmount_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAmount_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Subtract: charitable tax credit</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblTaxCredit_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblTaxCredit_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Add: capital gains tax</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblCapitalGain_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblCapitalGain_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#fff;padding:5px;border:1px solid #e2dac4;">Net cost of donation</td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAfterTaxCost_Direct"></div></td>
                        <td style="text-align:right;background:#fff;padding:5px;border:1px solid #e2dac4;"><div id="lblAfterTaxCost_Sell"></div></td>
                  </tr>
                  <tr>
                        <td style="background:#ccc;padding:5px;border:1px solid #999;"><b>Net Benefit of Your Securities Donation</b></td>
                        <td style="background:#ccc;padding:5px;border:1px solid #999;" colspan="2"><div id="lblNetBenefit_Direct"><b></b></div></td>
                  </tr>
            </table>
            </form>
 
</body>
</html>

Open in new window

0
 
seanpowellAuthor Commented:
Hmm - the page doesn't go anywhere - can it submit to the same html page?
0
 
hieloCommented:
>>the page doesn't go anywhere
So how are you going to process the information? If it does not submit anywhere, then the data will just be on the browser and you will never get/see it on the server. At any rate, if that is what you really want, then change:
return !(true && err);

to
return false;
0
 
seanpowellAuthor Commented:
The data is only supposed to be on the browser.

Thanks, I'll try that.
0
 
seanpowellAuthor Commented:
Thanks - what a task that was...
0
 
hieloCommented:
You are welcome!
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now