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>