Link to home
Start Free TrialLog in
Avatar of Refael
RefaelFlag for United States of America

asked on

passing var jquery to php


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

Avatar of Kiran Sonawane
Kiran Sonawane
Flag of India image

Have you tried for ?

$('#total').val("<?=your_php_variable?>");
Sorry I haven't read your question carefully.
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.


Avatar of Refael

ASKER


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

ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Refael

ASKER


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.  
Avatar of Refael

ASKER


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?
 
Avatar of Refael

ASKER


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;
  }
}
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.
Uhps! I missed two posts.

If it works for you then we got it ;-)
Avatar of Refael

ASKER


:-)

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?
You did not use the upper proposed function: total.toFixed(2);

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