/    Sign up×
Community /Pin to ProfileBookmark

passing javascript variable value into php without page refresh

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

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@eval_BadCode_Feb 04.2012 — I won't consider looking at your example because you didn't put [ PHP ] forum tags around your code (very annoying). But I do know what you are asking and I think it would make more sense to show you a simple example. The "buzz" word you're looking for is AJAX. Infact, I like ajax so much that I use jquery (this is a MUST have javascript library; its AWESOME don't pass it up).

page.html
[code=html]
<html><head><title>Ajax Example</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function callAjaxAddition() {
arguments0 = {
arg1: $("#exampleForm input[name='arg1']").val(),
arg2: $("#exampleForm input[name='arg2']").val()
};
$.ajax({
type: "POST",
url: "processAjax.php",
data: {arguments: arguments0},
success: function(data) {
$("#answer").html('<ul><li>'+data+'</li></ul>');

}
});
return false;
}
</script>
</head>
<body><div id="exampleForm">
<input name="arg1" /> + <input name="arg2"> = <div id="answer"></div>
<br />
<button onClick="callAjaxAddition()">Click Me to Add</button> <!-- maybe it should be input type=button ?!? -->
</div></body></html>
[/code]


processAjax.php
[code=php]
<?php

$a=0;
foreach($_POST['arguments'] as $v) $a+= (int) $v;
echo $a;

[/code]


Since ajax can get dirty really fast, this example also shows you how to pass an array of values over POST or GET. Don't be fooled into parsing a bunch of chunked strings or json when it is so easy to use an array.

I tested this code. It works and looks like this:

http://img140.imageshack.us/img140/2867/screenshot3id.png

Edit: I removed a trailing comma from the array. You can not have a trailing comma in javascript lists or objects, it will cause internet explorer to mess up.


Cheers :p
Copy linkTweet thisAlerts:
@mlorcamFeb 10.2012 — Thanks a lot for this simple but powerful solution. I suffered with many other "solutions" I have implemented without feeling successful. This one was a very clever piece of sw.

?
×

Success!

Help @lee_help spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...