Forums

How to check if a number is already loaded at DB

kounosuke 08 Feb, 2012
Hi!
I'm trying to use Custom Server Side Validation to check a textbox input against a ChronoForm DB record to check if the number provided by user is already at DB, to limit user to send form only once. This number is always unique to a person in my country and we call it DNI, and it is something like the Social Security Number in USA.

I named DB 'mbod_concurso20120101', and DNI textbox field name is 'dni'. Model ID, which I don't know what it is but I think you might need it, is 'chronoform_data'. Joomla es 1.5.x and ChronoForm is V4 RC3.11.

I was trying some modified code I found in this forum, but it is not working, it always return false. I'm using Form Wizard in Extended Mode.

I attach a capture of Events tab: the idea behind all this is, after user submit form, check DB to see if DNI is already at DB records. If DNI is new then go to OnSuccess, send OK message and Save to DB new record. If DNI is already in DB then go to OnFail and send Error message.


<?php
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(*) 
    FROM `mbod_concurso20120101`
    WHERE `dni` = '{$form->data['dni']}' ;
";
$db->setQuery($query);
$resultado = $db->loadResult();
if ( $resultado ) {
  return true;
}
else {
  return false;
}
?>


I know I must be doing something silly, as my knowledges of PHP and SQL are tremendously limited.

Thanks in advance for your help!
kounosuke 08 Feb, 2012
Hi!

I make it work!

I copy the code that's working, so you can tell if there's something not very right, or that can be improved.

Thanks anyway! 🙂


<?php
$db =& JFactory::getDBO();
$query = "
  SELECT `dni`
    FROM `mbod_concurso20120101`
    WHERE `dni` = '{$form->data['dni']}' ;
";
$db->setQuery($query);
$resultado = $db->loadResult();
if ( $resultado ) {
  return false;
}
else {
  return true;
}
?>
GreyHead 10 Feb, 2012
Hi kounosuke,

It looks Ok to me. The only change I would make is to add an error message
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT `dni`
    FROM `mbod_concurso20120101`
    WHERE `dni` = '{$form->data['dni']}' ;
";
$db->setQuery($query);
$resultado = $db->loadResult();
if ( $resultado ) {
  $form->validation_errors['dni'] = "This DNI is already registered.";.
  return false;
}
?>
I also took out the 'return true;' as it is assumed.

Bob

PS This is also a good candidate for Ajax validation form the form itself.
This topic is locked and no more replies can be posted.