Advertisement
jargon

// Linedancer (LD4) :: "LD4 Framework/LD4 Classes/LD4 Form URL Class.js"

Dec 18th, 2024
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // Linedancer (LD4) :: "LD4 Framework/LD4 Classes/LD4 Form URL Class.js"
  2.  
  3. class LD4FormURLClass {
  4.  
  5.     whichBoolean(value) {
  6.                
  7.         if (typeof value === "string") {
  8.             value = value.toLowerCase();
  9.         }
  10.        
  11.         if (value === "true" || value === true) {
  12.             return true;
  13.        
  14.         } else if (value === "false" || value === false) {
  15.             return false;
  16.         }
  17.        
  18.         return null; // Not a boolean
  19.     }
  20.  
  21. formulate(formSubmitID, formID, outputID) {
  22.     // Ensure the button is set up with an event listener
  23.     const submitButton = document.getElementById(formSubmitID);
  24.     if (!submitButton) {
  25.         console.error(`Submit button with ID '${formSubmitID}' not found.`);
  26.         return;
  27.     }
  28.  
  29.     // Replace any existing listener
  30.     submitButton.replaceWith(submitButton.cloneNode(true));
  31.  
  32.     submitButton.addEventListener('click', () => {
  33.         // Base URL
  34.         const baseUrl = `http://${window.location.hostname}/LD4.php`;
  35.  
  36.         // Get form data
  37.         const form = document.getElementById(formID);
  38.         if (!form) {
  39.             console.error(`Form with ID '${formID}' not found.`);
  40.             return;
  41.         }
  42.         const formData = new FormData(form);
  43.  
  44.         // Build query parameters
  45.         const queryParams = new URLSearchParams();
  46.         queryParams.append('con', 'Recurse');
  47.  
  48.         // Process form data
  49.         for (const [key, value] of formData.entries()) {
  50.             // Handle checkboxes explicitly
  51.             const element = form.querySelector(`[name="${key}"]`);
  52.             if (element && element.type === 'checkbox') {
  53.                 if (element.checked) {
  54.                     // Append key with an empty value for checked checkboxes
  55.                     queryParams.append(key, '');
  56.                 } else {
  57.                     // Omit unchecked checkboxes entirely
  58.                     continue;
  59.                 }
  60.             } else if (this.whichBoolean(value) === true || value === '') {
  61.                 // Include just the key for `true` or empty string values
  62.                 queryParams.append(key, '');
  63.             } else if (this.whichBoolean(value) !== false) {
  64.                 // Include key-value pair for other non-false values
  65.                 queryParams.append(key, value);
  66.             }
  67.             // Omit the key entirely if the value is `false`
  68.         }
  69.  
  70.         // Construct the full URL
  71.         const fullUrl = `${baseUrl}?${queryParams.toString().replace(/=(?=&|$)/g, '')}`;
  72.  
  73.         // Display the URL in the output element
  74.         const outputElement = document.getElementById(outputID);
  75.         if (outputElement) {
  76.             outputElement.textContent = fullUrl;
  77.         } else {
  78.             console.warn(`Output element with ID '${outputID}' not found. Full URL: ${fullUrl}`);
  79.         }
  80.  
  81.         // Optional: Uncomment to redirect to the generated URL
  82.         // window.location.href = fullUrl;
  83.     });
  84. }
  85. }
  86.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement