Comments





GUESTBOOK


src ===Copy&Paste "send.html" boilerplate for website integration== <div id="comments"></div> <br> <form action="" method="POST" id="commentForm"> <input type="text" name="name" placeholder="Your Name"><br> <textarea name="comment" rows="4" cols="50"></textarea> <input type="hidden" name="jsonFilename" value="" id="jsonFilenameInput"> <input type="submit" value="Submit"><br> </form> <script> window.addEventListener("DOMContentLoaded", function() { var urlbase; // Declare the urlbase variable fetch('https://ry3yr.github.io/comments.txt') .then(response => response.text()) .then(data => { urlbase = data.trim(); // Assign the value to urlbase }) .then(() => { var url = window.location.href; var cacheBuster = Date.now(); // or you can use a version number if you prefer var jsonFilename = url.replace(/[^A-Za-z0-9]/g, "") + ".json"; var jsonUrl = urlbase + jsonFilename; jsonUrl = jsonUrl + "?v=" + cacheBuster; document.getElementById("jsonFilenameInput").value = jsonFilename; document.getElementById("commentForm").action = urlbase + "submit_comment.php"; fetch(jsonUrl) .then(response => response.json()) .then(data => { if (Array.isArray(data)) { data.reverse(); data.forEach(entry => { var comment = entry.comment; var name = entry.name; renderComment(name, comment); }); } else { console.log("No comments found for the current URL."); } }) .catch(error => { console.log("Error fetching JSON file:", error); }); }); }); function renderComment(name, comment) { var commentsElement = document.getElementById("comments"); var commentElement = document.createElement("div"); commentElement.innerHTML = "<strong>" + name + "</strong>: " + comment; commentsElement.appendChild(commentElement); } </script> ====.httaccess==== Header set Access-Control-Allow-Origin "*" Header set Access-Control-Allow-Methods "GET,PUT,POST,DELETE" Header set Access-Control-Allow-Headers "Content-Type, Authorization" ====submit_comment.php=== < ?php if ($_SERVER["REQUEST_METHOD"] === "POST") { $url = $_SERVER["HTTP_REFERER"]; // Get the current URL $filename = preg_replace("/[^A-Za-z0-9]/", "", $url); // Remove non-alphanumeric characters from the URL $name = $_POST["name"]; // Get the submitted name $comment = $_POST["comment"]; // Get the submitted comment $jsonFilename = $_POST["jsonFilename"]; // Get the submitted jsonFilename $existingData = file_get_contents($jsonFilename); $existingJson = json_decode($existingData, true); if (!$existingJson) { $existingJson = []; } // Create a new data entry with the submitted name and comment $newData = array( "name" => $name, "comment" => $comment ); // Append the new data to the existing JSON data $existingJson[] = $newData; // Encode the merged data as JSON $json = json_encode($existingJson); // Write the updated JSON back to the file file_put_contents($jsonFilename, $json); echo "<a href=javascript:history.back()>[View Comment]</a>"; } ?> </pre> </details>