I am new to JQuery and I use JSON stringify to pass my jquery array to the PHP but I've got an error. Can anyone help me to pass this jquery array into PHP?
JQUERY
<script>
$(document).ready(function(){
let arraySelected = [];
var arrayJSON = JSON.stringify(arraySelected);
$(".btn").click(function(){
const find = jQuery.inArray($(this).val(),arraySelected);
if(find !==-1){
arraySelected.splice(find, 1);
console.log($(this).val() +" has been deleted");
$(this).css("background-color", "");
}else{
console.log($(this).val() +" has been pushed");
arraySelected.push($(this).val());
$(this).css("background-color", "red");
$.ajax({
type: "POST",
url: "index.php",
data: { myArray : arraySelected },
success: function() {
alert("Success");
}
});
}
console.log(arraySelected);
})
});
</script>
PHP
<?php
$myArray = json_decode($_POST['myArray']);
?>
This is the error shown on my webpage
Notice: Undefined index: myArray in C:\xampp\htdocs\JQuery PHP\JQuery PHP 1\index.php on line 13