Jump to content

Using Php Sent Parameters In Jscript Function


baber_abbasi

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. :)

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.