baber_abbasi Posted June 28, 2006 Share Posted June 28, 2006 Hello,I am not good in Jscript coding while I know PHP very well. Just stuck in a very small issue though i.e "Using PHP sent parameters in jscript function"I am using Jscript to fetch 2 parameter values (these are actually form field name), add them and give their total in another form field. There are 3 fields in the form. [code]<HEAD><script type="text/javascript">function calc(f, e){ one = document.autoSumForm.f.value; two = document.autoSumForm.e.value; document.autoSumForm.total1.value = (one * 1) + (two * 1);}</script></HEAD><BODY><form name="autoSumForm"> <table width=50% bgcolor="#FFFFFF" cellspacing=1 cellpadding=2> <tr bgcolor=""> <td><b>One</b></td> <td><b>Two</b></td> <td><b>Total</b></td> </tr><? //loop through here and post as an arrayfor ($i = 1; $i <= 1; $i++) { //variables for auto calculation$fn = "a".$i;$ef = "b".$i;?> <td><input name="a<?=$i?>" type="text" onFocus="calc(<?=$fn?>, <?=$ef?>);"></td> <td><input name="b<?=$i?>" type="text" onFocus="calc(<?=$fn?>, <?=$ef?>);"></td> <td><input name="total<?=$i?>" type="text"></td> </tr><? //end loop here }?> </table> </td> </tr> </table> </form></BODY></HTML>[/code]The only problem in this code is that function i.e function calc(f, e){}is not fetching parameters values i.e (f , e).Plz tell me how to make it work & thanks in advance. :) Quote Link to comment Share on other sites More sharing options...
nogray Posted June 28, 2006 Share Posted June 28, 2006 You need to include the div names in single quoteslike thisonFocus="calc('<?=$fn?>', '<?=$ef?>');" Quote Link to comment Share on other sites More sharing options...
baber_abbasi Posted June 28, 2006 Author Share Posted June 28, 2006 Thanks.I just replied but it couldn't work. Can you plz look into this matter?? Thanks again. Quote Link to comment Share on other sites More sharing options...
nogray Posted June 28, 2006 Share Posted June 28, 2006 ok, I see the error. You'll need to use ids instead of names.change your calc function to[code]function calc(f, e){ one = document.getElementById(f).value; two = document.getElementById(e).value; document.getElementById('total1').value = (one * 1) + (two * 1);}[/code]Notice you'll always use total1 in this function.and change your HTML to[code]<td><input name="a<?=$i?>" id="a<?=$i?>" type="text" onFocus="calc(<?=$fn?>, <?=$ef?>);"></td> <td><input name="b<?=$i?>" id="b<?=$i?>" type="text" onFocus="calc('<?=$fn?>', '<?=$ef?>');"></td> <td><input name="total<?=$i?>" id="total<?=$i?>" type="text"></td>[/code] Quote Link to comment Share on other sites More sharing options...
baber_abbasi Posted June 29, 2006 Author Share Posted June 29, 2006 Thanks indeed. It worked great. :) 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.