1

I have a checkbox as follows,

<tr>
  <td>
     <input type="checkbox" checked="checked" name="feegroupid[]" class="fee_checkbox" value="<?php echo $fee_value->fee_groups_feetype_id ?>">
  </td>

   <td> 
      <input type="text" name="actual_pay[]" class="form-control modal_amount actual_pay" id="amount" value="<?php echo $feetype_balance; ?>">
   </td>
   </tr>

And my PHP function if the checkbox row is selected,

  foreach($_POST['feegroupid'] as $index => $selected) {

     echo $index; 

     $actualPayment = $_POST['actual_pay'][$index];      

    }

I have two rows with checkbox, Here the echo $index is displaying only the first row index 0 if i select the second row (whose index is 1). But both is selected (ie.checked), it is showing both index as 0,1 respectively.. But when we uncheck one checkbox(say first row with index 0 is unchecked), it is showing the 0 only but checked index is 1.

I am in the need to get the value of another input box with name actual_pay[] based on the checked box index..

How to get the respective checked box index instead of the first row index while changing checkbox?

Attached image has a button click to pay which should send the checked box index (ie, second box is checked and its index is 1).

enter image description here

11
  • 2
    Checkboxes are only sent to the PHP from the browser IF they are Checked Commented Mar 7, 2018 at 0:34
  • How can i get the index of that checked box? Because i need to get a value of another class based on the checked box index. Commented Mar 7, 2018 at 0:37
  • 1
    Look at the value and not the index Commented Mar 7, 2018 at 0:37
  • use $selected not the $index Commented Mar 7, 2018 at 0:53
  • It is showing error undefined offset.. Commented Mar 7, 2018 at 0:53

1 Answer 1

1

Something like this should work:

<tr>
    <td>
        <input type="checkbox" checked="checked" name="feegroupid[]" class="fee_checkbox" value="<?php echo $fee_value->fee_groups_feetype_id; ?>">
    </td>
    <td> 
        <input type="text" name="ap_<?php echo $fee_value->fee_groups_feetype_id; ?>" class="form-control modal_amount actual_pay" id="amount" value="<?php echo $feetype_balance; ?>">
    </td>
</tr>

PHP:

if (isset($_POST['feegroupid'])) {
    foreach ($_POST['feegroupid'] as $k => $v) {
        if (isset($_POST['ap_' . $v])) {
            echo $_POST['ap_' . $v]; //$actualPayment
        } else {
            echo 'Not found for item: ap_' . $v;
        }
    }
}
Sign up to request clarification or add additional context in comments.

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.