Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'username availability'.

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 1 result

  1. Okay so I just finished reading the Javascript book (including Ch 11) and I am pushing forward trying to get AJAX to work on my prototype website for the first time. Specifically I want to have my registration page use AJAX to notify the user of whether their desired username is available in our system or not. I am familiar with HTML / CSS so I don't think the issue is in there, and am familiar enough with PHP and MySQL but not necessarily how PHP interacts with Javascript via AJAX. Here is my code, emphasis on the Javascript portion (and PHP portion) because those are the parts that I am most unfamiliar with: <tr> <td class="form_left"><label for="username">Username:</label></td> <td class="form_right"><input type="text" id="username" name="username" size="25" maxlength="25" value="<?php if (isset($_POST['username'])) {echo $_POST['username'];} ?>" onkeyup="verifyUsername()" /><span id="usernameSpan" style="color:red;">*</span></td> </tr> ... <script src="/js/register.js"></script> <script src="/js/ajax.js"></script> Here is my PHP code: require_once ($_SERVER["DOCUMENT_ROOT"] . '/includes/config.inc.php');[/background][/size][/font][/color] [color=#000000][font=verdana, geneva, lucida, 'lucida grande', arial, helvetica, sans-serif][size=3][background=rgb(245, 245, 245)]// if (isset ($_POST['enteredUsername'])) { $submitted_username = $_POST['enteredUsername']; require_once (MYSQL); // Connect to the db. } $check_username_query = "SELECT username FROM users WHERE username = $submitted_username"; $r = mysqli_query ($dbc, $check_username_query) ; if (mysqli_num_rows($r) == 0) { echo 'VALID'; } else { echo 'INVALID'; } And here is my Javascript code: function verifyUsername() { 'use strict'; //Get a reference (get element by ID) to the entered username value: var ajax = getXMLHttpRequestObject(); var enteredUsername = U.$('username'); var usernameSpan = U.$('usernameSpan'); // Validate the first name: if (/[a-zA-Z0-9._-]{1,25}$/i.test(username.value)) { //tested true in console if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = ""; } else {usernameSpan.innerText = "";} //Begin the AJAX request ajax.onreadystatechange = function() { if (ajax.readyState == 4){ //Check the status code: if ( (ajax.status >= 200 && ajax.status < 300) || (ajax.status == 304) ) { if (ajax.responseText == 'VALID') { if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username available"; } else {usernameSpan.innerText = "Username available";} } else { if (ajax.responseText == 'INVALID'){ if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username not available"; } else {usernameSpan.innerText = "Username not available";} } } } } }; ajax.open('POST', 'resources/verifyusername.php', true); ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); var data = 'enteredUsername=' + encodeURIComponent(username.value); ajax.send(data); } else { if (usernameSpan.textContent !== undefined) { usernameSpan.textContent = "Username must be 25 characters or less, contain only \n\ numbers, letters, '.', '_', and '-'. "; } else {usernameSpan.innerText = "Username must be 25 characters or less, contain only \n\ numbers, letters, '.', '_', and '-'."; } } } //End of verifyUsername() function As of right now when I enter a username it changes the asterisk to a blank spot. This means that my PERL regular expression is at least passing. If I can at least narrow down the error to whether it's the PHP or Javascript code causing the issue then that will make it much easier to eventually find and fix the problem. Any help would be greatly appreciated! Once I get this AJAX thing down the first time it will make all subsequent attempts much easier because I will have a template to base my attempts off of. I have been dabbling in the Developer's Console in Chrome which at least shows verifyusername.php being initiated by register.js but not much more info.
×
×
  • Create New...