Form select field populated from asset listing - retaining values after validation errors


(Erwin Strobel) #1

Hello,

I have created a form that features a select field populated by an asset listing.

I achieved this by creating a text field, and then using the id/name values from this field as the basis for the select field asset listing. This asset listing was then nested in on the Form “Page contents” Bodycopy

The asset listing “Page contents” Bodycopy looks like this “<select name="q3183950:q7" id="q3183950_q7" class="sq-form-field">%asset_listing%</select>

The select field is populating correctly, however when the form is submitted, and there are mandatory field errors, the values for this select field are not retained (e.g. not selected on page refresh).

Was wondering if anyone has a solution on how to retain the select field value when page is refreshed due to mandatory field errors, but not when successfully submitted.

I should note that I am a novice when it comes to javascript. I have tried javascript solution to store the selected value as session variable, this works to a degree, however is not optimal, as the value is still loaded even after the form has been successfully completed.

This is the js that I currently have:

<script>
var selectedItem = sessionStorage.getItem("SelectedItem");  
$('#q3183950_q7').val(selectedItem);

$('#q3183950_q7').change(function() { 
    var dropVal = $(this).val();
    sessionStorage.setItem("SelectedItem", dropVal);
});
</script>

Any help would be much appreciated.


(Erwin Strobel) #2

Solution if anyone else comes across this issue: use a post variable within the nested asset listing, e.g.

<option value="%asset_name%"
%begin_asset_name^eq:{globals_post_q3183950:q7}%
selected="selected"
%end_asset%
    >%asset_name%</option>

no need for js