Support

Account

Home Forums General Issues Dynamically set form ID

Solving

Dynamically set form ID

  • Hi, I’m working on website, where customer is allowed to create and save own custom posts on frontend. The problem is, edition should be possible within loop or at archive page – saved posts are custom post types – sizes of products. Customer can filter own sizes and edit them (in popup).

    I’ve put form inside loop, so the ID of form is ID or CPT post, but as it’s always the same form, there are same ID’s of fields and it won’t work this way I suppose 🙁

    Another way would be to put the form outside loop and set the ID of the form through jQuery when the edit button of post with particular ID was clicked.

    Any suggestion how to do that?
    Either use same form multiple times or dynamically set form ID through jQuery

    Thanks in advance for any suggestions 🙂

  • Hey man!

    If you want to every post on page have it’s own form, you can do something like this in your acf_form() function:

    acf_form(array(
       'form_id' = 'form_' . get_the_ID(),
       'post_id' = get_the_ID()
       [...]
    ));

    Assuming that you are going to place that inside the loop like that

    while(have_posts()): the_post()

    P.s.: Sorry about my english

  • That’s how I’ve made it. But it won’t work 🙁
    This is my form:

    acf_form_head();

    $fields = array(
    ‘field_5d88ebc436c1d’, // s_body_size
    ‘field_5d88ec8636c1e’, // s_body_type
    ‘field_5d88ecc036c1f’ // s_head_size
    );
    acf_form(array(
    ‘id’ => ‘form-‘.$post->ID,
    ‘post_id’ => $post->ID,
    ‘new_post’ => array(
    ‘post_type’ => ‘custom_size’,
    ‘post_status’ => ‘publish’,
    ),
    ‘post_title’ => false,
    ‘post_content’ => false,
    ‘uploader’ => ‘basic’,
    ‘fields’ => $fields,
    ‘submit_value’ => ‘Update Size’
    ));

    Placed in loop ( while(have_posts()): the_post() )

    And it gives errors. Form itself has own ID, but inputs not. (Found 7 elements with non-unique id #acf-field_5d88ebc436c1d) as all fields of the form have same id’s

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Dynamically set form ID’ is closed to new replies.