{"id":1636,"date":"2025-03-03T14:57:14","date_gmt":"2025-03-03T13:57:14","guid":{"rendered":"https:\/\/test.kending.de\/?p=1636"},"modified":"2025-03-04T02:01:14","modified_gmt":"2025-03-04T01:01:14","slug":"formulartest","status":"publish","type":"post","link":"https:\/\/test.kending.de\/?p=1636","title":{"rendered":"Formulartest"},"content":{"rendered":"<div id=\"formFields\"><\/div>\n<p><button id=\"submitButton\">Senden<\/button><br \/>\n<script src=\"https:\/\/script.google.com\/macros\/s\/AKfycbwWpRHW2UHryJ8V40ldxx-bUhYm2lFsJbbrXy9XTCE0fEASLdpRG-62W3PkcJ9j_qsp\/exec\"><\/script><\/p>\n<p><script>\n    let config = {};\n    const scriptURL = \"https:\/\/script.google.com\/macros\/s\/AKfycbztaJIKMhv2IfSfA66KUXATk991WGwV8Ab7hqdw3DDgBrigWE5clafzGzSGILcKUgE-\/exec\"; \/\/ \u2705 Zentrale URL-Variable<\/p>\n<p>    async function loadConfig() {\n        let response = await fetch(scriptURL + \"?action=getFormConfig\");\n        config = await response.json();<\/p>\n<p>        if (config.css_url) {\n            let link = document.createElement(\"link\");\n            link.rel = \"stylesheet\";\n            link.href = config.css_url;\n            document.head.appendChild(link);\n        }<\/p>\n<p>        if (config.form_title) {\n            document.title = config.form_title;\n        }<\/p>\n<p>        return config;\n    }<\/p>\n<p>    async function loadForm() {\n        let config = await loadConfig();\n        let formFields = document.getElementById(\"formFields\");<\/p>\n<p>        \/\/ Zeigt die GPS-Warnung an, falls in Config gesetzt\n        if (config.GPS_Info) {\n            let gpsInfo = document.createElement(\"p\");\n            gpsInfo.textContent = config.GPS_Info;\n            gpsInfo.style.color = \"red\";\n            formFields.appendChild(gpsInfo);\n        }<\/p>\n<p>        \/\/ Falls GPS aktiviert ist, Standort abrufen\n        if (config.GPS_Info) {\n            navigator.geolocation.getCurrentPosition(position => {\n                let inputLat = document.createElement(\"input\");\n                inputLat.type = \"hidden\";\n                inputLat.name = \"latitude\";\n                inputLat.value = position.coords.latitude;<\/p>\n<p>                let inputLon = document.createElement(\"input\");\n                inputLon.type = \"hidden\";\n                inputLon.name = \"longitude\";\n                inputLon.value = position.coords.longitude;<\/p>\n<p>                formFields.appendChild(inputLat);\n                formFields.appendChild(inputLon);<\/p>\n<p>                \/\/ Falls Google Maps API-Key vorhanden ist, kleine Karte anzeigen\n                if (config.maps_api_key && config.maps_api_key.trim() !== \"\") {\n                    let mapFrame = document.createElement(\"iframe\");\n                    mapFrame.width = \"100%\";\n                    mapFrame.height = \"250\";\n                    mapFrame.style.border = \"none\";\n                    mapFrame.src = `https:\/\/www.google.com\/maps\/embed\/v1\/place?key=${config.maps_api_key}&q=${position.coords.latitude},${position.coords.longitude}`;\n                    formFields.appendChild(mapFrame);\n                }\n            }, error => {\n                console.warn(\"GPS konnte nicht ermittelt werden.\", error);\n            });\n        }<\/p>\n<p>        config.fields.forEach(field => {\n            let label = document.createElement(\"label\");\n            label.textContent = field.question + (field.required ? \" *\" : \"\");<\/p>\n<p>            let input;\n            if (field.type === \"choice\") {\n                input = document.createElement(\"select\");\n                field.options.forEach(option => {\n                    let opt = document.createElement(\"option\");\n                    opt.value = option;\n                    opt.textContent = option;\n                    input.appendChild(opt);\n                });\n            } else {\n                input = document.createElement(\"input\");\n                input.type = field.type;\n            }<\/p>\n<p>            input.name = field.name;\n            if (field.required) input.required = true;\n            formFields.appendChild(label);\n            formFields.appendChild(input);\n            formFields.appendChild(document.createElement(\"br\"));\n        });<\/p>\n<p>        let submitButton = document.getElementById(\"submitButton\");\n        if (config.absenden_text) {\n            submitButton.textContent = config.absenden_text;\n        }\n    }<\/p>\n<p>    document.getElementById(\"dynamicForm\").addEventListener(\"submit\", async function(e) {\n        e.preventDefault();\n        let formData = {};\n        new FormData(this).forEach((value, key) => formData[key] = value);<\/p>\n<p>        let response = await fetch(scriptURL, { \/\/ \u2705 Hier wird die zentrale Variable genutzt\n            method: \"POST\",\n            body: JSON.stringify(formData),\n            headers: { \"Content-Type\": \"application\/json\" }\n        });<\/p>\n<p>        if (response.ok) {\n            alert(config.bestaetigung || \"Deine Daten wurden \u00fcbermittelt.\");\n            this.reset();<\/p>\n<p>            if (config.redirect_url) {\n                window.location.href = config.redirect_url;\n            }\n        } else {\n            alert(\"Fehler beim Senden!\");\n        }\n    });<\/p>\n<p>    loadForm();\n<\/script><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Senden<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1636","post","type-post","status-publish","format-standard","hentry","category-allgemein"],"_links":{"self":[{"href":"https:\/\/test.kending.de\/index.php?rest_route=\/wp\/v2\/posts\/1636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/test.kending.de\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/test.kending.de\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/test.kending.de\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/test.kending.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1636"}],"version-history":[{"count":6,"href":"https:\/\/test.kending.de\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions"}],"predecessor-version":[{"id":1642,"href":"https:\/\/test.kending.de\/index.php?rest_route=\/wp\/v2\/posts\/1636\/revisions\/1642"}],"wp:attachment":[{"href":"https:\/\/test.kending.de\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/test.kending.de\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/test.kending.de\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}