I am using an embedded Gravity forms Elementor form with AJAX enabled.
I noticed that when the user fills out the form, submits it and there is an error, all the previously submitted inputs are deleted!
How can I change this behaviour and always preserve already inputted fields?
Thanks!
2
Answers
Here is my analysis- To preserve user input in the Gravity form in the event of an error. You can use hook gform_pre_render and gform_validation This ensures that the form retains its properties. This is a simple method. To achieve this goal:
Disable AJAX for Forms: Although AJAX is convenient, it can sometimes cause problems with storing input values. Try temporarily disabling it to see if that fixes the problem.
Use this code snippet: Add the following code to the file. Functions.php or your theme’s custom plugin:
add_filter(‘gform_pre_render’,’save_input_values’);
add_filter(‘gform_validation’,’store_input_values’);
function save_input_values($form) { .
foreach ($form[‘fields’] e.g. &$field) {;
if (isset($POST[‘entry‘ .
$field[‘id’]])) { $field[‘defaultValue’] =
sanitize_text_field($POST[‘input‘ . $field[‘id’]]);
– –
– –
return $form;
This code will fetch the input values from the submitted form. Set as default and ensures that it remains visible even if an error occurs.
Using this solution will allow users to work more efficiently. This is because previously entered data will not be lost in the event of a data transmission error.
You should try this code-
Add_filter(‘gform_pre_render’, ‘save_input_values’);
add_filter(‘gform_validation’, ‘store_input_values’);
characteristic save_input_values($shape)
foreach ($form[‘fields’] as &$subject)
if (isset($POST[‘input‘ . $field[‘id’]]))
$subject[‘defaultValue’] = sanitize_text_field($POST[‘input‘ . $subject[‘id’]]);
function store_input_values($validation_result)
$shape = $validation_result[‘form’];
foreach ($form[‘fields’] as &$field)
if (isset($POST[‘input‘ . $area[‘id’]]))
$subject[‘defaultValue’] =
sanitize_text_field($POST[‘input‘ . $discipline[‘id’]]);