Anyone had better luck with this PHP by FormsCity for creating a Duplicate Entry?

By: Matt Clute | Asked: 06/12/2022
ForumsCategory: Code SnippetsAnyone had better luck with this PHP by FormsCity for creating a Duplicate Entry?
Matt CluteMatt Clute asked 2 years ago I tried using a code snippet from FormsCity that sounded like a perfect solution. (read the description below)... If your users are creating many form entries with similar field values in your Formidable Forms, they won’t like to type the same values again and again. In this instance, it is a good user experience to provide a duplicate entry button. Formidable doesn’t provide this out of the box. However, they have shared a custom code snippet on their website While this works, it is not a perfect solution. What this code snippet does is, upon clicking the “Duplicate” link, it creates a duplicate entry in the database and then shows the newly created entry in edit mode in the form. User can make changes in the new entry and save it again. The issue with this approach is it doesn’t support the case where the user clicks on the “Duplicate” link and then decided not to create the new entry. Since the new entry is created immediately upon clicking the link, it is too late to discard the form. The user will be confused with the new entry that unexpectedly showed up in their list. There are more issues to this approach.
  • If the user refresh the page for some reason, it will create more and more duplicate entries without their knowledge.
  • The page for creating a brand new entry and the page for creating an entry by duplication can’t be the same.
However I ended up with the same error message each time I tried to activate the snippet. "Cannot redeclare function fill_values_for_duplicate_entry"  Any thoughts? Better options? Thanks!
2 Answers
Best Answer
Victor Font answered 2 years ago
The error you're receiving is because there is another function that's already been declared with the name "fill_values_for_duplicate_entry". Change the name of this function in the code snippet to something custom of your choosing whereever you see the function name.
Matt CluteMatt Clute replied 2 years ago

Thanks for your response Victor. Do you know why there was an error detected in line 4 of the code?:

if(isset( $_GET['copy_entry']) && !in_array( $field->type, array("hidden", "divider", "html", "end_divider", "user_id" )

I deactivated the duplicate of the snippet and the plugin introduced a "critical error" on my site. The hosting provider had this to say in fixing it:

"Your website and database have been reverted to account for the error introduced by the bad snippet. I've installed the plugin again. Please be very careful because, as you discovered, this does break the site if bad code is pushed."

Do you think it could still be an issue with the snippet apart from having the duplicate? I'm hesitant to try it again after that happening, Thanks.

Ted Det replied 2 years ago

Does this also work with repeater fields in a multi page form?

Victor Font replied 2 years ago

@mattclute, You have open parens in the line you are asking about. It should be:

if( isset( $_GET['copy_entry'] ) && !in_array( $field->type, array("hidden", "divider", "html", "end_divider", "user_id" ) ) )

Victor Font replied 2 years ago

@Teddet, This snippet is not designed to work with repeaters. Repeaters are, in actuality, embedded forms. Each row is a new form entry. It may be possible to duplicate repeater rows with Ajax or jQuery, but I've never tested this because no one has ever asked for this capability.

This article explains how repeaters work. Perhaps it will inspire you to a solution.

Matt CluteMatt Clute replied 2 years ago

Thanks for your response Victor.
It looks like I didn't enter the line that caused the error correctly in my original comment. The original code snippet from FormsCity that caused the error on line 4 actually matches your entry. I wonder if @Teddet was on to something when asking about compatibility with multi-page forms? Given that my form is 5 pages in length.

Victor Font replied 2 years ago

You might be getting some place Matt. Have you ever examined the source code for a multi-page form in the browser? Formidable changes field types to hidden for any field not displayed on the currently viewed page. This could be the problem because you are skipping hidden fields in the snippet. I don't know what's causing your error though. You need to enable WordPress debug to get more details.

Kevin Harwood answered 10 months ago

Yeah I have the exact need to duplicate form entries with repeater fields - shocked that it isn't easier - or already an option. Even the 'duplicate' function on the form entry page doesn't do it, which to me mean that it is in-fact not a 'duplicate' function as well, it doesn't actually duplicate the entries. Totally get that they are their own "form entry id" within another form though - I know I can probably pythonically duplicate the data, but not the strongest in php for this....

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community