www.webdeveloper.com
Results 1 to 5 of 5

Thread: JavaScript not recognizing ID

Hybrid View

  1. #1
    Join Date
    Sep 2010
    Posts
    8

    Question JavaScript not recognizing ID

    Hi,

    I've got a strange issue on my hands here. I'm trying to get AJAX form validation working for a user registration form for a project I'm working on. I have it working in all fields except the username field.

    Here's the JS code that it calls from the form.

    Code:
    if (id == "username") {
                    var unInp = document.getElementById('username').value;
                    if (unInp.length > 4) {
                        // send input to validator file
                        $.get("../includes/validator.php", { check: "true", type: "username", value: unInp },
                                function(data) {
                                    if (data == "0") {
                                        document.getElementById(id + 'IMG').src = "images/tick.png";
                                        $('#form_hidden').fadeIn(400);
                                    } else {
                                        document.getElementById(id + 'IMG').src = "images/cross.png";
                                    }
                                });
    
                    } else {
                        document.getElementById(id + 'IMG').src = "images/cross.png";
                    }
                }
    It seemingly skips over the "if (id == "username") {" and automatically displays the 'else' which is the cross.png meaning it's wrong.

    Here's the code from the page where the form is:

    Code:
    <form method='post' action='register.php' id='wcwlogin' class='dropout'>
    
                    	<label class='login' for='username'>"?><? echo outputLan('regPage','username'); ?></label>
                    	<input type="text" autocomplete="off" name="username" id="username" class="input_field" size="40"
                            onkeyup="checkInput('username');" value="<? echo $username;?>" /> <? echo "<img id='usernameIMG'
                                                                                                   src='images/loader.gif'
                                                                                                   alt='output'
                                                                                                   class='hidden'>
    
                    <div id='form_hidden'>
                        <label class='login' for='firstname'>"?><? echo outputLan('regPage','first name');?><? echo "</label> 
                        <input type='text' autocomplete='off' name='firstname' id='firstname' class='input_field' size='47'
                               "?> onkeyup="checkInput('firstname');" value="<? echo $fname;?>"><? echo "<img id='firstnameIMG'
                                                                                                     src='images/loader.gif'
                                                                                                     alt='output'
                                                                                                     class='hidden'>
    
                        <label class='login' for='lastname'>"?><? echo outputLan('regPage','last name');?><? echo "</label>
                        <input type='text' autocomplete='off' name='lastname' id='lastname' class='input_field' size='47'
                               "?> onkeyup="checkInput('lastname');" value="<? echo $lname;?>"> <? echo "<img id='lastnameIMG'
                                                                                                    src='images/loader.gif'
                                                                                                    alt='output'
                                                                                                    class='hidden'>
    
                        <label class='login' for='email'>"?><? echo outputLan('regPage','email');?><? echo "</label>
                        <input type='text' autocomplete='off' name='email' id='email' class='input_field' size='47'
                              "?> onkeyup="checkInput('email');" value="<? echo $email;?>" <? echo "<img id='emailIMG'
                                                                                                 src='images/loader.gif'
                                                                                                 alt='output'
                                                                                                 class='hidden'>
                        <label class='login' for='password'>"?><? echo outputLan('regPage','password');?><? echo "</label>
                        <input type='password' autocomplete='off' name='password' id='password' class='input_field' size='47'
                               "?> onkeyup="checkInput('password');" value="<? echo $pass;?>" <? echo "<img id='passwordIMG'
                                                                                                   src='images/loader.gif'
                                                                                                   alt='output'
                                                                                                   class='hidden'>
                        <label class='login' for='password2'>"?><? echo outputLan('regPage','password2');?><? echo "</label>
                        <input type='password' autocomplete='off' name='password2' id='password2' class='input_field' size='47'
                               "?> onkeyup="checkInput('password2');" value="<? echo $pass2;?>"<? echo "><img id='password2IMG'
                                                                                                     src='images/loader.gif'
                                                                                                     alt='output'
                                                                                                     class='hidden'>"?>
    
                        <label class='login' for='gender'><? echo outputLan('regPage','gender');?><? echo "</label>
                        <select name='gender' style='padding:25px;' id='gender' class='input_field' style='height:30px;'>
                            "?> <option value="select" <? echo "$dSel;"?>><? echo "Select</option>
                            "?><option value="male" <? echo "$mSel;"?>><? echo outputLan('regPage','male');?></option>
                            <option value="female" <? echo "$fSel;"?>><? echo outputLan('regPage','female');?></option>
                        </select>
    		
    		<label class='login' for='contact'><? echo outputLan('regPage','contact');?><? echo "</label>
    		<select name='contact' id='contact' class='input_field' style='height:30px;'>
    			<option value='yes'>Yes</option>
    			<option value='no'>No</option>
    		</select>
    
                        "?><input type='hidden' name='cap1' id='cap1' value="<? echo $cap1; ?>">
                           <input type='hidden' name='cap2' id='cap2' value="<? echo $cap2;?>"> <? echo "
    
                        <label class='login' for='captcha'> "?><? echo outputLan('regPage','captcha'); echo $capString;?><? echo "</label>
                        <input type='text' autocomplete='off' name='captcha' id='captcha' class='input_field' size='10'
                               "?> onkeyup="checkInput('captcha');"<? echo "><img id='captchaIMG' src='images/loader.gif' alt='output'
                                                                     class='hidden'>
                        <br/>
                        <input type='submit' name='register' id='subBut' "?> value="<? echo outputLan('regPage','register button');?>"<? echo "class='submit'>
                    </div>
                    <!-- end of hidden form element -->
                </form>
        </div> </div>";
    }
    
    ?>
    That is only part of the code on the page....that's just the form.

    The weird thing is that every other form element is working and validating itself properly.....even the email field which has a very similar validation process when compared to the username field. Both have to query the database to see if the value is already present. But the username field doesn't seem to even be getting that far.

    Any help would be greatly appreciated. If you need more of the code, please let me know.

    Thank you!!

  2. #2
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    It looks like a PHP mistype. You forgot to close the <label> element (or to echo the following elements), thus you might get a HTML mismatch there:
    Code:
    <label class='login' for='username'>"?><? echo outputLan('regPage','username'); ?><? echo " </label>

  3. #3
    Join Date
    Sep 2010
    Posts
    8
    Thanks for the reply!

    I echoed the </label> command like this:

    Code:
    <label class='login' for='username'>"?><? echo outputLan('regPage','username'); ?><? echo "</label>
                    	<input type='text' autocomplete='off' name='username' id='username' class='input_field' size='40'
                            "?> onkeyup="checkInput('username');" value="<? echo $username;?>" /> <? echo "<img id='usernameIMG'
                                                                                                   src='images/loader.gif'
                                                                                                   alt='output'
                                                                                                   class='hidden'>
    Is that how you would have done it? It's still not working unfortunately.

    I'm not surprised that it's a PHP mistype though.....it just isn't echoing the ID it seems.

    Thanks again for the reply! I really appreciate it.

  4. #4
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Give it a View Source and see how the HTML written code looks like, not the php code...

  5. #5
    Join Date
    Sep 2010
    Posts
    8
    I just did a view source and here's what I got. The <label> tag is being closed.

    Code:
    <label class='login' for='username'>Username <span class="low_emph">(at least 5 characters)</span></label> 
                    	<input type='text' autocomplete='off' name='username' id='username' class='input_field' size='40'
                             onkeyup="checkInput('username');" value="" /> <img id='usernameIMG'
                                                                                                   src='images/loader.gif'
                                                                                                   alt='output'
                                                                                                   class='hidden'>

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles