Forums

how to put a duplicate error?

donna22 20 Feb, 2009
how to put a duplicate error? if there is already a record in the database..for example i input a username and it was recorded in the databses, now if i input that username again, there must be an error prompt to the user.. how can i do this..?? please help....
GreyHead 20 Feb, 2009
Hi donna22,

This is a good case for an AJAX check to make sure that the username is unique. I think that someone posted an example here.

Or you can use ChronoForms server-side validation to do a quick database lookup.

Bob
donna22 22 Feb, 2009
how could i do it? to create a server side validation?

pls help..
GreyHead 22 Feb, 2009
Hi donna22,

Severside validation works after the form is submitted. In ChronoForms there is a box on the Validation tab where you can enter code to do a validation check. For this the code would be something like this:
<?php
$username =& JRequest::getString('uername', '', 'post');
if ( $username = '' ) {
  return "You must enter a username";
}
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(username)
    FROM `#__some_database_table`
    WHERE `username` = $username;";
$db->setQuery($query);
if ( $db->loadResult() > 0 ) {
  return "Username is already in use";
}
?>
Note: not tested and may need de-bugging!

Bob
donna22 23 Feb, 2009
great! ill try this one first then ill give you feedback with the result..
;)
donna22 24 Feb, 2009
Bob.,
it returns non error; i tried to debug it..tried to modify it, but still no prompt error and still send into the database,,,...
any help?
GreyHead 24 Feb, 2009
Hi donna22,

Please post the actual code you used here.

Bob
donna22 25 Feb, 2009
here is the code:
<?php
$username =& JRequest::getString('username', '', 'post');
if ( $username = '' ) {
  return "You must enter a username";
}
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(username)
    FROM `jos_chronoforms_uname`
    WHERE `username` = $username;";
$db->setQuery($query);
if ( $db->loadResult() > 0 ) {
  return "Username is already in use";
}
?>
GreyHead 25 Feb, 2009
Hi donna22,

I suspect that the query is failing and I think you may need quotes round the value. Please try this, if it doens't work we'll add some diagnostics.
<?php
$username =& JRequest::getString('username', '', 'post');
if ( $username = '' ) {
  return "You must enter a username";
}
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(username)
    FROM `jos_chronoforms_uname`
    WHERE `username` = ".$db->quote($username).";";
$db->setQuery($query);
if ( $db->loadResult() > 0 ) {
  return "Username is already in use";
}
?>

Bob
jarekpater 30 May, 2009
Hi. I'm just starting with ChronoForms and i have a question. How to re-write this code to check "text_2" from the table? If already exist it will put error.
Max_admin 01 Jun, 2009
Hi Jarek,

If you need to do the same function of the code above (username check) then there is a better way with the latest release but to do what you need here is a code sample:


$query = "
  SELECT text_2
    FROM `jos_chronoforms_uname`
    WHERE `username` = ".$db->quote($username).";";
$db->setQuery($query);
if ( $db->loadResult() > 0 ) {
  return "text_2 already exists";
}


Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
jarekpater 01 Jun, 2009
Thank you so much. But can you write this better way? In this I do something wrong. I have now v3.1 RC5 release of ChronoForms.
Max_admin 05 Jun, 2009
Hi Jarek,

Ok, first please get the official RC5.1

you need this code in the Joomla registration plugin after submit box:


<?php
$MyForm =& CFChronoForm::getInstance('form name');
$MyPlugins =& CFPlugins::getInstance($MyForm->formrow->id);
if($MyPlugins->cf_joomla_registration['errors']){
$MyForm->formerrors = $MyPlugins->cf_joomla_registration['errors'];
return;
}
?>


The code above will stop the form if there is an error and will show the error you got whatever it was!

Cheers
Max
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
This topic is locked and no more replies can be posted.