/    Sign up×
Community /Pin to ProfileBookmark

Automatically Calculate Total

Greetings All,
I have made a script that has 4 fields.

Quantity, Item Description, Item Unit Price and Total.

The first field, quantity is a form field which a user enters the amount into. The description and unit price fields are data pulled from a database.

The total field is a field which automatically calculates the Quantity Field multiply by the Unit Price.
My problem is getting the “total” field to automatically generate a value once a customer enters a numeric value into the quantity field.

Can anyone point me in the right direciton?
I figure the solution would be one of dhtml or ajax since the total field would update on the fly once quantity changes.
Any help would be appreciated.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@manyamileJul 15.2007 — Ok while I am waiting for my own answer I'll answer yours. I am not an expert and while I have no doubt as to the way I'd do it the way I explain it may not be the best.

  • 1. I would use ajax (like you said) to bring the description and unit price to javascript. The only thing I would do in php would be the mysql queries (unless you want to build a table with a variable number of rows, when using php is easier, otherwise you need the dom).

  • 2. In html you have your <form>...</form> tags that have your input fields in between. I would also put a submit button in there, (otherwise how would the program know when the user is done writing?)

    <input type="submit" value="Submit" onclick="calculate()"/><br />

    Now calculate() is a function that will be called when you click submit.

    If the field quantity is of the form:<input type="text" id="quantity" />

    then you can access its contents in javascript with: qty=document.getElementById("quantity").style.value

    which must be placed inside the calculate function - that is using dhtml.


  • But you are in the php forum. If you want everything in php you can't use dhtml or ajax ( it has javascript objects and you use it if you want to send information without reloading the page.)

    Maybe this mixture of javasdript in a php forum is why people are not answering. I wish I knew why they aren't answering mine.

    To do it with php alone:

    You need an html file:

    <html><body>

    <form action="process.php" method="post">

    Quantity: <input name="quantity" type="text" />

    <input type="submit" />

    </form>

    </body></html>


    Call this second file "process.php"

    <html><body>

    <?php

    $quantity = $_POST['quantity'];

    $price=$quantity* $unit;

    echo "Total price is ". $price ;

    ?>

    </body></html>

    You can read more at: tizag.com

    Actually I don't know if you can write to an input field using php alone.

    Good luck on your project!
    Copy linkTweet thisAlerts:
    @gc40authorJul 15.2007 — I found a script that does what something similiar with the calculations that I want, however, their are two include files that are encrypted.

    Here is the script:
    [code=php]<?php

    include('logic/edit_invoice.php');
    include('include/editinvjs.php');

    ?>
    <form method="post" name="fmaddinvoice" id="fmaddinvoice" action="<?php echo $_SERVER['PHP_SELF']; ?>" onsubmit="return finalCheck();">
    <br>
    <table border="0" align="center">
    <tr><td><b><?php echo INV_DATE; ?></b></td><td><input type="text" size="10" name="invdate" value="<?php echo date('Y-m-d'); ?>"> </td></tr>
    <tr><td><b><?php echo ORDER_ID; ?></b></td><td><input type="text" size="20" name="order_id" maxlength="32" value="<?php echo $order_id; ?>"></td></tr>


    <tr><td>
    <b><?php echo DETAILS; ?></b>
    </td><td>
    <!--textarea name="details" cols="50" rows="12"></textarea-->

    <table align="center" border="0">
    <tr><td align="center"><b>No.</b></td><td align="center"><b><?php echo DESC; ?></b></td><td align="center"><b><?php echo QTY; ?></b></td><td align="center"><b><?php echo $moneyunit; ?>/<?php echo UNIT; ?></b></td><td align="center"><b><?php echo TAX; ?></b></td><td align="center"><b><?php echo TOTAL; ?></b></td></tr>
    <?php for($i=0; $i< $rowed1; $i++){ ?>
    <tr>
    <td><input type=text size=2 name="r<?php echo $i; ?>" value="<?php echo ($i+1); ?>" maxlength="5">:</td>
    <td><input type=text size=20 name="d<?php echo $i; ?>" value="<?php echo $prod_list[$i]['product_name']; ?>"></td>
    <td><input type=text size=2 name="n<?php echo $i; ?>" value="<?php echo $prod_list[$i]['qty']; ?>" onchange="checkNumber(this);subSum(<?php echo $i; ?>);"></td>
    <td><input type=text size=6 name="p<?php echo $i; ?>" value="<?php echo $prod_list[$i]['price']; ?>" onchange="checkNumber(this);subSum(<?php echo $i; ?>);"></td>
    <?php
    if(isset($prod_list[$i]['tax'])){
    $temtax = $prod_list[$i]['tax'];
    }else{
    $temtax = $taxrate;
    }
    ?>
    <td><input type=text size=5 name="ta<?php echo $i; ?>" value="<?php echo $temtax; ?>" onchange="checkNumber(this);remind(this.value);subSum(<?php echo $i; ?>);"></td>
    <?php
    if(isset($prod_list)){
    $total_value = $prod_list[$i]['qty']*$prod_list[$i]['price']*(1+$temtax);
    $total_value = num_filter($total_value);
    }
    ?>
    <td><input type=text size=7 name="to<?php echo $i; ?>" value="<?php echo $total_value; ?>" onchange="checkNumber(this);subSum(<?php echo $i; ?>);redoTotal();"></td>
    </tr>

    <?php } ?>
    </table>
    <div id="item2" style="display:none">
    <table align="center">
    <?php for($i=$rowed1; $i<$rowed2; $i++){ ?>
    <tr>
    <td><input type=text size=2 name="r<?php echo $i; ?>" value="<?php echo ($i+1); ?>" maxlength="5">: </td>
    <td><input type=text size=20 name="d<?php echo $i; ?>"></td>
    <td><input type=text size=2 name="n<?php echo $i; ?>" onchange="checkNumber(this);subSum(<?php echo $i; ?>);"></td>
    <td><input type=text size=6 name="p<?php echo $i; ?>" onchange="checkNumber(this);subSum(<?php echo $i; ?>);"></td>
    <td><input type=text size=5 name="ta<?php echo $i; ?>" value="<?php echo $taxrate; ?>" onchange="checkNumber(this);remind(this.value);subSum(<?php echo $i; ?>);"></td>
    <td><input type=text size=7 name="to<?php echo $i; ?>" onchange="checkNumber(this);subSum(<?php echo $i; ?>);redoTotal();">
    </td>
    </tr>

    <?php } ?>
    </table>
    </div>
    </td></tr>
    <tr><td>
    <b><?php echo NET_TOTAL; ?></b>
    </td><td><?php echo $moneyunit; ?>
    <input type="text" name="totbt" size="8" value="0">
    </td></tr>
    <tr><td>
    <b><?php echo TAX; ?>:</b>
    </td><td><?php echo $moneyunit; ?>
    <input type="text" name="tottax" size="7" value="0">
    </td></tr>
    <tr><td>
    <b><?php echo GREAT_TOTAL; ?></b>
    </td><td><?php echo $moneyunit; ?>
    <input type="Text" name="total" size="8" value="0">
    </td></tr>
    <tr><td colspan=2 align="center">
    <input type="submit" name="submit" value="<?php echo ADD_INV; ?>"><br><br>
    </form>
    </td></tr>

    </table>
    <script language="javascript">
    redoTotal();
    </script>
    <?php
    ?>
    [/code]
    Copy linkTweet thisAlerts:
    @gc40authorJul 16.2007 — Help please?
    ×

    Success!

    Help @gc40 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.5,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

    tipper: @Samric24,
    tipped: article
    amount: 1000 SATS,
    )...