SitePoint Sponsor |
|
User Tag List
Results 1 to 9 of 9
-
Jul 1, 2008, 10:40 #1
- Join Date
- Dec 2007
- Location
- Macedonia
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Quick and dirty javascript login. Please!
Hy friends!
I will be grateful if someone out there knows how I can code a simple javascript login functionality, with redirect to page depending on the user! I will have 2 users:
Buyer - username: Buyer; password: buyer;
Seller - username Seller; password: seller;
After login the buyer should be redirected to buyer.html, and seller to seller.html
I got this form:
Code:<form id="loginform" action=""> <fieldset> <div> <label for="usrname">Username:</label> <input name="usrname" type="text" size="20" /> </div> <div class="pass_field"> <label for="pswrd">Password:</label> <input name="pswrd" type="text" size="20" /> <input name="login" class="login_btn" type="image" src="images/home_login_btn.png" /> </div> </fieldset> </form>
Thank you and thank you!
Jimmy
-
Jul 1, 2008, 12:12 #2
- Join Date
- Jun 2008
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
here you go:
I cannot post links so you need to fix the google and yahoo links to test it.
<script type="text/javascript">
function login() {
var usrname = document.getElementById('usrname');
var pswrd = document.getElementById('pswrd');
if(usrname.value == 'buyer' && pswrd.value == 'buyer') {
window.location.href = 'google.com';
}
if(usrname.value == 'seller' && pswrd.value == 'seller') {
window.location.href = 'yahoo.com';
}
}
</script>
<form id="loginform" action="javascript:login()">
<fieldset> <div> <label for="usrname">Username/label> <input name="usrname" type="text" size="20" /> </div>
<div class="pass_field"> <label for="pswrd">Password/label> <input name="pswrd" type="text" size="20" />
<input name="login" class="login_btn" type="image" src="images/home_login_btn.png" /> </div> </fieldset> </form>
-
Jul 1, 2008, 12:28 #3
- Join Date
- Dec 2007
- Location
- Macedonia
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Thanks for your help! I really appreciate you taking time to answer my question!
But, why the heck it's not working on my side?Did you test it already?
The submit button doesn't even execute the form.. It's like the form doesn't have an action attached to it, but sure I placed action="javascript:login()"..
Any ideas what could be wrong ?
Thank you!
-
Jul 1, 2008, 12:38 #4
- Join Date
- Jun 2008
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
It worked on IE, firefox does NOT support name, so I changed name to id:
<script type="text/javascript">
window.onload = function() {
alert('java script enabled');
}
function login() {
var usrname = document.getElementById('usrname');
var pswrd = document.getElementById('pswrd');
if(usrname.value == 'Buyer' && pswrd.value == 'buyer') {
window.location.href = 'google.com';
}
if(usrname.value == 'Seller' && pswrd.value == 'seller') {
window.location.href = 'yahoo.com/';
}
}
</script>
<form id="loginform" action="javascript:login()">
<fieldset> <div> <label for="usrname">Username/label> <input id="usrname" type="text" size="20" /> </div>
<div class="pass_field"> <label for="pswrd">Password/label> <input id="pswrd" type="text" size="20" />
<input name="login" class="login_btn" type="image" src="images/home_login_btn.png" /> </div> </fieldset> </form>
-
Jul 1, 2008, 12:45 #5
-
Jul 1, 2008, 12:57 #6
- Join Date
- Sep 2005
- Location
- Sydney, NSW, Australia
- Posts
- 16,875
- Mentioned
- 25 Post(s)
- Tagged
- 1 Thread(s)
You should also replace action="javascript:login()" with onsubmit="login()"
Stephen J Chapman
javascriptexample.net, Book Reviews, follow me on Twitter
HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
<input name="html5" type="text" required pattern="^$">
-
Jul 1, 2008, 13:01 #7
-
Jul 1, 2008, 13:04 #8
- Join Date
- Dec 2007
- Location
- Macedonia
- Posts
- 40
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
Nope, "onsubmit.." doesn't work..
-
Jul 1, 2008, 13:39 #9
- Join Date
- Jun 2008
- Posts
- 6
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
you button is not a submit button, you need to change that too.
Bookmarks