Undefined array key with session variable

Hi everyone, I am getting an undefined array key with a session variable. The code works fine once I set the variable. Can someone please help me with that issue?
Thanks

<?php
session_start(); 
$servername = "localhost";
$username = "xxx";
$password = "xx";
$database ="xx";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);

// Check connection
if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
}
//echo "Connected successfully";
?> 
<!DOCTYPE html>  
 <html>  
 <head>  
      <meta charset="utf-8">  
      <meta name="viewport" content="width=device-width, initial-scale=1">  
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
 </head>  
 <body>  

<?php 
if(isset($_POST['changeit1']))  {
    if(isset($_POST['checkbox1']) == '1') {
        $_SESSION['checked1'] = true; 
        $query="UPDATE switch_status SET status = '1' WHERE  switch_id = '1'";

    } else {
    $_SESSION['checked1'] = false;
      $query="UPDATE switch_status SET status = '0' WHERE  switch_id = '1'";    

    }
     $result = mysqli_query($conn, $query) or die(mysqli_error());
}    



?>


<form id="form" action="" method="POST" >

         <label>
            Checkbox 1
            <input type="checkbox" name="checkbox1" value="1" class="checkbox"
                <?php echo ($_SESSION['checked1'] == true)? ' checked="checked"':'' ?>>
        </label>
        <input type="hidden" name="changeit1" />  





    </form>
<script>
$(document).ready(function(){
     $('.checkbox').on('change',function(){
        $('#form').submit();
     });
});
</script>
 </body>  
 </html>

Use checkbox with jquery and ajax to update database

Hello everyone
So, my objective is to update four rows in my database (id-> int , status->tinyint) from 0 to 1 and vice verca using a checkbox. This should be done without refreshing the page and using a form without submit button. Based on my research, I was able to write the following code but not sure if it is correct as I am very new to this part. Can someone please help me with this, I have been trying to do this for a very long time.
Thanks you

Index.php

<form id="myform" method="post" action="">
<input type="checkbox" class="ids" name="ids[]" value="1">
<input type="checkbox" class="ids" name="ids[]" value="2">
<input type="checkbox" class="ids" name="ids[]" value="3">
<input type="checkbox" class="ids" name="ids[]" value="4">
</form>

<div id="response"></div>


<script>
 $(document).ready(function() {
     $('#myform').click(function() {

     var state = ($(this).is(':checked')) ? '1' : '0';

       $.ajax({
         url: "test.php",
         type: "post",
         data: $('.ids:checked').serialize(),state: state
         success: function(data) {
         $('#response').html(data);
          }
      });


    });
 });
</script>

test.php

<?php
include_once("db.php");

if (isset($_POST['ids'])) {
$id_value = $_POST['ids'];
 $state = (int)$_POST['state'];

        foreach($id_value as $check) {

          $sql_check = "UPDATE toggle SET status= '$state' WHERE id = '$check' ";
          $result_check = mysqli_query($conn,$sql_check);

         if($result_check){
             echo "sucess";
         }else{
             echo "failure";
         }
        }

}
?>

How to update database value with checkbox using jquery and ajax

Hello everyone
I am trying to update a table [id - int (11) and status - tinyint(1)] which has four rows using checkboxes with jquery and ajax.
I have manage to do it by dynamically generating the checkboxes. But, now I want to use indiviual checkbox to update each value.

Checkbox generated from loop dynamically

while ($row = $result->fetch_assoc()) {
        ?>
                <label class="switch">
                    <input type="checkbox" id='id[] ' class="check" <?php echo $row['status']==1?'checked':'' ?>  />
                    <span class="slider round"></span>
                </label>

Something like

<form method="post" action="">
<input name="id[]" value="1" type="checkbox" checked=""
<input name="id[]" value="1" type="checkbox" checked=""
</form>