I have a form that save the data in the usermeta and I'm using the flexible content to allow specific user to create forms depends of the country they are.
I'm trying to save it but I don't have a key/ID to save the question with the answer to allow to the future people can edit them because if I change the order of the same kind of sub field they just change the position in the array and that wont let me get an specific ID to save it in the user meta.
I'm just trying to make a dynamic form to the people are registering and in the future allow them to change it.
I tried using the key of the layout but the problem is that the layout can be ordered and that change the array where the key is save and doesn't let me relate in the future.
This is how I did it, I create the labels for the user and inputs
<?php
$mypost = get_page_by_title($user_country , OBJECT, 'Countries');
// check if the flexible content field has rows of data
if( have_rows('application_form_fields', $mypost->ID) ): ?>
<div class="col-sm-12">
<h2><?php _e('Extra Information', 'Adecco_Login_Plugin'); ?></h2>
</div>
<?php // loop through the rows of data
$flex = get_field('application_form_fields');
error_log(print_r($mypost, true));
while ( have_rows('application_form_fields', $mypost->ID) ) : $row = the_row();
if( get_row_layout() == 'short_text' ):?>
<div class="col-sm-12">
<div class="form-row">
<input type="text" name="uni" class="form-control" id="uni" placeholder="<?php the_sub_field('label'); ?>" required>
</div>
</div>
<?php elseif( get_row_layout() == 'long_text' ): ?>
<div class="col-sm-12">
<div class="form-row">
<input type="text" name="uni" class="form-control" id="uni" placeholder="<?php the_sub_field('label'); ?>" required>
</div>
</div>
<?php elseif( get_row_layout() == 'multipick' ): ?>
<div class="col-sm-12">
<div class="form-row">
<select id="field" name="field" class="form-control" required>
<option value=""><?php the_sub_field('label'); ?></option>
<?php
while ( have_rows('values') ) : the_row(); ?>
<option><?php the_sub_field('item_text') ?></option>
<?php endwhile;
?>
</select>
</div>
</div>
<?php elseif( get_row_layout() == 'checkbox' ): ?>
<div class="col-sm-12">
<div class="form-row">
<div class="col-sm-9">
<label for="toggle3"><?php the_sub_field('label'); ?></label>
</div>
<div class="col-md-3">
<input id="toggle-off3" name="toggle3" type="radio" value="No">
<label class="radio" for="toggle-off3"><?php _e('No', 'Adecco_Login_Plugin'); ?></label>
<input id="toggle-on3" name="toggle3" type="radio" value="Yes">
<label class="radio" for="toggle-on3"><?php _e('Yes', 'Adecco_Login_Plugin'); ?></label>
</div>
</div>
</div>
<?php endif;
endwhile;
else :
// no layouts found
echo "No layout found";
endif;
[
{
"key": "group_5c509fc032e05",
"title": "Application Form",
"fields": [
{
"key": "field_5c509fd900af1",
"label": "Custom Fields for Application Form",
"name": "application_form_fields",
"type": "flexible_content",
"instructions": "",
"required": 0,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"layouts": {
"5c509fe7de1c8": {
"key": "5c509fe7de1c8",
"name": "short_text",
"label": "Short Text",
"display": "table",
"sub_fields": [
{
"key": "field_5c50aab34181a",
"label": "Label",
"name": "label",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
],
"min": "",
"max": ""
},
"5c50a5975061b": {
"key": "5c50a5975061b",
"name": "long_text",
"label": "Long Text",
"display": "table",
"sub_fields": [
{
"key": "field_5c50abad60442",
"label": "Label",
"name": "label",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
],
"min": "",
"max": ""
},
"5c50a5d85061d": {
"key": "5c50a5d85061d",
"name": "multipick",
"label": "Multipick",
"display": "table",
"sub_fields": [
{
"key": "field_5c50abc760443",
"label": "Label",
"name": "label",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
},
{
"key": "field_5c50abd060444",
"label": "Values",
"name": "values",
"type": "repeater",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"collapsed": "",
"min": 0,
"max": 0,
"layout": "row",
"button_label": "Add Item",
"sub_fields": [
{
"key": "field_5c50abdc60445",
"label": "Item Text",
"name": "item_text",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
]
}
],
"min": "",
"max": ""
},
"layout_5c50ac6f6044a": {
"key": "layout_5c50ac6f6044a",
"name": "checkbox",
"label": "Checkbox (YES\/NO)",
"display": "table",
"sub_fields": [
{
"key": "field_5c50ac6f6044b",
"label": "Label",
"name": "label",
"type": "text",
"instructions": "",
"required": 1,
"conditional_logic": 0,
"wrapper": {
"width": "",
"class": "",
"id": ""
},
"default_value": "",
"placeholder": "",
"prepend": "",
"append": "",
"maxlength": ""
}
],
"min": "",
"max": ""
}
},
"button_label": "Add Field",
"min": "",
"max": ""
}
],
"location": [
[
{
"param": "post_type",
"operator": "==",
"value": "countries"
}
]
],
"menu_order": 0,
"position": "normal",
"style": "default",
"label_placement": "top",
"instruction_placement": "label",
"hide_on_screen": "",
"active": 1,
"description": ""
}
]