Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Login redirect 2

Status
Not open for further replies.

dugen

Programmer
Jun 16, 2003
59
US
i have created a login section on my site. I have 9 different usernames and passwords and each one needs to go to a different URL. With the code below i could only direct a user to one URL. Is there any way I could have text2 and text1 have multiple values that each direct a username and password to the correct URL?



<form>
<p>ENTER USER NAME :
<input type="text" name="text2">
</p>
<p> ENTER PASSWORD :
<input type="password" name="text1">
<input type="button" value="Check In" name="Submit" onclick=javascript:validate(text2.value,"building9",text1.value,"life") >
</p>

</form>
<script language = "javascript">


function validate(text1,text2,text3,text4)
{
if (text1==text2 && text3==text4)
load('success.htm');
else
{
load('failure.htm');
}
}
function load(url)
{
location.href=url;
}
</script>

<p align="center"><font face="arial" size="-2">This free script provided by <a href="
 
Here is some code I had that does exactly what you want... I modified it to work like your code did (using the load() function).

This is the modified javascript block you would use:
Code:
<script type="text/javascript">
var loginArray = [];
loginArray[loginArray.length] = {username:'jeff',password:'pass1'};
loginArray[loginArray.length] = {username:'james',password:'pass2'};
loginArray[loginArray.length] = {username:'susan',password:'pass3'};

function validate() {
	var myFrm = document.forms[0];
	var success = false;
	var username = myFrm.elements['username'].value;
	var password = myFrm.elements['password'].value;
	for (var loop=0, max=loginArray.length; loop<max; loop++) {
		if (loginArray[loop].username == username && loginArray[loop].password == password) {
			success = true;
			break;
		}
	}
	if (success) {
		load('failure.htm');
	} else {
		load('failure.htm');
	}
}

function load(url) {
	location.href = url;
}
</script>
And the modified HTML a little too...
Code:
<form action="" onsubmit="return(validate());">
<fieldset>
	<legend>Enter your login details below</legend>
	<p>ENTER USER NAME : 
		<input type="text" name="username" id="username">
	</p>
	<p>ENTER PASSWORD : 
		<input type="password" name="password" id="password">
		<input type="submit" value="Check In" name="Submit">
	</p>
</fieldset>
</form>
Cheers,
Jeff

[tt]Jeff's Page @ Code Couch
[/tt]

What is Javascript? FAQ216-6094
 
Do the validation server side. That's the only way to keep someone from looking at the page and entering the correct code for where ever they want to go. Then you can redirect to whatever page you've chosen for the individual user.

Lee
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top