html - Javascript Password Submit - Form - Stack Overflow

I am trying to set a password to a form. I basically want someone to type a password into a password fi

I am trying to set a password to a form. I basically want someone to type a password into a password field and hit submit and if the password is right, it redirects them to buybutton.php in the same window. If the password isn't right, a javascript alert pops up that says 'Password wrong'. I've tried to write out the Javascript but no action is happening when the button is clicked. What have I done wrong in my code below? Thanks for the help!

<!DOCTYPE html>
<body width="950" height="300" style="background-image:url('giftcard_bg.jpg');">
<head>
<SCRIPT LANGUAGE="JavaScript">
    function goForit() {
      var passwd;
      passwd = this.document.giftForm.pass.value;
      if(passwd=="samsamsam"){
      window.open ('buybutton.php','_self',false)
      }
      else{
      alert('Password wrong');
      }
    }
</SCRIPT>
</head>


<div style="position:absolute; top:150px; left:285px; text-align:center;">
    <form id="giftForm">        
        <input type="password" id="pass" placeholder="Enter Secret Code" style="font-size:150%;"/>
        <br />
        <input type="button" onClick="goForit()" value="Submit" style="font-size:150%; margin-top:15px;" />                 
     </form>
</div>

</body>
</html>

I am trying to set a password to a form. I basically want someone to type a password into a password field and hit submit and if the password is right, it redirects them to buybutton.php in the same window. If the password isn't right, a javascript alert pops up that says 'Password wrong'. I've tried to write out the Javascript but no action is happening when the button is clicked. What have I done wrong in my code below? Thanks for the help!

<!DOCTYPE html>
<body width="950" height="300" style="background-image:url('giftcard_bg.jpg');">
<head>
<SCRIPT LANGUAGE="JavaScript">
    function goForit() {
      var passwd;
      passwd = this.document.giftForm.pass.value;
      if(passwd=="samsamsam"){
      window.open ('buybutton.php','_self',false)
      }
      else{
      alert('Password wrong');
      }
    }
</SCRIPT>
</head>


<div style="position:absolute; top:150px; left:285px; text-align:center;">
    <form id="giftForm">        
        <input type="password" id="pass" placeholder="Enter Secret Code" style="font-size:150%;"/>
        <br />
        <input type="button" onClick="goForit()" value="Submit" style="font-size:150%; margin-top:15px;" />                 
     </form>
</div>

</body>
</html>
Share Improve this question asked Aug 16, 2012 at 17:19 MillerMediaMillerMedia 3,66118 gold badges75 silver badges154 bronze badges 3
  • 9 Hope you're not depending on this for any real security. – j08691 Commented Aug 16, 2012 at 17:20
  • 2 Just a FYI, the HTML is invalid: it's <head></head> <body></body>. You shouldn't nest <head> inside <body>. – JJJ Commented Aug 16, 2012 at 17:28
  • Yeah @j08691, it's not necessarily a super secure thing, it's just a fun little promotion we're running so it's not really a huge deal if they see the password. Haha, this would definitely not be effective for that... – MillerMedia Commented Aug 16, 2012 at 19:40
Add a ment  | 

4 Answers 4

Reset to default 3

You should use document.getElementById('pass').value instead of this.document.giftForm.pass.value

function goForit() {
  var passwd;
  passwd = document.getElementById('pass').value;
  if(passwd=="samsamsam"){
  window.open ('buybutton.php','_self',false)
  }
  else{
  alert('Password wrong');
  }
}

Word of warning:

But be aware! Everybody can see the source code and find the password!!!

Instead of this, you should send the form to the server, and in the server side, check if it's correct. If it's correct, redirect the user to buybutton.php, but sending him a cookie which authorises him to enter to that page. And that page should check if he has that cookie.

But I'm not an expert, if you want that you should make another question asking that.

passwd = this.document.giftForm.pass.value; 

causes an error try

passwd = document.getElementById('pass').value;

You were missing a ; too after window.open ('buybutton.php','_self',false)

this should work

function goForit() {
      var passwd = document.getElementById("pass").value;
      if(passwd=="samsamsam"){
      window.open ('buybutton.php','_self',false)
      }
      else{
      alert('Password wrong');
      }
    }

Oriol and Rajeev's answer are correct.

Alternatively, you can do this:

change

passwd = this.document.giftForm.pass.value;

to

passwd = document.forms['giftForm'].pass.value;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744263155a4565735.html

相关推荐

  • html - Javascript Password Submit - Form - Stack Overflow

    I am trying to set a password to a form. I basically want someone to type a password into a password fi

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信