There's no such thing as a stupid question, but they're the easiest to answer.
JoinTour
Login
 
Tag Cloud
access audio avg avg 8 bios blue screen boot bsod computer connection cpu crash css dell desktop dma driver drivers dvd email error excel explorer firefox firefox 3 freeze gimp graphics hard drive hardware hijackthis hjt install internet internet explorer itunes keyboard laptop macro malware monitor motherboard network networking outlook outlook 2003 outlook 2007 outlook express pio problem problems router seo server slow sound sp3 spyware trojan usb video virtumonde virus vista vundo windows windows vista windows xp winxp wireless
Web Design & Development
Search
Search in:
 
Advanced Search
Tech Support Guy Forums > Internet & Networking > Web Design & Development >
Javascript: How to run script with form?


HELLO AND WELCOME! Before you can post your question, you'll have to register -- it's completely free! Click here to join today! We highly recommend that you print a copy of our Guide for New Members. Enjoy!

 
Thread Tools
DrP's Avatar
DrP DrP is offline
Senior Member with 481 posts.
 
Join Date: Jul 2005
Location: UK
Experience: What's a compoota?
18-Aug-2005, 08:46 AM #1
Javascript: How to run script with form?
How do I get the 2nd script here to work? I am trying to take the variable "password" and check it == "go". If it does then I want to show this by diverting to another page.
Please, please, please help! This is giving me a headache!
DrP

<head>
<title>Teacher login</title>
<script type="text/javascript">
function validate(form)
{
var return_value = true;
var password = form.pwd_password.value;

if (password == "")
{
return_value = false;
alert("Enter password!");
form.pwd_password.focus;
}
}
</script>
<script type="javascript">
function check_password()
{
var login = false;
if (password == "go");
{
login = true;
location.href="xxxxx.html);
}
else
{alert("Password incorrect. Please try again.");
}
}
</script>
</head>
<body>
<form id="frm_login" onsubmit="return validate(this)" action="javascript:check_password()">
<table>
<tr>
<td><h3>Password:</h3></td>
<td><input type="password" id="pwd_password" size="20"/></td>
</tr><tr>
<td></td>
<td><input type="submit" value="Login"/></td>
</tr>
</table>
</form>
</body>
</html>
drdrew1469's Avatar
Senior Member with 437 posts.
 
Join Date: Nov 2000
Location: PA
Experience: God
18-Aug-2005, 12:00 PM #2
don't have time to play around with it right now, but combining them into one function would probably best since both check the same item. if not you can have your submit call both functions at the same time: "onsubmit="return validate(this);return check_password(this)" and leave your action for something else. good luck, i'll check back later.

hth,

drew
__________________
Please help me as I am attempting to (re)gain my (in)sanity.
DrP's Avatar
DrP DrP is offline
Senior Member with 481 posts.
 
Join Date: Jul 2005
Location: UK
Experience: What's a compoota?
18-Aug-2005, 01:38 PM #3
Drew, I have tried to combine into one script. Had some success...but only a a little. The script now reads:

<script type="text/javascript">
function validate(form)
{
var return_value = true;
var password = form.pwd_password.value;
var trupassword = "football";

if (password == "")
{
return_value = false;
alert("Enter password!");
form.pwd_password.focus;
}
else
{
if (password == 'trupassword');
{
window.open('class_pages.html','','width=250,height=140')
window.close('login.html')
}
}
return return_value
}
</script>

I'm not sure what the "return return_value" does, but it was on the script I took the first part of this script from.
The later part of the script is supposed to check whether the entered password is the same as the variable "trupassword". If it is then the new window will open. However, at the moment a new window will open whether password = trupassword or not! No idea why. I tried putting an else do this after it but that just rendered the whole script useless cos nothing happened. If the password doesn't match the trupassword then I'd like the user redirected to enter it again.
Thanks for the any help btw.

Last edited by DrP : 18-Aug-2005 01:36 PM. Reason: more info
drdrew1469's Avatar
Senior Member with 437 posts.
 
Join Date: Nov 2000
Location: PA
Experience: God
19-Aug-2005, 08:41 AM #4
This ought to do it, good job btw. A couple things, your quotes around 'trupassword' made that the actual pw, not your variable from above which was part of why it would open the window with any pw, now fixed...another reason was a needed 'else if' in the middle (if this do this, else if this do this, else do this) <---got that ...your 'return return_value' kinda threw me for a sec, but now you can see what it does/doesn't do with the working code in there. Enjoy.

Hope this is what you want...

drew


<script type="text/javascript">
function validate(form)
{
var return_value = true;
var password = form.pwd_password.value;
var trupassword = "football";

if (password == "")
{
return_value = false;
alert("Enter password!");
form.pwd_password.focus;
}
else if (password == trupassword)
{
window.open('class_pages.html','','width=250,height=140')
window.close('login.html')
}
else
{
return_value = false;
alert("Wrong password!");
form.pwd_password.focus;
}
}
</script>

<body>
<form id="frm_login" onsubmit="return validate(this)" action="">
<table>
<tr>
<td><h3>Password:</h3></td>
<td><input type="password" id="pwd_password" size="20"/></td>
</tr><tr>
<td></td>
<td><input type="submit" value="Login"/></td>
</tr>
</table>
</form>
</body>
__________________
Please help me as I am attempting to (re)gain my (in)sanity.
DrP's Avatar
DrP DrP is offline
Senior Member with 481 posts.
 
Join Date: Jul 2005
Location: UK
Experience: What's a compoota?
19-Aug-2005, 09:21 AM #5
Beauty!!! Thanks for that Drew. At last I've got somewhere! Unfortunately, I've a feeling that's the easy bit... I'm doing my best to learn this javascript but am finding it difficult, so thanks again for the help. Amazingly, it also works in Firefox and Netscape (you may remember supplying a code for a picture gallery. Works fine in IE but not in the other two. A link is below if you fancy it, but tbh I'm thinking of forgetting Firefox and Nescape until I have a bit more experience.)
Nice one
DrP

http://www.cliveportman.co.uk/clive_site/home.html

is now:
www.cliveportman.co.uk/gallery/gallery.html

Last edited by DrP : 20-Aug-2005 07:27 AM. Reason: changed link
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are Off
Refbacks are Off

You Are Using:
Server ID
Advertisements do not imply our endorsement of that product or service.
All times are GMT -4. The time now is 09:38 PM.
Copyright © 1996 - 2008 TechGuy, Inc. All rights reserved.
Powered by vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Powered by Cermak Technologies, Inc.