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.
I have already created a database connection for the form.
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
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
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:
Is the above code correct?
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?
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:
Bob
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
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?
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?
Hi raygold,
Put back the brackets '(' I left out of the 'if' lines
Bob
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
After I edit code,I no longer receiving error message but server validation don't work.😟
Hi raygod,
Please post all the code from the Server Validation box here so I can take a look.
Bob
Please post all the code from the Server Validation box here so I can take a look.
Bob
This is the code:
My form name is basicDemo and the field are name and email
<?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
Hi raygold,
Sorry I mismatched some names there.
Bob
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
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.
Thank you very much for you patience and your time.
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.
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);
}
?>
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.
How Can I remove validation for "Please enter an email" and "Please enter a name"?
For these I use live validation.
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
Bob
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`";
}
withif ( $name ) {
$select[] = "SUM(CASE WHEN `name` = '$name' THEN 1 ELSE 0 END) AS `name_count`";
}
changing 'name' for 'email' in the second.Bob
This topic is locked and no more replies can be posted.