0

I have a calculation that makes a total based on an exchange rate, sometimes I want this rate to change based on a checkbox.

So my calculation is

<?php
if (!empty($sum)) {
echo ceil($sum / $line['rate'] * 10) / 10.0;
$sum1 = ceil($sum / $line['rate'] * 10) / 10.0;
}
?>

then if my checkbox is ticked I want it to change to

<?php
if (!empty($sum)) {
echo ceil($sum / ($line['ask_rate']+.02) * 10) / 10.0;
$sum1 = ceil($sum / ($line['ask_rate']+.02) * 10) / 10.0;
}
?>

This is the checkbox and update button, ideally when the update button is pressed it sees if the checkbox is ticked and changes the calculation if it is.

<tr>
    <td>Special Rate?</td>
    <td><input type="checkbox" name="special" value="1"></td>
    <td>Press Update</td>
    <td><INPUT TYPE="SUBMIT" VALUE="update" NAME="B1"></form></td>
<tr>    

The form submits to itself and totals up. But I'm having trouble working out how to make it happen.

Kilisi
  • 402
  • 11
  • 33
  • To make this work, you have to enclose the `` elements into a `
    ` element. Then, depending on the form action and method, we can help you with your issue.
    – CodiMech25 Feb 03 '19 at 13:10
  • it's in a form, the form submits to itself – Kilisi Feb 03 '19 at 13:11
  • Possible duplicate of [How to read if a checkbox is checked in PHP?](https://stackoverflow.com/questions/4554758/how-to-read-if-a-checkbox-is-checked-in-php) – CodiMech25 Feb 03 '19 at 13:15

2 Answers2

2

You can try this:

<?php

if (!empty($sum)) {
    $rate = $line['rate'] * 10;
    if(isset($_REQUEST['special']))
    {
        $rate = ($line['ask_rate'] + .02) * 10
    }

    $sum1 = ceil($sum / $rate) / 10.0;
    echo $sum1;
}

Depending on the method of your <form> (GET or POST), you can replace $_REQUEST with $_GET or $_POST

thomas.drbg
  • 1,068
  • 10
  • 9
  • It's POST, I'll look up what REQUEST difference is from POST, it's working with REQUEST though – Kilisi Feb 03 '19 at 13:29
1

You can query for isset($_REQUEST["special"]) to differenciate the to cases:

<?php
    if (isset($_REQUEST["special"]) && !empty($sum)) {
        echo ceil($sum / ($line['ask_rate']+.02) * 10) / 10.0;
        $sum1 = ceil($sum / ($line['ask_rate']+.02) * 10) / 10.0;
    } else if (!empty($sum)) {
        echo ceil($sum / $line['rate'] * 10) / 10.0;
        $sum1 = ceil($sum / $line['rate'] * 10) / 10.0;
    }
?>
Beppo
  • 176
  • 7