Jump to content


Photo

Using Php Sent Parameters In Jscript Function


  • Please log in to reply
4 replies to this topic

#1 baber_abbasi

baber_abbasi
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationPakistan

Posted 28 June 2006 - 08:14 AM

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.


<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>

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


Baber N Abbasi

#2 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 28 June 2006 - 04:39 PM

You need to include the div names in single quotes
like this

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

NoGray.com


#3 baber_abbasi

baber_abbasi
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationPakistan

Posted 28 June 2006 - 06:46 PM

Thanks.

I just replied but it couldn't work. Can you plz look into this matter?? Thanks again.
Baber N Abbasi

#4 nogray

nogray
  • Members
  • PipPipPip
  • Advanced Member
  • 930 posts
  • LocationSan Francisco CA

Posted 28 June 2006 - 08:14 PM

ok, I see the error. You'll need to use ids instead of names.

change your calc function to
function calc(f, e){    
  one = document.getElementById(f).value;
  two = document.getElementById(e).value; 
  document.getElementById('total1').value = (one * 1) + (two * 1);
}
Notice you'll always use total1 in this function.

and change your HTML to
<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>

NoGray.com


#5 baber_abbasi

baber_abbasi
  • Members
  • PipPipPip
  • Advanced Member
  • 33 posts
  • LocationPakistan

Posted 29 June 2006 - 05:13 PM

Thanks indeed. It worked great. :)
Baber N Abbasi




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users