Jackpot
Jackpot

Reputation: 15

How to execute a calculation php script on html form

I make a PHP script for calculation based on pricelist. My script work powerful, but the problem is: I must enter the value directly in source code. I wouldlike to offer end-user the opportunity to use my script without any code by a simply fill html form.

I have difficulty to link my php script on the form to make this calculation.

Bellow my source code:

  1. FORM

     <center>
         <form action="samepage.php" method="post">
        <input type="text" name="amount" value="0"/>F
        <input name="button" type="submit" id="button" value="CALCULATE"  />         
       </form>
    
        <br>
    
  2. PHP SCRIPT

    Corresponding your Amount, YOU MUST PAY:
    // How to remplace 500 value with form value?

    // THE PRICELIST PHP SCRIPT

    <?php
    if($montant <= 5000){ // If the amount are <= to 500, 200 must add
      echo $montant + 200;
    F </h2>
        </center>
    

    I need your help, Thanks in advance!


I have difficulty to link my php script on the form to make this calculation.

Upvotes: 0

Views: 2463

Answers (3)

Jackpot
Jackpot

Reputation: 15

Hello All and thanks you so much for your useful help!

Now I wouldlike to execute the same script on 2 page.

The first page containt the form and the second the PHP script.

My is/else collection is easy to understand, I continue to use that. What modification must I do to execute the same script on 2 page please?

I think to do this:

FIRST PAGE:

<form action="next-page.php" method="POST">
            <input type="text" name="amount" placeholder="0" />F
            <input name="submit" type="submit" value="CALCULATE"/>
        </form>

SECOND PAGE

    <strong>Corresponding your Amount, YOU MUST PAY: </strong><br><h2>
        <?php 

        if (isset($_POST['submit'])){
            $montant = $_POST['amount'];
            if ($montant <= 5000) {
                echo $montant + 200;
            } else
            if ($montant <= 10000) {
                echo $montant + 400;
            } else
            if ($montant <= 15000) {
                echo $montant + 600;
            } else
            if ($montant <= 25000) {
                echo $montant + 900;
            } else
            if ($montant <= 35000) {
                echo $montant + 1100;
            } else
            if ($montant <= 50000) {
                echo $montant + 1200;
            } else
            if ($montant <= 75000) {
                echo $montant + 1700;
            } else
            if ($montant <= 100000) {
                echo $montant + 2000;
            } else
            if ($montant <= 150000) {
                echo $montant + 2200;
            } else
            if ($montant <= 200000) {
                echo $montant + 2400;
            } else
            if ($montant <= 250000) {
                echo $montant + 2900;
            } else
            if ($montant <= 300000) {
                echo $montant + 3900;
            } else
            if ($montant <= 400000) {
                echo $montant + 4900;
            } else
            if ($montant <= 200000) {
                echo $montant + 2400;
            } else
            if ($montant <= 750000) {
                echo $montant + 7400;
            } else
            if ($montant <= 1000000) {
                echo $montant + 9900;
            } else
            if ($montant <= 2000000) {
                echo $montant + 14900;
            } else
            if ($montant <= 3000000) {
                echo $montant + 19900;
            }else{

            }
        }
?>
        F</h2>
</center>

Upvotes: 1

Jamie M
Jamie M

Reputation: 421

If you're posting back to the same page, you can use the following:

<?php if ($_POST['amount']) {
        $montant = (float) $_POST['amount']; // Number cast for security, use (int) to remove decimals
    } else {
        $montant = 500;
    } // How to remplace 500 value on the form value  ?>

This can also be done in shorthand as follows:

<?php $montant = $_POST['amount'] ? (float) $_POST['amount'] : 500; ?>

I know you didn't ask, but you could also simplify your if/else collection a few ways, such as using an array then looping it:

<?php $vals = array(3000000 => 19900, 2000000 => 14900, 1000000 => 9900); //etc
foreach ($vals as $total => $additional) {
    // Working down from highest to lowest until we match
    if ($montant <= $total) {
        $montant += $additional; 
        break; // Exit when found
    }
} ?>

Upvotes: 0

Ramu Bhusal
Ramu Bhusal

Reputation: 61

if the php code is on the same page then you need to do this.

<center>
        <form action="" method="POST">
            <input type="text" name="amount" placeholder="0" />F
            <input name="submit" type="submit" value="CALCULATE"/>
        </form>
    <br>
    <strong>Corresponding your Amount, YOU MUST PAY: </strong><br><h2>
        <?php 

        if (isset($_POST['submit'])){
            $montant = $_POST['amount'];
            if ($montant <= 5000) {
                echo $montant + 200;
            } else
            if ($montant <= 10000) {
                echo $montant + 400;
            } else
            if ($montant <= 15000) {
                echo $montant + 600;
            } else
            if ($montant <= 25000) {
                echo $montant + 900;
            } else
            if ($montant <= 35000) {
                echo $montant + 1100;
            } else
            if ($montant <= 50000) {
                echo $montant + 1200;
            } else
            if ($montant <= 75000) {
                echo $montant + 1700;
            } else
            if ($montant <= 100000) {
                echo $montant + 2000;
            } else
            if ($montant <= 150000) {
                echo $montant + 2200;
            } else
            if ($montant <= 200000) {
                echo $montant + 2400;
            } else
            if ($montant <= 250000) {
                echo $montant + 2900;
            } else
            if ($montant <= 300000) {
                echo $montant + 3900;
            } else
            if ($montant <= 400000) {
                echo $montant + 4900;
            } else
            if ($montant <= 200000) {
                echo $montant + 2400;
            } else
            if ($montant <= 750000) {
                echo $montant + 7400;
            } else
            if ($montant <= 1000000) {
                echo $montant + 9900;
            } else
            if ($montant <= 2000000) {
                echo $montant + 14900;
            } else
            if ($montant <= 3000000) {
                echo $montant + 19900;
            }else{

            }
        }
?>
        F</h2>
</center>

Upvotes: 0

Related Questions