Abu Isaac
Abu Isaac

Reputation: 75

Dealing with multiple checkbox in a form with PHP?

I have a form with multiple checkboxes representing the cost; the value of those comes from the database.

<input type="checkbox" name="check" value="12.00" />
<input type="checkbox" name="check" value="40.00" />

On submitting the form, I use javascript to add the checked values together and post that new value in a hidden field.

<input type="hidden" name="total_cost" value="" />

I need to pass the row id from the database with the cost value for each checkbox. that way, when I check one or more checkboxes and submit the payment I need to change the status to PAID from NOT PAID.

Note: It is not possible for me to change the status while checking the checkbox. I can only change the status only after all the the transaction is completed.

enter image description here

Upvotes: 2

Views: 373

Answers (2)

Abu Isaac
Abu Isaac

Reputation: 75

The issue i faced here is to get the row id from the db for respective Cost in the server side. Finally I got a solution from @rene by setting the input value as the ID of the row from the database. Hope it works.

Solution I got: <input type="checkbox" name="check[]" value="{row id from the database}" />

So I will get the Cost for each checked row at the server side by using the ID (value of input). Also I can update the status of each COST by having the respective row IDs after getting the positive response from the payment gateway and not if the response is negative.

Thank you all for the suggestions.

Upvotes: 1

SaidbakR
SaidbakR

Reputation: 13534

First of all naming your multiple form's element in this way will not send multiple data. You have to name your elements like the following:

<input type="checkbox" name="check[]" value="12.00" />
<input type="checkbox" name="check[]" value="40.00" />

Note the square brackets [] in the name attribute of the element.

Upvotes: 3

Related Questions