How do I disable submit button on forms for some users

Status
Not open for further replies.
I think navigator.cookieEnabled should work in most versions of IE. So something like ...

Code:
requirejs(['fab/fabrik'], function () {
   Fabrik.addEvent('fabrik.form.loaded', function (form) {
      if (!navigator.cookieEnabled) {
         form._getButton('Submit').disabled="disabled";
      }
   });
});

... in a components/com_fabrik/js/form_X.js file (replace X with numeric id of form) should do it.

You could do it with jQuery(document).ready(function() {...}), and select the submit button with vanialla JS, but doing it with the Fabrik event is just convenient, as you get passed a reference to the form object you can then use to get the button with _getButton(), and don't need to worry about what the form ID is.

But just for completeness ...

Code:
jQuery(document).ready(function () {
   if (!navigator.cookieEnabled) {
     // replace X with your numeric form ID, or use some other selector
      jQuery('#fabrikSubmit_X').disabled="disabled";
   }
});

-- hugh
 
Last edited:
Hi!

Thanks @hugh

But I can't make the js-script work. It's loaded to the form and with no js-errors...

Regards
Matt
 
Last edited:
Ah, sorry, I thought you meant check if they have cookies enabled globally.

To test for a specific cookie for this page, you have to look in document.cookie.

On your page, before clicking "accept", document.cookie is empty. After accepting, it is ...

Code:
"SL_GWPT_Show_Hide_tmp=1; SL_wptGlobTipTmp=1; plg_system_eprivacy=2018-10-04; _ga=GA1.2.98151043.1538684038; _gid=GA1.2.933974887.1538684038; _gat=1"

... and I presume you want to make sure the plg_system_eprivacy one is set, so something like ...

Code:
jQuery(document).ready(function () {
   // cookies disabled entirely, or eprivacy cookie not present?
   if (!navigator.cookieEnabled || !document.cookie.test('eprivacy') {
     // replace X with your numeric form ID, or use some other selector
      jQuery('#fabrikSubmit_X').disabled="disabled";
   }
});

If you wanted to, you could also test the date in the cookie value. Although I presume that plugin should correctly handle expiring it.

-- hugh
 
Sorry, still no success to hide the submit button.

Placed the code in a file components/com_fabrik/js/custom.js

Regards
Matt
 
It has to be named form_X.js (where X is the numeric form ID). A file called custom.js won't get loaded.

As per my first reply:

... in a components/com_fabrik/js/form_X.js file (replace X with numeric id of form)

-- hugh
 
JavaScript:
jQuery(document).ready(function () {
   // cookies disabled entirely, or eprivacy cookie not present?
   if (!navigator.cookieEnabled || !document.cookie.test('eprivacy')){
     jQuery('#fabrikSubmit_X')[0].disabled='disabled';
     // replace X with your numeric form ID, or use some other selector
   }
});
 
Thanks!

Now it works much better!

I updated the code to show an alert. But is possible to show a message beside the Submit button instead?
Code:
jQuery(document).ready(function () {
   // cookies disabled entirely, or eprivacy cookie not present?
   if (!navigator.cookieEnabled || !document.cookie.test('eprivacy')){
     jQuery('#fabrikSubmit_1')[0].disabled='disabled';
        alert('Please, Accept cookies before submitting the form');
     // replace X with your numeric form ID, or use some other selector
   }
});

Regards
Matt
 
Last edited:
But sadly. with IE 8 and older the script don't work.
It would be just fine if it just blocked those user and showed them a message instead of the form. Is it doable?

Matt
 
Yes, I know. But many big organisations here in Sweden are very slow to update, and they are using our sites...
 
But is possible to show a message beside the Submit button instead?

No Fabrik magic involved, just use vanilla Javascript, or jQuery, to insert something into the DOM. So for example, the jQuery insertBefore() method:

http://api.jquery.com/insertbefore/

Code:
jQuery('<div>YOUR MESSAGE HERE</div>').insertBefore('#fabrikSubmit_X');

As before, replace X with your form ID, or use some other selector.

-- hugh
 
Digging deeper, it seems like you can't rely on navigator.cookieEnabled in any version of IE, because ... well, of course you can't, this is IE.

Code:
jQuery(document).ready(function () {
    // workaround for IE, make sure cookies are enabled by actually setting one (if currently empty)!
    var cookies = ("cookie" in document && (document.cookie.length > 0 ||
       (document.cookie = "test").indexOf.call(document.cookie, "test") > -1));

   // cookies disabled entirely, or eprivacy cookie not present?
   if (!cookies || !document.cookie.test('eprivacy')){
     jQuery('#fabrikSubmit_1')[0].disabled='disabled';
     // replace X with your numeric form ID, or use some other selector
     jQuery('<div>Please, Accept cookies before submitting the form</div>').insertBefore('#fabrikSubmit_X');
   }
});

-- hugh
 
Hi!

Thanks @cheesegrits
The scripts works fine, and its works the same way for all browsers, but that's ok. I just added an pop up to inform the users to accept cookies.
For users with old IE browsers I added a PHP form plugin to redirect them to a page with information about to update the browser.

Regards
Matt
 
Sadly I have to abandon this solution, because of IE 11 with compatibility mode.
I instead uses a message that state "this site uses cookies"...

Matt
 
Status
Not open for further replies.
We are in need of some funding.
More details.

Thank you.

Members online

No members online now.
Back
Top