...

View Full Version : Not sure how to get this to work...



xdarkinsidex
12-06-2010, 01:01 AM
Hi there! I'm completely new to javascript coding, but have suddenly found myself in need of using it. here's a little background (helps me keep track of where I am in my question).

I've been "playing" with php for quite a while, and I decided I wanted to see how hard it would be to code a fairly simple forum. So far, everything is working out with very little need for bug fixes and some efficiency suggestins from a friend.

I'm currently building the registration page, and wanted to have it compare the password and verification while the form is being filled. I found a code to do it after a few searches, but evidently I am missing something about how it works.

This is the code I found:

<script type='text/javascript'>
function comparePasswords()
{
if(document.getElementById('pass1').value != document.getElementById('pass2').value)
{
$('passwordShow').innerHTML = 'Passwords do not match';
}
else
{
$('passwordShow').innerHTML = '';
}
}
</script>

This is the full code for the form I am trying to use it in:

<?php
echo "<script type='text/javascript'>\n function comparePasswords()\n {\n if(document.getElementById";
echo "('pass1').value != document.getElementById('pass2').value)\n {\n $('passwordShow').innerHTML";
echo " = 'Passwords do not match';\n }\n else\n {\n $('passwordShow').innerHTML = '';\n";
echo " }\n }\n</script> ";

echo "<form action='index.php?pg=verify' method='post'>";
echo "<table align='center' style='color: #C0C0C0'>";
echo "<tr><td>Username:</td><td><input type='text' name='user' /></td></tr>";
echo "<tr><td>Password:</td><td><input type='password' name='pass1' /></td></tr>";
echo "<tr><td>Re-enter Password</td><td><input type='password' name='pass2' onkeyup='comparePasswords();' /></td></tr>";
echo "<tr><td>Email<br />(used for password recovery)</td><td><input type='text' name='email' /></td></tr>";
require ('questions.php');
echo "<input type='hidden' name='source' value='reg' />";
echo "<br><center><input type='submit'/></form>";
?>

What am I not understanding about this?

david56connor
12-06-2010, 01:14 AM
I'm also very new to javascript however I do notice that there are functions in there that require the jQuery library from what I have learned anyway.. Maybe when someone with a little more knowledge is available they can help you further.

Philip M
12-06-2010, 11:02 AM
You have assigned names to pass1 and pass2 but not ids.

echo "<tr><td>Password:</td><td><input type='password' name='pass1' id= 'pass1' /></td></tr>";


All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

xdarkinsidex
12-06-2010, 06:33 PM
I added in the id's, but it still isn't doing anything...do I need this jQuery thing, or is there a way of doing this that won't require me to ask my host to install new stuff?

Philip M
12-06-2010, 07:10 PM
I added in the id's, but it still isn't doing anything...do I need this jQuery thing, or is there a way of doing this that won't require me to ask my host to install new stuff?

No idea what is wrong. Use some alerts to trace the progress of the code. But another error is

onkeyup='comparePasswords()';

That is bound to throw an error while the password is being typed.

xdarkinsidex
12-07-2010, 12:50 AM
I changed it to onblur based on what a few tutorials said...

I would really prefer that the check be done before the form is submitted because of how annoying it is to have to go back and fix something you couldn't see while inputing it.

edit: I managed to get it working after re-reading one of the tutorial sites, merging code examples, putting the code on a seperate html page, and using php require() to include it in the code.

So now my code looks like this (just in case someone else is tring to figgure out the same problem):

function.html:

<script language="JavaScript">
<!--
function checkPass()
{
var guess = document.reg.pass1.value;
var secret = document.reg.pass2.value;
if (guess != secret)
{
alert('The passwords do not match.');
}
}
-->
</script>

register.php:

<?php
require('function.html');

echo "<form name='reg' action='index.php?pg=verify' method='post'>";
echo "<table align='center' style='color: #C0C0C0'>";
echo "<tr><td>Username:</td><td><input type='text' name='user' /></td></tr>";
echo "<tr><td>Password:</td><td><input type='password' name='pass1' /></td></tr>";
echo "<tr><td>Re-enter Password</td><td><input type='password' name='pass2' onblur='checkPass()' /></td></tr>";
echo "<tr><td>Email<br />(used for password recovery)</td><td><input type='text' name='email' /></td></tr>";
require ('questions.php');
echo "<input type='hidden' name='source' value='reg' />";
echo "<br><center><input type='submit'/></form>";
?>

Thanks for the help, though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum