...

View Full Version : Simple Password Prompt



swieder
06-09-2010, 05:57 PM
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



<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()" />

tomws
06-09-2010, 07:22 PM
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.

tomws
06-09-2010, 07:26 PM
Oh, and assignment (=) is not the same as testing for equality (==).

mjbeaumont
06-09-2010, 07:42 PM
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:



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.


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



<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()" />



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum