1

I am trying to return the values of all selected checkboxes through ajax. I cant work out how to pass the values through as a parameter.

HTML CHECKBOXES

<li><input class="customer_sales_value" id="customer_sales_1" type="checkbox" value="1"><label for="customer_sales_1">James</label></li>
<li><input class="customer_sales_value" id="customer_sales_2" type="checkbox" value="2"><label for="customer_sales_2">Jack</label></li>
<li><input class="customer_sales_value" id="customer_sales_3" type="checkbox" value="3"><label for="customer_sales_3">John</label></li>
<button id="customer_pop_share_btn">submit</button>

JQUERY

$('#customer_pop_share_btn').click(function(){
    sales_list = each($('.customer_sales_value').val());
    $.ajax({
        type    : 'POST',
        url     : '//'+base_url+'/ajax2/timeline-share.php',
        data    : 'sales_list='+sales_list,
        success : function(data) {
     });    
 });

My jquery is the issue, I think the way I want to do this is pass a list of the checkbox values into a variable like sales_list = 1,2,3 How do I do that?

2
  • You mean "send", not "return". Return is what the server sends back to the client. Commented Mar 1, 2019 at 0:42
  • yes sorry, send as a parameter. Commented Mar 1, 2019 at 0:52

2 Answers 2

2

first set name attribute to each checkbox, then push the value of checked checkbox hrough loop in javascript.

<li>
    <input class="customer_sales_value" id="customer_sales_1" name="c_sales_value" type="checkbox" value="1"><label for="customer_sales_1">James</label>
</li>
<li><input class="customer_sales_value" id="customer_sales_2" name="c_sales_value" type="checkbox" value="2"><label for="customer_sales_2">Jack</label></li>
<li><input class="customer_sales_value" id="customer_sales_3" name="c_sales_value" type="checkbox" value="3"><label for="customer_sales_3">John</label></li>

$('#customer_pop_share_btn').click(function() {
  var selectedValues    = [];
   $.each($("input:checkbox[name=c_sales_value]:checked"), function(){
        selectedValues.push($(this).val());

   });
  $.ajax({
      type: 'POST',
      url: '//' + base_url + '/ajax2/timeline-share.php',
      data: {
        sales_list: selectedValues
      },
      success: function(data) {});
  });
});
Sign up to request clarification or add additional context in comments.

Comments

1

Make an array of the values of the checked boxes using .map().

In the PHP script, $_POST['sales_list'] will be an array of the values if any of the boxes are checked; if none of the boxes are checked, the variable will not be set at all.

$('#customer_pop_share_btn').click(function() {
  var sales_list = $('.customer_sales_value:checked').map((i, el) => el.value).get();
  $.ajax({
      type: 'POST',
      url: '//' + base_url + '/ajax2/timeline-share.php',
      data: {
        sales_list: sales_list
      },
      success: function(data) {});
  });
});

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.