-2

I have a 4 input boxes, 3 are hidden fields. The first input box take a figure, while the second displays 20% of the figure and the 3rd displays the answer gotten from subtracting 20% of the initial figure from the figure. here is a typical example of what i mean.

textbox A = 100
20% of 100 = 20.
textbox B = 20.
100 - 20 = 80.
textbox C = 80

i have been able to do all this and passed the variables to the input box, but when i load the page it populates the textbox with undefined variable and when i click on the button it execute the script and the initial text that says undefined variable is inserted into my db. how do i post the answer into my db instead of post what loads with the page. here is my code.

When it loads up:

enter image description here

<?php
if (isset($_POST['amount'])&&  ($_POST['iniamount']) && 
($_POST['outamount'])) {

    $amount = $_POST['amount'];
    $iamount = 0.2;

    $inimain = $iamount * $amount;
    $outamount = $amount - $inimain;    

}
$date = date("Y-m-d H:i:s");
?>

here is the form

<div class="form">
    <form method="POST" enctype="multipart/form-data" name="donation" class="" >
        <div class="form-group ">
            <label for="cname" class="control-label col-lg-2">Donation Amount <span class="required">*</span></label>
            <div class="col-lg-10">
                <input class="form-control" id="amount" name="amount"  type="text" required />
                <input name="email" type="hidden" class="form-control" id="email"  required />
                <input name="iniamount" type="text" class="form-control" id="iniamount" value="<?php echo $inimain; ?>" required />                             
                <input name="outamount" type="text" class="form-control" id="outamount" value="<?php echo $outamount;  ?>" required />
                <input name="date" type="hidden" class="form-control" id="outamount" value="<?php echo $date;  ?>"  required />
            </div>
Brijal Savaliya
  • 1,101
  • 9
  • 19
eugbana
  • 37
  • 8
  • If your if condition is not true, are you defining $inimain and $outamount anywhere else? No? Then why do you wonder that this happens …? – CBroe Aug 23 '17 at 10:15

2 Answers2

1

Initialize the variable first

While The accepted answer will work, I think initializing the variable before hand will serve you better in the long run.

<?php
    $inimain = '';
    $outamount = '';
    // repeat for any other variable you might need in your form

    if (isset($_POST['amount'])&&  ($_POST['iniamount']) && ($_POST['outamount'])) {
        $amount = $_POST['amount'];
        $iamount = 0.2;
        $inimain = $iamount * $amount;
        $outamount = $amount - $inimain;    
    }
    $date = date("Y-m-d H:i:s");
?>

This will allow you to keep your HTML form simple and easy to read and will allow you more flexibility in the future, for example if you want to set a default value for certain input fields.

Steve Okay
  • 80
  • 12
William Perron
  • 1,288
  • 13
  • 18
1
<?php
    $amount = 0.0;
    $iamount = 0.0;
    $inimain = 0.0;
    $outamount = 0.0; 


    if (isset($_POST['amount'])&&  ($_POST['iniamount']) && ($_POST['outamount'])) {
        $amount = $_POST['amount'];
        $iamount = 0.2;
        $inimain = $iamount * $amount;
        $outamount = $amount - $inimain;    
    }
    $date = date("Y-m-d H:i:s");
?>

The problem is from the variables. Initiate the variables. Actually not like this because it is for calculation. but if you do like this and when the textboxs are empty it will cause undefine error.The system will not knw how to deal with it.

 $inimain = '';
 $outamount = '';

This is the correct way.

    $amount = 0.0;
    $iamount = 0.0;
    $inimain = 0.0;
    $outamount = 0.0; 



<input name="iniamount" type="text" class="form-control" id="iniamount" value="<?php if(isset($_POST['iniamount'])){echo $inimain;} ?>" required />                             
 <input name="outamount" type="text" class="form-control" id="outamount" value="<?php if(isset($_POST['$outamount'])){echo $outamount;}  ?>"  required />
 <input name="date" type="hidden" class="form-control" id="outamount" value="<?php if(isset($_POST['$date'])){echo $date;} ?>" required />
  • 1
    the calculations take place based on the `$_POST` variable and not user-defined variables, so the only time `$amount`, `$iamount`, `$inimain` and `$outamount` _need_ to be floats is already taken care of by OP's if statement. After that the variables are only use for display (unless OP has some other code he didn't show) so initializing to empty strings will work just fine and will not require another `if` in the form. In your answer you will _still_ need some if statement in the form so as to not output `0.0` in the input fields. – William Perron Aug 23 '17 at 17:13
  • @WilliamPerron. yeah your suggestion is true. But there is alot of work on the code assuming the user enter characters or a character. – Dawud Abdul Manan Aug 23 '17 at 17:25
  • true, however this goes in the realm of input validation, which is out of scope of this question. – William Perron Aug 23 '17 at 18:01
  • @WilliamPerron okay. i see – Dawud Abdul Manan Aug 23 '17 at 18:10
  • i guess they are all correct, thanks guys – eugbana Aug 24 '17 at 23:21