passing javascript variable value to php without page refres

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lina531
Forum Newbie
Posts: 6
Joined: Sat Nov 26, 2011 12:04 am

passing javascript variable value to php without page refres

Post by lina531 »

<script type="text/javascript">
function CalculateTotal(){
var p=document.percent.govt_share_capital.value;
var k=document.percent.total_paid.value;
var total=(p/k);
var gtotal=total*100;
//var lll=<?php //echo $gtotal; ?>;
//alert(lll);
}
</script>


<form id="percent" name="percent">
<table>
<tr>
<td>Government share capital out of total paid up share capital</td>
<td><input type="text" name="govt_share_capital" id="govt_share_capital" /></td>
<td>Total Paid up share capital</td>
<td><input type="text" name="total_paid" id="total_paid" /></td>
<td>Percentage of Government share capital to total paid up share capital</td>
<td>
<input type="text" name="gtotal" id="gtotal" value="<?php echo $lll; ?>" onclick="javascript:CalculateTotal();"/>
</td>
</tr>
<?php $total = "<script language=javascript>document.write(gtotal);</script>";
echo $total; ?>
</table>
</form>

Hi,

can anyone please help me for solve this query. I want the function should take input from the 2 fields i.e.govt_share_capital &total_paid and show the percentage in the "gtotal" field on click without page refresh that too in the same page without submitting the form.

thnks.... please help me!!!
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: passing javascript variable value to php without page re

Post by Gopesh »

Hi,u need not pass javascript variable to php.
Try this

Code: Select all

var gtotal=total*100;
document.getElementById("gtotal").value=gtotal;
After this cahnge the Html code as

Code: Select all

<input type="text" name="total_paid" id="total_paid" onchange="CalculateTotal()" />

It is not the complete solution,but hope that you got the idea of solving the problem.
lina531
Forum Newbie
Posts: 6
Joined: Sat Nov 26, 2011 12:04 am

Solved: passing javascript variable value to php without pag

Post by lina531 »

thanks

ya this code helps me. it works correctly........................
Last edited by lina531 on Mon Feb 06, 2012 5:13 am, edited 1 time in total.
Gopesh
Forum Contributor
Posts: 143
Joined: Fri Dec 24, 2010 12:48 am
Location: India

Re: passing javascript variable value to php without page re

Post by Gopesh »

ok..If your problem is solved,pls change the heading as solved..Good Luck
Post Reply