Как брать foreach из другого файла php
Я пытаюсь делать так чтобы был post запрос с помощью js и менялось значение в json формате, но он не видит $element1
index.php
<div class="text"></div>
<form> <?php
$json_data345234514523452 = file_get_contents("accounts/".$_COOKIE['id'].".json");
$product4561 = json_decode($json_data345234514523452, true);
?>
<?php
$json_data = file_get_contents("accounts/".$_COOKIE['id'].".json");
$products2 = json_decode($json_data, true);
if(count($products) != 0){
foreach($products2 as $product2){
?>
<?php
}
}
?>
<?php
$json2323232 = file_get_contents('avatar/'.$_COOKIE['id'].'.json');
$data2323232 = json_decode($json2323232, true);
?>
<?php
if($_COOKIE['accboolg1'] == true){
$products65724 = file_get_contents('accounts/'.$_COOKIE['id'].'.json');
$products6573432 = json_decode($products65724, true);
foreach($data2323232 as $element1){
$url = $element1;
$file = file_get_contents($url); // получить файл
$name = basename($url); // получить имя файла
$ext = pathinfo($url, PATHINFO_EXTENSION); // получить расширение файла
$name2 =pathinfo($url, PATHINFO_FILENAME); // получить имя файла без расширения
if($products2){
echo '<input value="'.$name2.'" style="background-image:url(http://susamo9a.beget.tech/'.$element1.');" type="submit" name="'.$name2.'"><br>';
}else{
echo "Извини но ты такой аватарки не имеешь!";
}
}
}
?>
</form>
<?php echo $_COOKIE['id'] ?>
<script>
const form = document.querySelector('form');
console.log(form);
form.addEventListener('submit', evt => {
evt.preventDefault();
fetch('form.php', {
method: 'POST',
body: new FormData(form)
}).then(function(response){
console.log(response)
}).catch(function(error){
console.log(error);
});
})
</script>
form.php
<?php
//$method = $_SERVER['REQUEST_METHOD'];
//print_r($_POST);
//$json_data2657 = file_get_contents("../accounts/".$_COOKIE['id'].".json");
//$products657 = json_decode($json_data2657, true);
//$products657[$_COOKIE['id']][$_COOKIE['accaname']]['Avatar'] = $_POST;
//file_put_contents('../accounts/'.$_COOKIE['id'].'.json', json_encode($products657));
$method = $_SERVER['REQUEST_METHOD'];
print_r($_POST);
$json_data2657 = file_get_contents("../accounts/".$_COOKIE['id'].".json");
$products657 = json_decode($json_data2657, true);
$products657[$_COOKIE['id']][$_COOKIE['accname']]['Avatar'] = $element1;
file_put_contents('../accounts/'.$_COOKIE['id'].'.json', json_encode($products657));
У меня выходит такая картина и не могу понять почему он не хочет менять accounts/$_COOKIE['id'].json
