Jump to content

Archived

This topic is now archived and is closed to further replies.

baber_abbasi

Using Php Sent Parameters In Jscript Function

Recommended Posts

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 array
for ($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. :)

Share this post


Link to post
Share on other sites
You need to include the div names in single quotes
like this

onFocus="calc('<?=$fn?>', '<?=$ef?>');"

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites

×

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.