Ruud
Ruud

Reputation: 249

math php from javascript calculation

Hello I would like to send an email with php code. I have the following script calculation that works:

<script type="text/javascript"> 
window.onload = function () {
    var total_of_order= <?php echo $order_total ?>;
    var extra_costs= 38;
    var total = Math.ceil(parseInt(total_of_order) - extra_costs);
    $('calculateTotal').innerHTML = total;
    } 
 </script>

<div id="calculateTotal"></div>

How can I code this to pure php? Like?

<?php echo $extra_costs; $extra_costs= "38";?>
<?php echo $order_total ?>
<?php echo $total = Math.ceil(parseInt(order_total) - extra_costs);?>

As you can see I''m the worst coder, could someone help me?

dfasf

Upvotes: 0

Views: 534

Answers (4)

scartag
scartag

Reputation: 17680

<?php  

$extra_costs= 38;

$total = ceil($order_total) - $extra_costs;

?>

<div id="calculateTotal"><?php echo $total ?></div>

Upvotes: 0

Matt K
Matt K

Reputation: 6709

<?php

$extra_costs = 38;
$total = ceil($order_total - $extra_costs);

?>

<script type="text/javascript">
    $('calculateTotal').innerHTML = '<?php=$total?>';
</script>

Upvotes: 0

Rob W
Rob W

Reputation: 348972

Use the ceil function, without Math. prefix. Also, since you're doing your calculations in PHP, you can directly output the result in the <div>, without any JavaScript.

<div id="calculateTotal">
<?php
    $order_total = ...; //Defined previously, not included in your question
    $extra_costs = 38;
    echo ceil($order_total -  $extra_costs);
?>
</div>

Upvotes: 1

Jeff Wilbert
Jeff Wilbert

Reputation: 4520

Just remove the Math.

<?php echo $extra_costs; $extra_costs= "38";?>
<?php echo $order_total ?>
<?php echo ceil(order_total - extra_costs);?>

Upvotes: 0

Related Questions