Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP multiply Two fields

Tags:

php

I am trying to multiply to fields together to obtain a total in PHP form.

   <label for="190_mnth2"></label>
          <div align="center">
            <input name="190_mnth" type="text" id="190_mnth2" value="10" size="5" />
          </div></td>
        <td><div align="center">
          <label for="190_rate"></label>
          <input name="190_rate" type="text" id="190_rate" value="190.00" size="10" />
        </div></td>
        <td><div align="center">
          <input name="total_190" type="text" id="total_190" value=<? echo '190_mnth2' * '190_rate' ?> size="10" />

The above is my current code but the answer is totally wrong it gives me 36100 What is wrong with my formula if anyone can assist?

like image 466
Maggie Ackermann Avatar asked Dec 15 '22 07:12

Maggie Ackermann


1 Answers

First of all you cannot calculate the total like that, it's not Javascript, you need a form with a get/post request which will send a request to the server, server will process and throw the calculated value back to the user.. so wrap the fields around forms, set your method to post(preferred) and than you can write your PHP code like

<?php
   if(isset($_POST['submit_button_name'])) { //Use $_GET if it's a GET request
       //Save the values in variable
       $mnth_190 = $_POST['190_mnth']; 
       $rate_190 = $_POST['190_rate'];

       //Calculate here
       $total = $mnth_190 * $rate_190;

       /* Now you can use $total either to echo straight in your page, 
      or inside another input field */
   }
?>

Also make sure you validate the data before the form is posted and is calculated, check whether the user input doesn't have string or any other special character.

like image 186
Mr. Alien Avatar answered Dec 17 '22 21:12

Mr. Alien