Populate multiple fields by checkbox

Javascript coding ..

Moderators: egami, macek, gesf

Post Reply
hjortsberg
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Oct 30, 2017 4:13 pm

Sat Jun 30, 2018 3:25 pm

Hi!

Today I have a page with many forms, each form has some hidden fields, eg. order_id, price, etc.
Today the user has to pay each order one by one.

What I am trying to do is to make a list of checkboxes for the user so that the user only selects the wanted orders to pay, the checkboxes should then populate 2 or more hidden fields in different ways.

If the user checks 3 orders, let´s say orderID 45, 67 and 765 that have prices of 50, 78 and 732 then two hidden fields order_id should be populated with a list of orderID´s commaseparated list, like 45,67,765 and the price hidden field should be populated with the sum of the orders like 860 (the prices added).

How do I do this??

With regards
Hjortsberg

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Sun Jul 01, 2018 1:26 pm

How do I do this??
I'm wondering why you would want to put all the details in hidden fields???

Simply process the details server side, and if you want to have a total for the user to see - use a visible form, otherwise there's no need.

Do not rely on client supplied data as it can be faked.

hjortsberg
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Oct 30, 2017 4:13 pm

Sun Jul 01, 2018 1:30 pm

Maybe I am being unclear on what I want.

Regardless of what I said I want my users to click checkboxes on a page and 2 different things has to be passed through a form, only logic I can come up to is to have two hidden fields.

One field HAS to contain orderID in a string commaseparated.
One field HAS to contain a sum of all orders.

Any help?

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 761
Joined: Mon Feb 22, 2016 5:52 pm

Mon Jul 02, 2018 8:33 am

If you must do it the way you have stated, then you are making hard work for yourself.

What would be wrong with the following?

Code: Select all

<form method="post" id="invoices" action="process_invoices.php">
  <fieldset>
     <legend>Select invoices you want to pay</legend>
     <input type="checkbox" id="iv01" name="iv01" value="288.10">
     <label for="iv01">IV01 288.10</label>
     <input type="checkbox" id="iv02" name="iv02" value="163.45">
     <label for="iv02">IV02 163.45</label>
     <button form="invoices" type="submit" name="submit" value="submit">Submit</button>
  </fieldset>
</form>

hjortsberg
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Oct 30, 2017 4:13 pm

Mon Jul 02, 2018 11:05 am

What I have today is

foreach
<form>
<input hidden name="order_id" value="$somevalue1">
<input hidden name="price" value="$othervalue1">
</form>
<form>
<input hidden name="order_id" value="$somevalue2">
<input hidden name="price" value="$othervalue2">
</form>
<form>
<input hidden name="order_id" value="$somevalue3">
<input hidden name="price" value="$othervalue3">
</form>
endforeach

and the user has to pay each order seperatly.

What I want is like

<form>
foreach
<checkbox value="somevaluetocheck (maybe the order_id?)">
endforeach
</form>

When a checkbox is checked, I want TWO things to happen.
I want the order_ids to be put in a hidden field comma separated.
I want the sum of all checked prices to be put in another hidden field.

The result in the 2 hidden fields should be eg

83647,91645,92745
and
750 (from the three orders with order_ids of 83647,91645,92745, the prices are 200+200+350)

Post Reply