• Resolved umbellularia

    (@umbellularia)


    I have a CF7 form on one of my sites that is meant to be filled out and submitted multiple time successively, for data entry functionality to a manually administered database.

    The default behavior of CF7 is to clear all of the form fields upon submission. This is mostly desirable, however, I’d like a couple fields to hold their values (submitter name and email) such that the submitter doesn’t need to enter them each time they submit the form.

    Can someone provide code to selectively avoid clearing certain fields upon form submission? Or otherwise autofill these certain fields when the form is re-initialized?

    Many thanks !

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter umbellularia

    (@umbellularia)

    OK, I’ve devised a hacky solution for this. I’m sure I’m not following best practices here (beginning with direct edits to the CF7 plugin source), so please do let me know if you note any bugs here, or better ways to accomplish this.

    I am flagging form fields that I don’t want reset/cleared upon submission with a unique class name, like this:
    [email* your-email autocomplete:email class:noResetOnMailSent]

    Then in CF7’s main scripts.js, I have modified this portion:

    			if ( 'mail_sent' == data.status ) {
    				$form.each( function() {
    					this.reset();
    				} );

    And changed it to this:

    			if ( 'mail_sent' == data.status ) {
    				$form.each( function() {
    					$.each( $(this)[0], function() {
    						if (!($(this).hasClass("noResetOnMailSent"))){
    							$(this).not(':button, :submit, :reset, :hidden').val('').prop('checked', false).prop('selected', false);
    						}
    					} );
    				} );

    This original portion of code, called upon the ‘mail_sent’ condition, resets the entire form to default values. The modified version iterates through each form element, clearing the ones that do not possess the “noResetOnMailSent” class name.

    I hope this might help someone else, and doesn’t introduce too many other bugs into CF7. This should clearly be reworked into a separate plugin of its own accord, such that the CF7 core isn’t modified.

    Hey, This is pretty cool.

    Is there a way to maintain this script change via the child theme?

    Thanks!

    You shouldn’t be editing plugin files directly. I’d like to see someone come up with an answer that isn’t editing the plugin files.

    No, probably shouldn’t be editing the plugin directly, but what else can we do? I don’t see any support on this from CF7 otherwise.

    In any case, this didn’t work for me, unfortunately.

    And an another suggestion of simply commenting out this entire section:

    
    			if ( 'mail_sent' == data.status ) {
    				$form.each( function() {
    					this.reset();
    				} );
    

    to prevent clearing all fields doesn’t work in all browsers. It works in Chrome (preserving all fields on submit), but not Safari, where the fields are still cleared on submit.

    OK, I take that back, I found a way to make it work. For a given field, let’s say a text field, I might have:

    
    [text* your-name class:noResetOnMailSent] 
    

    but it seems if I change it to:

    
    [text* your-name id:your-name class:noResetOnMailSent] 
    

    then it works

    Awesome. Thanks @astronomerdave

    mar1os

    (@mar1os)

    I use a form to send data to a database from paper forms. Data is sent by ZAPIER to the database and to the e-mail address. I used this script and everything works fine, the fields that I want do not actually reset … but despite the fact that the input fields are checked, when I use the form again, the fields that have not been reset and are visually selected are not sent on delivery. Fields in database and in email are empty after second send.

    • This reply was modified 5 years ago by mar1os.
Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘How to maintain/not clear certain field values after submission?’ is closed to new replies.