[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/js/dist/script-modules/block-library/form/ -> view.js (source)

   1  var __webpack_exports__ = {};
   2  /* wp:polyfill */
   3  let formSettings;
   4  try {
   5    formSettings = JSON.parse(document.getElementById('wp-script-module-data-@wordpress/block-library/form/view')?.textContent);
   6  } catch {}
   7  
   8  // eslint-disable-next-line eslint-comments/disable-enable-pair
   9  /* eslint-disable no-undef */
  10  document.querySelectorAll('form.wp-block-form').forEach(function (form) {
  11    // Bail If the form settings not provided or the form is not using the mailto: action.
  12    if (!formSettings || !form.action || !form.action.startsWith('mailto:')) {
  13      return;
  14    }
  15    const redirectNotification = status => {
  16      const urlParams = new URLSearchParams(window.location.search);
  17      urlParams.append('wp-form-result', status);
  18      window.location.search = urlParams.toString();
  19    };
  20  
  21    // Add an event listener for the form submission.
  22    form.addEventListener('submit', async function (event) {
  23      event.preventDefault();
  24      // Get the form data and merge it with the form action and nonce.
  25      const formData = Object.fromEntries(new FormData(form).entries());
  26      formData.formAction = form.action;
  27      formData._ajax_nonce = formSettings.nonce;
  28      formData.action = formSettings.action;
  29      formData._wp_http_referer = window.location.href;
  30      formData.formAction = form.action;
  31      try {
  32        const response = await fetch(formSettings.ajaxUrl, {
  33          method: 'POST',
  34          headers: {
  35            'Content-Type': 'application/x-www-form-urlencoded'
  36          },
  37          body: new URLSearchParams(formData).toString()
  38        });
  39        if (response.ok) {
  40          redirectNotification('success');
  41        } else {
  42          redirectNotification('error');
  43        }
  44      } catch (error) {
  45        redirectNotification('error');
  46      }
  47    });
  48  });
  49  


Generated : Sat Feb 22 08:20:01 2025 Cross-referenced by PHPXref