Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    May 2010
    Posts
    23
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Simple Password Prompt

    I am fairly new to Javascript. From what I can compare to sample codes this should work. I have a button with an onclick that opens a prompt. Compares whats written to a php SESSION variable. If true redirects, if false displays an alert.

    However, all of this means nothing because when I press the button, no prompt comes up! Heres the code

    Code:
    <head>
    		<script type="text/javascript">
    
    			function passPrompt()
    			{
    			var pass=prompt("Please enter your password:","")
    			if pass = <?php $pwd ?>
    				header('Location: setup.php');
    			else 
    				alert("Password is incorrect");
    			}
    		</script>
    </head>
    
    ...
    
    <input id="settings" type="button" name="settings" value="Settings" onclick="passPrompt()" />

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Tip: install Firebug and reload your page. You'll see this error: [EDIT: I should say "something like this error"... that's what I got on mine without setting the variable.]
    missing ( before condition
    if pass = header('Location: setup.php');\n
    Hint: PHP functions aren't Javascript functions. And conditions must be wrapped in parentheses.
    Last edited by tomws; 06-09-2010 at 07:24 PM.
    Are you a Help Vampire?

  • #3
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Oh, and assignment (=) is not the same as testing for equality (==).
    Are you a Help Vampire?

  • #4
    New Coder
    Join Date
    Jun 2010
    Posts
    15
    Thanks
    0
    Thanked 6 Times in 6 Posts
    You have several syntax errors in your script. You're missing parentheses, quotes, two equals signs instead of one, and technically, the php needs to be altered as well (you have to actually echo the contents of the $pwd variable). Several ways to do all of this, but one is:

    Code:
    if (pass == '<?php=$pwd?>')
    Also, I'm not sure what you're trying to secure, but just be aware that since you're printing the password in plain text in the HTML, that it would be pretty easy for anyone to get the password by viewing source.

    Also, if $pwd is empty, then leaving the box empty and submitting will result in the redirect.

    Quote Originally Posted by swieder View Post
    I am fairly new to Javascript. From what I can compare to sample codes this should work. I have a button with an onclick that opens a prompt. Compares whats written to a php SESSION variable. If true redirects, if false displays an alert.

    However, all of this means nothing because when I press the button, no prompt comes up! Heres the code

    Code:
    <head>
    		<script type="text/javascript">
    
    			function passPrompt()
    			{
    			var pass=prompt("Please enter your password:","")
    			if pass = <?php $pwd ?>
    				header('Location: setup.php');
    			else 
    				alert("Password is incorrect");
    			}
    		</script>
    </head>
    
    ...
    
    <input id="settings" type="button" name="settings" value="Settings" onclick="passPrompt()" />


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •