1

The title should be more specific, I know, but the question is too elaborated to even describe in one title itself... I'm having troubles to pass certain amount of values into one array to post via AJAX.

Here's the HTML code with a foreach loop inside the table

<table class="table table-striped table-bordered table-hover">
  <thead>
    <th style="text-align: center;">Select</th>
    <th style="text-align: center;">Name</th>
    <th style="text-align: center;">Situation</th>
    <th style="text-align: center;">Bill</th>
    <th style="text-align: center;">Date Limit</th>
    <th style="text-align: center;">Value of Bill</th>
    <th style="text-align: center;">Own Value</th>
    <th style="text-align: center;">Responsible Name</th>
   </thead>
   <tbody>
   <?php
     foreach ($result as $value) {
        echo '<tr style="text-align: center;">
              <td><input style="cursor: pointer;" type="checkbox" value="'. $value['VALUEBILL'] .'" class="check_list"></input>
              <input style="display: none;" type="checkbox" checked="true" value="'. $value['IDTRANSACTION'] .'" name="numControle"></td>
              <td>'. utf8_encode($value['NAME']) .'</td>
              <td>'. $value['SITUATION'] .'</td>
              <td>'. $value['BILL'] .'</td>
              <td>'. $value['DATELIMIT'] .'</td>
              <td>'. $value['VALUEBILL'] .'</td>
              <td>'. $value['OWNVALUE'] .'</td>
              <td>'. utf8_encode($value['RESPONSABLENAME']) .'</td>
              </tr>';
     }

  ?>
  </tbody>
 </table>

For a better understanding, I have one jQuery script which will sum all bills selected and give the result of all of them together, taking the checked $value['VALUEBILL'] checkboxes.

Here's the jQuery script to get and sum all checked checkboxes values.

$(document).ready(function(){
   update();
});
$('input[type=checkbox]').click(function(){
   update();
})
function update(){
   var amount2 = 0;
   $('.check_list').each(function () {
      if (this.checked) {
        amount2 += Number($(this).val());
          checked_boxes ++;
      }
   $("input[name=amount]").val("$"+amount2.toFixed(2));
});

Here's the form where the script sum all values and input:

<form name="sentMessage" id="purchases" novalidate>
  <h4 style="margin-top: -5px;">Total:</h4>
  <input type="text" class="form-control" id="amount2" name="amount" value="" disabled="">
  <button type="submit">Send Payment</button>
</form>

I pointed the code to a better understanding on what I'm doing and to let you know about what the main problem is

Q: So, what do you need, because is not clear at all?

Take note inside the foreach loop, it has an ocult checkbox with the value of $value['IDTRANSACTION'], this is what I need to send as well with the other checkbox... The first checkbox have the value of the bill, and the second, the ID of transaction, which each one have its unique value, and it's extremely important to account all those bills.

So my question is:

  • How do I check this box as well when the first checkbox is checked? (I tried a few jQuery codes, but they checked ALL of them in a row when I checked the first one, I need to check only the one which is checked, and not the others that foreach provide);

  • After getting the first issue sorted out, I need to populate one array inside the form to send in one AJAX post (which is already ready and working properly) to get all the checkboxes values with the ID's of each one.

That's it... I'm sorry for the long question, but I needed to provide all the info I could so you could understand what I'm trying to do.

Cheers!

1
  • This question is similar to: Script to populate array and send via AJAX. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Dec 15, 2024 at 17:15

0

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.