I am programming an application as we all do... But for now i have stumpled uppon an error i can't figure out and i thought it would be nice to get some new eyes to look at it.
Let me explain. Shortly explained, when i load the page and it is fresh the DOM functions work fine, but after i request the same page with AJAX the DOM functions don't work.
I have my index.php as you see below and the included ajaxmineopslag.php is also the file i make a request for with AJAX. The AJAX request is called in loggedinpage.js as the function "sendDataAJAX". The function is triggered when you onclick either the id mine_opslag_l or classer_tilbud_l. These id's trigger the function and has a parameter that defines which page that gets included in the getinnerclassertilbud.php which is the page the request is sent to.
After the request has been sent the ajaxmineopslag.php does as supposed but the DOM elements don't work. The javascript function that does not work now is the first one at the loggedinpage.js where the settings-opslag-wrapper should be displayed when the settings-opslag is clicked.
Thanks for your patience and time!
index.php
<!DOCTYPE>
<html>
<head>
<title>Ajax</title>
<style>
.adddisplay {
display: block;
}
.settings-opslag-wrapper {
display: none;
}
</style>
</head>
<body>
<a id="mine_opslag_l">Mine opslag</a>
<a id="classer_tilbud_l">Classers Tilbud</a>
<div id="mainchangercontent">
<?php include "include/ajaxmineopslag.php"; ?>
</div>
<script type="text/javascript" src="js/loggedinpage.js"></script>
</body>
</html>
My loggedinpage.js
const settingclick = document.getElementsByClassName('opslag-wrapper');
for (var i = 0; i < settingclick.length; i++) {
settingclick[i].getElementsByClassName('settings-opslag')[0].addEventListener('click', function() {
this.getElementsByClassName('settings-opslag-wrapper')[0].classList.toggle("adddisplay");
});
}
document.getElementById("mine_opslag_l").addEventListener("click", function() {
sendDataAJAX("1");
});
document.getElementById("classer_tilbud_l").addEventListener("click", function() {
sendDataAJAX("2");
});
function sendDataAJAX(dataz) {
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 & this.status == 200) {
document.getElementById("mainchangercontent").innerHTML = this.responseText;
}
};
xmlhttp.open("GET", "getinnerclassertilbud.php?p=" + dataz, true);
xmlhttp.send();
}
The getinnerclassertilbud.php page
<?php
//init.php contains my db connection and such...
include "core/init.php";
if (isset($_GET)) {
if (isset($_GET["p"])) {
$p = $_GET["p"];
if (!empty($p)) {
if ($p == 1) {
include "include/ajaxmineopslag.php";
} else if ($p == 2) {
//this is an other page like ajaxmineopslag.php, but this page does not annoy me for now
include "include/ajaxclassertilbud.php";
}
}
}
}
?>
ajaxmineopslag.php page
<div class="opslag-wrapper">
<div class="settings-opslag">
<p>View hidden div</p>
<div class="settings-opslag-wrapper">
<p>This is the hidden div that does not show up after AJAX request</p>
</div>
</div>
</div>
<div class="opslag-wrapper">
<div class="settings-opslag">
<p>View hidden div</p>
<div class="settings-opslag-wrapper">
<p>This is the hidden div that does not show up after AJAX request</p>
</div>
</div>
</div>
<div class="opslag-wrapper">
<div class="settings-opslag">
<p>View hidden div</p>
<div class="settings-opslag-wrapper">
<p>This is the hidden div that does not show up after AJAX request</p>
</div>
</div>
</div>