[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

passing var jquery to php

Posted on 2011-10-10
13
Medium Priority
?
295 Views
Last Modified: 2012-05-12

Hello Experts,

I need to use a variable from jquery to php to preform calculation.
Below is a script that basically print a number into a form field.
I need to take this number (the value) and use it to calculate totals (in php).

would be thankful for any help.


$(".checkbox").click(function(){
calculate_total_if_checkboxes_checked();
});
});

            
function calculate_subtotal_if_checkboxes_checked(){
var flag = true;
              
$(".checkbox").each(function(){
if (!$(this).is(':checked')) 
flag = false;
});
              
if (flag) {
var subtotal = 100;
$('#subtotal').val(subtotal);
}
else 
$('#total').val("");
}

Open in new window

0
Comment
Question by:Refael
  • 6
  • 5
  • 2
13 Comments
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36947017
Have you tried for ?

$('#total').val("<?=your_php_variable?>");
0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36947024
Sorry I haven't read your question carefully.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36947120
Hello Refael,

PHP cannot read client computer HTML fields.
PHP has to wait until HTML form data are submitted to the PHP processing server side script page or you can create a smaller PHP source page that can be called by AJAX calls from the client browser page.

But what never can happen is that PHP is reading or getting data direct from the user entering page.

That is because PHP reads server source page text and produces one text stream that is sent to browser. The browser can start to display that text stream after PHP has finished producing the text stream. The browser interprets that text and if it finds JavaScript on it like jQuery then is that all executed on client side. First contact to PHP again is the next form submit or the AJAX call to some PHP page.

My recomendation is to do the totals on browser in JavaScript and to verify it again on PHP side if you do not trust the browser calculation for example because JavaScript can be turned off.


0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Refael
ID: 36947149

Hello Zvonko,

thank you so much for the above.
here is something simple i need to do. it might be that doing it using jquery is not the best solution. if it can be done using php alone i would be happy to know.

in a php form there are two check boxes. if the user check both of the check boxes i need to add to the total price $20. that's all.

so basically the calculation is made which is basically $50 and it prints in the page "total $50".
only when the user check both check boxes i need to do the following:

print it into the "additional fee" field e.g. $20
then calculate the totals = $50 + $20

is it possible to do it before submitting the form?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36947213
Yes, that is possible and can be done easilly.


<html>
<head>
<title>Zvonko &#42;</title>
<script>
function reCalc(theForm){
  var total = 0;
  if(theForm.basicFee.checked) total=20;
  if(theForm.basicFee.checked && 
     theForm.additionalFee.checked) total=70;
  theForm.grandTotal.value = total.toFixed(2);
}
</script>
</head>
<body>
<form action="your.php" method="PUT" onClick="reCalc(this)" >
<input type="checkbox" name="basicFee" > Basic service Feed <br>
<input type="checkbox" name="additionalFee" > Additional service Feed <br>
<input type="text" name="grandTotal" size="5" readonly> 
<input type="submit" >
</form>
</body>
</html>

Open in new window

0
 
LVL 63

Accepted Solution

by:
Zvonko earned 2000 total points
ID: 36947231
If you have more then one price component then you can calulate totals like this:
<html>
<head>
<title>Zvonko &#42;</title>
<script>
function reCalc(theForm){
  var total = 0;
  if(theForm.basicFee1.checked) total+=50;
  if(theForm.basicFee2.checked) total+=60;
  if(theForm.basicFee3.checked) total+=70;
  if(total > 0 && theForm.additionalFee.checked) total+=20;
  theForm.grandTotal.value = total.toFixed(2);
}
</script>
</head>
<body>
<form action="your.php" method="PUT" onClick="reCalc(this)" >
<input type="checkbox" name="basicFee1" > Basic service Fee 1 <br>
<input type="checkbox" name="basicFee2" > Basic service Fee 2 <br>
<input type="checkbox" name="basicFee3" > Basic service Fee 3 <br>
<input type="checkbox" name="additionalFee" > Additional service Fee <br>
<input type="text" name="grandTotal" size="5" readonly> 
<input type="submit" >
</form>
</body>

Open in new window

0
 

Author Comment

by:Refael
ID: 36947267

Zvonko, first, thank you!

the task is not that complicated and i am trying to follow your posting above to understand... i will explain...

a user fill in form. at the bottom of the form there are 3 fields as follow:

price -> this is fixed (no function needed)
additional fee -> only when the user check both of the check-boxes it should print "20".
total -> id the user did not check both of the check-boxes the total is obviously the fixed price alone.
if he did check them both then the price should be calculated 20+50.

that's all.  
0
 

Author Comment

by:Refael
ID: 36947357

that's works now:

function reCalc(theForm) {
  var total = 0;
  if(theForm.basicFee.checked && theForm.additionalFee.checked) {
   subtotal=20;
  theForm.subTotal.value = 20;
  theForm.grandTotal.value = 70;
  }
}

great! if the user unchecked the boxes is it possible to auto-recalculate or provide a button to re-calculate?
 
0
 

Author Comment

by:Refael
ID: 36947455

Zvonko you are A star!

got it to work!!

function reCalc(theForm) {
  var total = 0;
  if(theForm.basicFee.checked && theForm.additionalFee.checked) {
   subtotal=20;
  theForm.subTotal.value = 20;
  theForm.grandTotal.value = 70;
  }
  else {
         subtotal=0;
         theForm.subTotal.value = 0;
       theForm.grandTotal.value = 50;
  }
}
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36947467
That was my version one.
Here again the version one with the new total method:
<html>
<head>
<title>Zvonko &#42;</title>
<script>
function reCalc(theForm){
  var total = 0;
  if(theForm.basicPrice.checked) total+=50;
  if(total > 0 && theForm.additionalFee.checked) total+=20;
  theForm.grandTotal.value = total.toFixed(2);
}
</script>
</head>
<body>
<form action="your.php" method="PUT" onClick="reCalc(this)" >
<input type="checkbox" name="basicPrice" > Basic price $50.00 <br>
<input type="checkbox" name="additionalFee" > Additional service Fee $20.00<br>
<input type="text" name="grandTotal" size="5" readonly> 
<input type="submit" >
</form>
</body>
</html>

Open in new window

If my html confuses you then post here your HTML page source as seen in browser when you look at page source and I can adapt to your field names.
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36947472
Uhps! I missed two posts.

If it works for you then we got it ;-)
0
 

Author Comment

by:Refael
ID: 36947486

:-)

Zvonko can you just tell me one more small thing...

the initial price is 59.99 and i add 20
the total prints "79.99000000000001"
is there a way to round the number to  79.99?
0
 
LVL 63

Expert Comment

by:Zvonko
ID: 36947572
You did not use the upper proposed function: total.toFixed(2);

The Number.toFixed(numOfTrailingDigits); does the conversion for you.

0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question