Forums

Validate user if already exist

raygold 04 Mar, 2011
How Can I validate fields like username and email to verify if they already exist in database?
I have already created a database connection for the form.
GreyHead 05 Mar, 2011
Hi raygold,

Is this to register users? The Joomla! Registration plug-in already includes the Joomla! code to check this?

Otherwise there are some serverside validation examples in this thread and an Ajax example in this extract from The ChronoForms Book.

Bob
raygold 05 Mar, 2011
The topic is very helpful,but I'd like to get two differents validation message.

I copy and past this code from bubila's topic:
<?php
$db =& JFactory::getDBO();
$cf = JRequest::getString('name', '', 'post');
$query = "
  SELECT COUNT(*)
    FROM `jos_chronoforms_basicDemo`
    WHERE `name` = '$cf'
";
$db->setQuery($query);
if ( $db->loadResult() ) {
  return "name already exist";
}
?>


<?php
$db =& JFactory::getDBO();
$cf = JRequest::getString('email', '', 'post');
$query = "
  SELECT COUNT(*)
    FROM `jos_chronoforms_basicDemo`
    WHERE `email` = '$cf'
";
$db->setQuery($query);
if ( $db->loadResult() ) {
  return "email already exist";
}
?>


Is the above code correct?
GreyHead 05 Mar, 2011
Hi raygold,

That code will work but it will return after the first error. If both inputs are wrong the second erorr won't be reported. Try this instead:
<?php
$db =& JFactory::getDBO();
$messages = array();
$name  = JRequest::getString('name', '', 'post');
$email = JRequest::getString('email', '', 'post');
$query = "
  SELECT COUNT(`name` = '$name') AS `name_count`, 
      COUNT(`email` = '$email') AS `email_count`, 
    FROM `#__chronoforms_basicDemo`
";
$db->setQuery($query);
$data = $db->loadAssoc();
if $data['name'] > 0 ) {
  $messages[] =  "name already exists";
}
if $data['email'] > 0 ) {
  $messages[] =  "email already exists";
}
if ( count($messages) ) {
  return implode('<br />', $messages);
}
?>
Not tested and may need debugging.

Bob
raygold 05 Mar, 2011
I get the following error message:
Parse error: syntax error, unexpected T_VARIABLE, expecting '(' in /components/com_chronocontact/libraries/chronoform.php(258) : eval()'d code on line 13

How can I solve this?
GreyHead 05 Mar, 2011
Hi raygold,

Put back the brackets '(' I left out of the 'if' lines
if ( $data['name'] > 0 ) {
  $messages[] =  "name already exists";
}
if ( $data['email'] > 0 ) {
  $messages[] =  "email already exists";
}

Bob
raygold 05 Mar, 2011
After I edit code,I no longer receiving error message but server validation don't work.😟
GreyHead 05 Mar, 2011
Hi raygod,

Please post all the code from the Server Validation box here so I can take a look.

Bob
raygold 05 Mar, 2011
This is the code:
<?php
$db =& JFactory::getDBO();
$messages = array();
$name  = JRequest::getString('name', '', 'post');
$email = JRequest::getString('email', '', 'post');
$query = "
  SELECT COUNT(`name` = '$name') AS `name_count`,
      COUNT(`email` = '$email') AS `email_count`,
    FROM `jos_chronoforms_basicDemo`
";
$db->setQuery($query);
$data = $db->loadAssoc();
if ( $data['name'] > 0 ) {
  $messages[] =  "name already exists";
}
if ( $data['email'] > 0 ) {
  $messages[] =  "email already exists";
}
if ( count($messages) ) {
  return implode('<br />', $messages);
}
?>


My form name is basicDemo and the field are name and email
GreyHead 05 Mar, 2011
Hi raygold,

Sorry I mismatched some names there.
if ( $data['name_count'] > 0 ) {
  $messages[] =  "name already exists";
}
if ( $data['email_count'] > 0 ) {
  $messages[] =  "email already exists";
}

Bob
raygold 06 Mar, 2011
Nothing to do,the validation still doesn't work. I'll settle to validate two fields with only one error message.

Thank you very much for you patience and your time.
GreyHead 06 Mar, 2011
Hi raygold,

My apologies, I gave you some code that just doesn't work even with the typos fixed. I needed to dig further into the MySQL manual and think through the logic.

Bob

For future readers here's a serverside validation snippet that is like raygold's but coded to work with the Joomla! user's table.
<?php
$db =& JFactory::getDBO();
$messages = array();
$select = array();
$name  = JRequest::getString('name', '', 'post');
if ( !$name ) {
  $messages[] = "Please enter a name";
} else {
  $select[] = "SUM(CASE WHEN `name` = '$name' THEN 1 ELSE 0 END) AS `name_count`";
}

$email = JRequest::getString('email', '', 'post');
if ( !$email ) {
  $messages[] = "Please enter an email";
} else {
  $select[] = "SUM(CASE WHEN `email` = '$email' THEN 1 ELSE 0 END) AS `email_count`";
}

if ( count($select) ) {
  $select = implode (', ', $select);
  $query = "
    SELECT $select
      FROM `#__users`
  ";

  $db->setQuery($query);
  $data = $db->loadObject();

  if ( $name && $data->name_count > 0 ) {
    $messages[] =  "This name is already in use";
  }
  if ( $email && $data->email_count > 0 ) {
    $messages[] =  "This email is already in use";
  }
}
if ( count($messages) ) {
  return implode('<br />', $messages);
}
?>
raygold 06 Mar, 2011
Thank you very much,code works perfectly.
How Can I remove validation for "Please enter an email" and "Please enter a name"?
For these I use live validation.
GreyHead 06 Mar, 2011
Hi raygold,

You can leave them -- if the LiveValidation is working they will never be triggered.

If you want to remove them then replace the two blocks like this
if ( !$name ) {
  $messages[] = "Please enter a name";
} else {
  $select[] = "SUM(CASE WHEN `name` = '$name' THEN 1 ELSE 0 END) AS `name_count`";
}
with
if ( $name ) {
  $select[] = "SUM(CASE WHEN `name` = '$name' THEN 1 ELSE 0 END) AS `name_count`";
}
changing 'name' for 'email' in the second.

Bob
raygold 06 Mar, 2011
Thank you very much,form validation now works perfectly🙂
This topic is locked and no more replies can be posted.