0

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.

Edited by reco21: n/a

1
Contributor
2
Replies
4
Views
5 Years
Discussion Span
Last Post by reco21
0

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;	
?>
0

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")
                },

            },

          });
        });

Edited by reco21

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.