2

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

8
  • 1
    You say that you use JS. Put on each row (tr as example) an attr with the id, then read it before submitting the data and aggregate them with Js. Commented Nov 26, 2014 at 14:37
  • 2
    Why calculate the total cost in js unless you use it for display? You are just going to have to recalculate on the server side anyway to validate that the sum of items is correct. You should never trust client side input. Commented Nov 26, 2014 at 14:38
  • 1
    Don't do this in client-side code. What's to stop someone from fiddling with the form values and changing their invoice totals to $0, or even "$-500" and giving themselves a nice "refund"? Commented Nov 26, 2014 at 14:38
  • if(!isset($_POST['var'])){ $status = "PAID"; }else{ $status = "NOT PAID"; } Commented Nov 26, 2014 at 14:48
  • Thankyou @MikeBrant and MarcB, I didn't expect that sort of loophole. Commented Nov 26, 2014 at 14:50

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

8 Comments

I don't know why another person deleted their answer, it was the same thing as this.
@sємsєм that I have made. So now can you please suggest to send the row id of that checked amount to the server. By row Id meant the id in the database for the respective costs. Thank you
@AbuIsaac Change the monetary values to the ID's you need and check will be an array with all ID's that are checked by the user.
@René thankyou very much. That is how I wanted. Thanks again. I am not sure whether you have suggested in this way. Anyway I am going to do this as follows: <input type="checkbox" name="check[]" value="{row id from db}">$12.00</input>
@sємsєм That's not what I meant with not being able to hold a textnode. That's just a string value. What's not possible is <input>TEXTNODE</input>. That's more limited to <textarea> and a whole lot of other elements.
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.