Hey Everyone.

Im using jquery's Validation plugin to check post values before I submit a form. Im trying to check a mysql table column to match user name to check for a duplicate name before submitting.
This snippet of code below works as expected..

<?php


$request = trim(strtolower($_REQUEST['login']));
//sleep(2);
usleep(150000);
$users = array('asdf', 'Peter', 'Peter2', 'George');
$valid = 'true';
foreach($users as $user) {
	if( strtolower($user) == $request )
		$valid = 'false';
}
echo $valid;
?>

Im just wondering how I could get mysql results into that array there.

Ive tried something like below..

$request = trim(strtolower($_REQUEST['login']));
//sleep(2);
usleep(150000);
$users = array( );
$query = mysql_query("SELECT login FROM members");
  while ($row = mysql_fetch_array($query)) 
     {  	
	$memberlogin = mysql_real_escape_string(trim($row['login']));
        $users[] = $memberlogin; 
	$valid = 'true';
        foreach($users as $user) 
         {
	   if( strtolower($user) == $request )
		$valid = 'false';
          }
echo $valid;								
	
     }

If someone can help thanks very much in advance.

I got it working, thanks guys. I guess this works well for anyone that needs.

So this is the "user.php" in the Milk demo for Jquery validation plugin..

users.php (original)

<?php

$request = trim(strtolower($_REQUEST['login']));
//sleep(2);
usleep(150000);
$users = array('asdf', 'Peter', 'Peter2', 'George');
$valid = 'true';
foreach($users as $user) {
	if( strtolower($user) == $request )
		$valid = 'false';
}
echo $valid;
?>

User.php (with mysql results being matched for duplicates)

<?php
$link = mysql_connect('localhost', 'username', 'password') or die('error');
@mysql_select_db('databasename',$link) or die('error');	
$valid = 'true';
$request = trim(strtolower($_REQUEST['login']));
//sleep(2);
usleep(150000);
$users = array( );
$query = mysql_query("SELECT login FROM members");
  while ($row = mysql_fetch_array($query)) 
     {  	
	$memberlogin = mysql_real_escape_string(trim($row['login']));
        $users[] = $memberlogin; 
	
        foreach($users as $user) 
         {
	   if( strtolower($user) == $request )
		$valid = 'false';
          }
							
	
     }echo $valid;	
?>

Sorry to bump this. What I did was kind of silly, below works fine.

<?php
include("../dbcon.php");
$valid = 'true';
$request = trim(strtolower($_REQUEST['user']));
usleep(150000);

$query = mysql_query("SELECT * FROM members WHERE user=\"$request\"");
$count = mysql_num_rows($query);
       if($count == 1)  {
        $valid = 'false';
          }
         echo $valid;
?>

jquery to run remote file and call back

$(document).ready(function() {
          $("#form").validate({

                rules: {
                user: {
                    required: true,
                    minlength: 2,
                    remote: "users.php"
                },
            },
                messages: {
                user: {
                    required: "Enter a username",
                    minlength: jQuery.format("Enter at least {0} characters"),
                    remote: jQuery.format("{0} is already in use")
                },

            },

          });
        });
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.