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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Dec 2010
    Location
    California
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Not sure how to get this to work...

    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:
    Code:
    <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:
    Code:
    <?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?

  • #2
    New Coder
    Join Date
    Dec 2010
    Posts
    36
    Thanks
    4
    Thanked 0 Times in 0 Posts
    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.

  • #3
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    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.

  • #4
    New to the CF scene
    Join Date
    Dec 2010
    Location
    California
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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?

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Quote Originally Posted by xdarkinsidex View Post
    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.

  • Users who have thanked Philip M for this post:

    xdarkinsidex (12-07-2010)

  • #6
    New to the CF scene
    Join Date
    Dec 2010
    Location
    California
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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:
    Code:
     <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 Code:
    <?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.
    Last edited by xdarkinsidex; 12-07-2010 at 02:11 AM. Reason: fixed problem, didnt want to make a new post


  •  

    Posting Permissions

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