PDA

View Full Version : disable submit if cookie exists


Hugh Jass
08-03-2000, 09:34 PM
I'm looking for a script which will disable the submit button of a form if the end user has visited before.

Can you help ?

Titan
08-04-2000, 07:23 PM
Well ummm MR. Jass http://talkboard.123webmaster.com/ubbhtml/smile.gif

I'm making a couple assumptions here..
1) your using a perl script to pass a username/password.

2) it's JS inserted in the perl as a require.

if that's so you need to add an if statement to the JS as such...

if (auth_remember_login.checked) {
var Remember_login = "on";
}

}
if( blnValid )

if (Remember_login == "on") {
document.cookie = "username=" + Username + ";expires=" + expdate.toGMTString() + ";";
document.cookie = "password=" + Password + ";expires=" + expdate.toGMTString() + ";";
document.cookie = "remember_login=" + Remember_login + ";expires=" + expdate.toGMTString() + ";";
}

if (Remember_login != "on") {
var Expires = "-1";
document.cookie = "username=" + Username + ";expires=" + Expires + ";";
document.cookie = "password=" + Password + ";expires=" + Expires + ";";
document.cookie = "remember_login=" + Remember_login + ";expires=" + Expires + ";";
}

that's WITHOUT seeing what you already have...

the cgi needs to be modified in the script...as such

in your config add:
$print_get_cookies = '1';

and the subroutine looking for input should be modied similar to this...

print qq|
<BODY |;
if ($print_get_cookies) { print qq~ OnLoad="GetCookies()"~; }
print qq|>|;
&cookie;

the &cookie is the cookie subroutine...

and lastly edit the post method line to something like this:
<FORM ACTION=$ENV{'SCRIPT_NAME'} METHOD=POST name="action" OnSubmit="return IsValid()">

Hope this leads you on your way...
you can also check out:
http://webreview.com/wr/pub/98/08/07/perl/index.html

it has a useful overview of how cookies can be recycled.

------------------
Best of Luck!

Titan

Hugh Jass
08-05-2000, 12:32 PM
Oh wow, you really know your stuff.

You see that word under my name ? ( Newbie )

It should read “ Newbie, and then some ! “ I’m still learning buttons & mouseovers etc.

It looks like I have some studying to do.

Here’s a brief explanation of what I’m trying to do:

I have a script for a form, which prompts the end user to enter their name, email and a choice from a drop-down menu.

On submit, the user is sent to the page chosen from the menu, and the details mailed to me.

Simple enough so far !

What I would like to do is disable the submit button when the user re-visits.

The user will therefore only be allowed 1 choice from the menu, unless they delete the cookie.

I've put the script I've got so far, on the web if you'd care to take a look.

It's at www.e-myther.redhotant.com/script.htm (http://www.e-myther.redhotant.com/script.htm)

I really appreciate your help.

Many thanks

Hugh.

Hugh Jass
08-06-2000, 04:12 PM
Here’s a script sent to me by someone called wiseone from a forum far far away.

It works fine except that the user is not sent to the page of their choice.

For some reason the return false for the cookie conflicts with the form validater.
Switching their positions and adding enable after the enabled submit button, works.

However, the data, which was previously mailed automatically, no longer gets sent.

Instead, a “New Message ” window appears with no data in it.

Does anyone know a way around this?

<HTML>
<HEAD><TITLE></TITLE>
<SCRIPT LANGUAGE="JavaScript1.2">
function setCookie(name, value, expire){
document.cookie=name + "=" + escape(value)
+ ((expire==null) ? "" : ("; expires=" + expire.toGMTString( )))
}

function getCookie(Name) {
var search=Name + "="
if (document.cookie.length>0){
offset=document.cookie.indexOf(search)
if (offset!=-1) {
offset +=search.length
end=document.cookie.indexOf(";",offset)
if (end==-1)
end=document.cookie.length
return unescape(document.cookie.substring(offset, end))
}}}

function register(name) {
var today=new Date( )
var expires=new Date( )
expires.setTime(today.getTime( ) + 1000*60*60*24*365)
setCookie("Previous_visit_on", name, expires)
}
</SCRIPT>
<SCRIPT language=JavaScript>
<!-- Hide from old browsers
//this section checks the validity of the details entered by the user
function validate_form() {
validity = true; // assume valid
if (!check_empty(document.formabc.username.value))
{ validity = false; alert('Name field is empty!'); }
if (!check_email(document.formabc.EMAIL.value))
{ validity = false; alert('Email address is invalid!'); }
if (document.formabc.Learn.selectedIndex==0)
{ validity =false; alert('No tutorial was selected!'); }
if (validity){
alert ("Let me know if it helps !");
setTimeout ("changePage()", 2000)
}
return validity;
}
function changePage(){
location=document.formabc.Learn.options[document.formabc.Learn.selectedIndex].value
}
function check_empty(text) {
return (text.length > 0); // returns false if empty
}
function check_email(address) {
if ((address == "")
| | (address.indexOf ('@') == -1)
| | (address.indexOf ('.') == -1))
return false;
return true;
}
// -->
</SCRIPT>
</HEAD>
<BODY bgColor=#fffff0>
<form name="formabc" method="post" action="mailto:hugh_jass@mail-me.com" onSubmit="return false; return validate_form()">
Name:<br><input type="text" size=50 name="username"><br>
Email:<br><input type="text" size=50 name="EMAIL"><br>
Tutorials:<size=50 name="TUTORIALS"><br>
<SELECT NAME="Learn">
<OPTION VALUE="Null"></OPTION>
<OPTION VALUE="choice01.htm">choice01</OPTION>
<OPTION VALUE="choice02.htm">choice02</OPTION>
<OPTION VALUE="choice03.htm">choice03</OPTION>
<OPTION VALUE="choice04.htm">choice04</OPTION>
<OPTION VALUE="choice05.htm">choice05</OPTION>
<OPTION VALUE="choice06.htm">choice06</OPTION>
<OPTION VALUE="choice07.htm">choice07</OPTION>
<OPTION VALUE="choice08.htm">choice08</OPTION>
<OPTION VALUE="choice09.htm">choice09</OPTION>
</select>
<br>
<SCRIPT LANGUAGE="JavaScript1.2">
var yourname=getCookie("Previous_visit_on")
if (yourname!=null)
document.write('<input type="submit" name="submitabc" onclick="register(this.form.username.value); history.go (0)" value="Submit"disabled>')
else
document.write('<input type="submit" name="submitabc" onclick="register(this.form.username.value); history.go (0)" value="Submit">')
</SCRIPT>
<input type="reset" name="resetabc" value="Reset"></form>
</BODY>
</HTML>


Thanks

Hugh.

Hugh Jass
08-07-2000, 05:35 PM
Eureka !

Got it.

I've posted it at the old http://www.e-myther.redhotant.com/script.htm address.

It took me a while to figure it out.

I've got a dimple in my chin and a dent in my forehead!

( "errrrrrr, DOH!" )

Hugh Jass.

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


[This message has been edited by Hugh Jass (edited 08-08-2000).]