Forum Moderators: open

Message Too Old, No Replies

recursive checking of javascript

         

dmmh

11:10 pm on Dec 8, 2004 (gmt 0)

10+ Year Member



basically what I wanne do is somehow, to constantly check, if someone has javascript enabled or not and immediately navigate away from the form if someone turns it off

I am using a very complicated submission form for a website (PHP/ MySQL) I am creating and beside the normal PHP validation, I use javascript to execute certain things that are otherwise hard to accomplish (chained selects, amounts based on user input on prev. page and some other things), all in order to make the process more understandable for the user

However, the process REQUIRES javascript (button onclick = this.form.submit() ) and I dont intend to change that, but it does pose me with a problem.
Ofcourse I can simply add some noscript content, but that is easily bypassed when a user temporarily disables javascript in the browser.

I know I could easily make something like I want with javascript, but that kinda defeats the purpose :p

any ideas?

Bernard Marx

12:29 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



but that kinda defeats the purpose

I reckon you're onto something there ;) No Javascript == No Javascript.
Your options are a combination of <noscript> and possible <meta (refresh)> tags.

the process REQUIRES javascript (button onclick = this.form.submit()

Can I ask why?

whoisgregg

4:28 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



dmmh, are you concerned that people will load the page with javascript enabled, enter invalid information, then disable javascript to submit the form? To circumvent your form validation?

And, Welcome to WebmasterWorld! :)

dmmh

8:19 am on Dec 9, 2004 (gmt 0)

10+ Year Member



no, I am concerned that they will will enter the page with javascript enabled, disable it to enter invalid information then enable it again to submit it ;)

whoisgregg

8:57 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If that scenario is a problem for you, then you should consider getting a different bunch of users. ;)

Barring that possibility, your entire validation routine should exist as a single function which is called with an

onsubmit
in the form tag. It can also be called with onclick and onblur and all the other stuff to happen "live," but you also can add the final check before submitting.

If your users start leaving the javascript disabled long enough to submit the form (circumventing the final check), then how about only enabling the submit button once the form is valid? You could have a <div id="validation"> that you use a constructed .innerHTML to list what steps remained to be valid then showed the submit button once everything was valid. If they disabled javascript at this point to go back and change things, then well, why do put up with these people at all? :)

dmmh

8:57 am on Dec 9, 2004 (gmt 0)

10+ Year Member



@ bernard:

well, some of the fields are optional, however, they do require a radio button to be checked in order to send along a value even if someone doesnt know a certain field ('unknown' or something like that)
I'd like all entries to look the same.

and there is another reason, cant think of it now though lol (its early, sue me :p ) and besides, it would be neat imo

dmmh

9:00 am on Dec 9, 2004 (gmt 0)

10+ Year Member



@ gregg

well, they need javascript enabled to submit it anyway
maybe I shouldnt be making an issue out of it...I use php to validate anyways

dmmh

9:04 am on Dec 9, 2004 (gmt 0)

10+ Year Member



now that we are on a roll (dont think I need to create another topic):

i have a javascript function which draws a html select each time a user presses a button, its defined as an array (< select name = "something[]" )
the values are populated by a MySQL DB query and the javascript is made by PHP.
This works perfectly sofar.
However, one of the selects is hardcoded, the others are dynamic and when submitting the form, it only seems to wanne post the hardcoded one, not the dynamic ones created via innerHTML

am I trying something that is impossible?

Bernard Marx

10:04 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is a Mozilla bug. I don't know of it in IE.
I can't remember if there is a workaround (sorry :( )
- apart from hard-coding a hidden field, and filling its value with delimited values from the created selects

dmmh

11:57 am on Dec 9, 2004 (gmt 0)

10+ Year Member



damn that sucks

Bernard Marx

11:58 am on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Don't take my word for it (or anything else).

dmmh

12:35 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



well actually, I got it working, I made an simple error in the code, which resulted in only one of the array keys being inserted, namely the first. works on IE 6 and Mozilla Firefox 1.0 :)

adni18

12:55 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could validate the fields directly before the form is submitted.

dmmh

1:01 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



yes, I realise that, but thank you for the suggestion.
I just thought it would be more nice to navigate the user away if they try to circumvent the javascript :)

Bernard Marx

2:23 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry. I should have mentioned that the bug is now apparently fixed.
(It's just likely to be 'out there')

kaled

2:43 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use onsubmit to add a parameter to the form's action. If that parameter is missing when you process the form, simply bounce the user back to a page that reads javascript must be enabled (or whatever).

Kaled.

Bernard Marx

4:21 pm on Dec 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think the idea is not to use submit in this case, otherwise the form can simply be submitted with JS turned off all along. This is all very upside-down.

On the subject of:

Testing to see whether JS has been temporarily disabled

Here's a way. Very simple really.

- Get the start time;
- loop at regular (2 sec) intervals;
- update current time;
- if difference is, say, 2 secs greater than the delay..
(Got to allow for heavy processes that may stall scripting. This also rules out use of modal prompts & dialogs)
..then set the cheat.flag to true.

It my test, turning script off at some point seems to cause scripting to sleep. It carries on when reactivated. All state is maintained. If this doesn't happen, the proof of the pudding is still the diff between current time and the last recorded time.

This does require that JS is on to start with. So you'd need the form inputs unavailable at first if scripting is off.

-----------------------------------------

k = -1; // just for demo

var cheat =
{
flag : false,
t0 : new Date().getTime(),
timer: null,

check: function()
{
var t1 = new Date().getTime();
var diff = t1 - this.t0;
this.t0 = t1;
/*--- just for demo ----------------------------------*/
if(document.getElementById("d0"))/* when ready*/
document.getElementById("d0").innerHTML = ++k +': '+diff;
/*----------------------------------------------------*/
if(diff > 4000){
this.flag = true;
/**/alert("cheat!");
clearTimeout(this.timer);
}

},

start: function(){ this.timer = setInterval('cheat.check()',2000)}
}

cheat.start();

-----------------------------------

ah yes. You'll need a div: id="d0" for the demo output;

dmmh

9:25 pm on Dec 10, 2004 (gmt 0)

10+ Year Member



mmm this seems like a good idea :)
Im gonna try this soon, let ya know, thanks for all the help sofar people, thumbs up

dmmh

9:01 pm on Dec 11, 2004 (gmt 0)

10+ Year Member



n1 man, thank you very much :)

natty

2:09 am on Dec 12, 2004 (gmt 0)

10+ Year Member



hi all ,

and sorry if i am late and have missed a point that makes what im saying irrelivant.

but i would say cant rely on js for form validation, its nice , and most of the time it is used by people with js turned on, but i woudl say all validation shoudl be done on the server side aswell. simply because you cant count on js being turned on, or fiddled with whist entering form information.
afew simple initial checks in the server side validation, and each submission need not hog valuable resources (?)

just my tuppence.

:)

dmmh

8:42 pm on Dec 12, 2004 (gmt 0)

10+ Year Member



I dont rely on javascript for validation, I just wanted to navigate people away from the submission process who try to cheat ;)

and this is simply an awesome way

when my site is done, which will be another month or so, maybe I can show you why I chose to do it this way :)