简体   繁体   中英

Passing the sum value from a textarea to an input field

How are you?

Here's what am trying to do:

I want to add every numbers typed by the user inside the textarea then, using javascript an <input> field will automatically sum it all up and displays the total. So once I click the Make Voucher submit button inside the save.php the sum total computed inside that <input> field will be enclosed inside a variable that will be inserted on the database.

现金券

In the image above you could see an Amount column which has a textarea below to which the user could put numbers to be added with. [See Figure 1] Then, Whatever the numbers a user has typed with it will be automatically computed and will be shown in Total Amount Due [See Figure 2]

As you can see everything is just working fine. In Figure 2, I used a <div> tag that will hold the total (or sum) of the value inserted by the user in the textare at the Figure 1. A button named Make Voucher does the trick by triggering the javascript to add the inserted values and retrieves the total and pass it on the <div> tag beside the Total Amount Due title.

See JSfiddle Please note that this code are perfectly working with my browser.

This is my way of getting the total (or sum) of the value inserted in the textarea so by the time the form calls the action in save.php I can pass the total 1170 to the database by the Insert statement through this.

$total = $_POST['totalAmount'];

But I found it hard doing so. Why? Because my concern is this:

1. I could not pass the sum 1170 to $total simple because it is not in the input field as I have said the javascript only shows the added (or sum) by using <div> tag. 2. You should have to click the Make Voucher button first before you could get the sum total.

So to be able to add this onto my INSERT statement the total value retrieved in <div id="res"></div> format must be placed inside the <input> field instead. I have tried doing this: <input type="text" name="total" onKeyUp="calc()"> But still I can't post the value 1170 to $total in save.php.

My concern is this:

Is there any way I can put the sum total of the textarea values inside an input field instead of using <div> tag? So I can be able to save the total in the database? Or my code is simply not organized?

Here is JSFIDDLE

First you have to change onDomReady to No wrap-in <body> because onDomReady in these your js script are not working.

NOTE for you : If you got preferable answer than please accept so other person can't put answer here.

It is very simple, just have a hidden input field, suppose <input type="hidden" name="hdnTotalAmout" /> and pass the total amout value to hidden field along with DIV tag. Then DIV will just show the total amount and use hidden field value to save in database at server-side. Like,

$total = $_POST['hdnTotalAmount']

Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM