0

I want to get the value of the id in the checkbox. but the variable in id always give the 1st value. how can send php array to jquery array. thanks

require "connection.php";
$sql = mysqli_query($con,"SELECT * FROM position ORDER BY position_align + 0 ASC");
$switch = array();
$pid = array();
while($row = mysqli_fetch_assoc($sql)){
    $pcode  = $row['position_code'];
    $pdesc  = $row['position_desc'];
    $status = $row['status'];
    ?>
    <tr>
        <td><?php echo $pcode; ?></td>
        <td><?php echo $pdesc; ?></td>
        <td>
            <div class="custom-control custom-switch">
                <?php
                if($status == 'active'){
                ?>
                <div class="onoffswitch4">
                  <input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="myonoffswitch" checked value="<?php echo $pcode; ?>">
                  <label class="onoffswitch4-label" for="pid[]">
                      <span class="onoffswitch4-inner"></span>
                      <span class="onoffswitch4-switch"></span>
                  </label>
                </div>

                <?php
                }else{
                ?>
                <div class="onoffswitch4">
                    <input type="checkbox" name="onoffswitch4" class="onoffswitch4-checkbox switch" onclick="change(this.value)" id="aaa[]" value="<?php echo $pcode; ?>">
                    <label class="onoffswitch4-label" for="myonoffswitch">
                        <span class="onoffswitch4-inner"></span>
                        <span class="onoffswitch4-switch"></span>
                    </label>
                </div>
                <?php
                }
                ?>

$('.switch').on('change', function() {
    if(this.checked) {
        var id = this.value;
        $.ajax({
            url: "admin_change_status.php",
            data: {
                id:id
            },
            type: "POST",
            success: function(result){

            }
        });
    }else{
        var id2 = this.value;
        $.ajax({
            url: "admin_change_status.php",
            data: {
                id2:id2
            },
            type: "POST",
            success: function(result){
            }
        });
    }
});

i expect to get the value of id in checkbox to jquery.

1
  • You want to get checkbox value when checked or all values? Commented Jul 29, 2019 at 8:11

1 Answer 1

3

If you want to get all values, you can only get it by classname not by id. You have to loop though all classes names and get each one of them, you can do this by each function. Each function loop though all the elements.

$('.onoffswitch4-checkbox').each(function(i){
    var statsValue = $(this).val(); 

    console.log(statsValue);
});
Sign up to request clarification or add additional context in comments.

1 Comment

Once You have stored statsValue, You need to pass it with ajax, expecting that you just need to pass only one value

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.