Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Linedancer (LD4) :: "LD4 Framework/LD4 Classes/LD4 Form URL Class.js"
- class LD4FormURLClass {
- whichBoolean(value) {
- if (typeof value === "string") {
- value = value.toLowerCase();
- }
- if (value === "true" || value === true) {
- return true;
- } else if (value === "false" || value === false) {
- return false;
- }
- return null; // Not a boolean
- }
- formulate(formSubmitID, formID, outputID) {
- // Ensure the button is set up with an event listener
- const submitButton = document.getElementById(formSubmitID);
- if (!submitButton) {
- console.error(`Submit button with ID '${formSubmitID}' not found.`);
- return;
- }
- // Replace any existing listener
- submitButton.replaceWith(submitButton.cloneNode(true));
- submitButton.addEventListener('click', () => {
- // Base URL
- const baseUrl = `http://${window.location.hostname}/LD4.php`;
- // Get form data
- const form = document.getElementById(formID);
- if (!form) {
- console.error(`Form with ID '${formID}' not found.`);
- return;
- }
- const formData = new FormData(form);
- // Build query parameters
- const queryParams = new URLSearchParams();
- queryParams.append('con', 'Recurse');
- // Process form data
- for (const [key, value] of formData.entries()) {
- // Handle checkboxes explicitly
- const element = form.querySelector(`[name="${key}"]`);
- if (element && element.type === 'checkbox') {
- if (element.checked) {
- // Append key with an empty value for checked checkboxes
- queryParams.append(key, '');
- } else {
- // Omit unchecked checkboxes entirely
- continue;
- }
- } else if (this.whichBoolean(value) === true || value === '') {
- // Include just the key for `true` or empty string values
- queryParams.append(key, '');
- } else if (this.whichBoolean(value) !== false) {
- // Include key-value pair for other non-false values
- queryParams.append(key, value);
- }
- // Omit the key entirely if the value is `false`
- }
- // Construct the full URL
- const fullUrl = `${baseUrl}?${queryParams.toString().replace(/=(?=&|$)/g, '')}`;
- // Display the URL in the output element
- const outputElement = document.getElementById(outputID);
- if (outputElement) {
- outputElement.textContent = fullUrl;
- } else {
- console.warn(`Output element with ID '${outputID}' not found. Full URL: ${fullUrl}`);
- }
- // Optional: Uncomment to redirect to the generated URL
- // window.location.href = fullUrl;
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement