Forums

Restrict Duplicate Entries based on field

tcmark 19 May, 2010
Hi, I have a question regarding the capabilities of ChronoForms.

I'm wondering if it's possible to use a particular field to restrict form submissions.
Bearing in mind that this will be for any user, not just registered users.

So (for example), let's say I have a "name" textbox. I only want each "name" to be able to submit the form once. (I'm aware of the potential problems of only checking on a "name" field...just want to get this working before I go further)

Is there a way of checking the database for a matching "name" entry, and halting any further progress if so. (e.g. Not entering the details into a DB table, not sending any emails out).

I have seen similar answers on this board, but only to do with registered users. I have tried to modify that code (currently in the On Submit code - before sending email: field).
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(*) 
    FROM `#__chronoforms_Testing`
WHERE `name` like '".$_POST['name']."'; 
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
  echo "Sorry, no duplicate entries.";
 return;
}
else{
?>
<p>
Thanks for your entry!</p>
<?php 
}
?>



So what happens currently is that the form works fine when entering new names.
When entering a name that already exists, the "sorry no duplicate entries" message comes up correctly.
However, the details are stilled added to the connected DB table, and the relevant emails are still sent out.

If anyone can help, it'd be much appreciated.

Thanks
tcmark 19 May, 2010
Ok, I feel stupid now...

I just realised I can replace the error message with a
global $mainframe;
$mainframe->redirect('somePageHere');

and it'll do exactly what I want 😶

Oh well, I'll leave it here incase anyone else is after the same thing.

Full Code:
<?php
$db =& JFactory::getDBO();
$query = "
  SELECT COUNT(*) 
    FROM `#__chronoforms_Testing`
WHERE `name` like '".$_POST['name']."';
";
$db->setQuery($query);
$count = $db->loadResult();
if ( $count ) {
global $mainframe;
$mainframe->redirect('somePageHere');
 return;
}
else{
?>
<p>
Thanks for your submission!</p>

<?php 
}
?>
GreyHead 19 May, 2010
Hi tcmark,

You can't do this in the Form HTML as you do with registered users but you can do it in the serverside validation; or by using Ajax in the form itself.

Using serverside validation rather than the OnSubmit box has the advantage that you will automatically stop any further processing.

Bob
daffy 22 May, 2010
Thanks Bob and tcmark. I was just about to make a question along these lines and I thought I should do a 'search' first and here we are.
But I had in mind to put something in the 'validate' section 'on blur' so that as soon as a duplicate name was entered an error message would result.
I have a chronoform 'groupnames' which asks for an identifying name for up to ten individuals.
And of course duplicates would be useless.
You are much cleverer persons than I so I wonder if there is anything on mootools which would help?
--
dave
GreyHead 23 May, 2010
Hi Dave,

I'm sure that you could do it with a custom script. But there is nothing built-in to ChronoForms or to LiveValidation that will do this for you.

Bob
dustdog 04 Jul, 2011
Hey tcmark,
Thanks for posting this script. Just what I needed on short notice! 🙂

Cheers,
Dustdog
This topic is locked and no more replies can be posted.