Passer la navigation

[Résolu] Pass a value in JS to a field in form

Ce fil est résolu. Voici une description du problème et la solution proposée.

Problem:

I want to auto-fill a hidden Toolset form field (cast-position) with the value of the ?pos URL parameter + 1, so that each new character entry gets the next position in the list. Using cred_before_save_data prevented the form from saving.

Solution:

Keep the field hidden and not required, then use the cred_save_data hook (after validation) to update the meta field

Or alternatively, use front-end JS with the form’s JS editor to populate the hidden input:

Relevant Documentation:

https://toolset.com/documentation/programmer-reference/cred-api/#cred_save_data

This support ticket is created Il y a 2 weeks, 5 days. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Ce sujet contient 3 réponses, a 1 voix.

Dernière mise à jour par Christopher Amirian Il y a 2 weeks, 2 days.

Assisté par: Christopher Amirian.

Auteur
Publications
#2820126

Hello,

I have a form where one field is :
[cred_field field='cast-position' force_type='field' class='form-control' output='bootstrap']

in the url of the for I have a "?pos" parameter.

In JS I need to add 1 to this "pos" value and pass it to the cast-position field.
Actually in my front-end form I don't want to see the field for the cast position.

Can you help ?

#2820147

Christopher Amirian
Supporter

Les langues: Anglais (English )

Hi,

I am not sure why you want JS on this case? You can set this on form submit.

1- Remove the field from the form or keep it hidden:

[cred_field field='cast-position' type='hidden']

2- Add this to Toolset → Custom Code (or your child theme functions.php).

Replace 12345 with your form ID and wpcf-cast-position with your field’s real meta key if different.

add_action( 'cred_before_save_data', function( $post_id, $form_data ) {

    if ( (int) $form_data['id'] !== 12345 ) {
        return;
    }

    // read ?pos= from the URL, add 1
    $pos = isset($_GET['pos']) ? intval($_GET['pos']) : null;
    if ( $pos !== null ) {
        update_post_meta( $post_id, 'wpcf-cast-position', $pos + 1 );
    }

}, 10, 2 );

Hook reference: https://toolset.com/documentation/programmer-reference/cred-api/#cred_before_save_data

Did I understand what you want to do correctly? If not, please give more context and the details so I can understand.

Thanks.

#2820256

Hi Christopher,

Thanks for your reply.

I'm registering movie character names and their position in the list as legally some actors must be listed first. That's why the position is important. That's also why I'm taking the last position when invoking the form to add a new one and want to add 1 to be the last on the list.

When applying your script, the form is actually not saving the new post anymore. Do you have an idea why ?

#2820591

Christopher Amirian
Supporter

Les langues: Anglais (English )

Hi,

It is hard to know what the issue might be. Usually, three scenarios might be included:

1) Keep the field in the form, hidden, and not required

In the form markup, use the hidden type (do not mark the field as required in the field group)

2) Set the value after the post is created

Use cred_save_data (not cred_before_save_data) so the post ID is valid and you’re past the server-side validation step.

add_action( 'cred_save_data', function( $post_id, $form_data ) {

    // Limit to this form
    if ( (int) $form_data['id'] !== 12345 ) return; // <-- replace with your form ID

    // Read ?pos= from the URL, add 1
    $pos = isset($_GET['pos']) ? intval($_GET['pos']) : null;

    // Cast-position meta key for a Types field with slug 'cast-position'
    $meta_key = 'wpcf-cast-position';

    if ( $pos !== null ) {
        update_post_meta( $post_id, $meta_key, $pos + 1 );
    }

}, 100, 2 );

Note: The meta key for a Types field slug cast-position is wpcf-cast-position. Double-check this in the database or by viewing the input’s name attribute in the form HTML.

3) Quick sanity checks

Temporarily make the field visible and submit with ?pos=3. If the post saves and you see 4 in the saved field, your hook & key are correct—then flip it back to hidden.

If it still won’t save, remove the form-ID condition to see if you picked the wrong ID.

If you truly want to set it on the client, keep the field hidden and drop this in the form’s JS editor:

jQuery(function($){
  var params = new URLSearchParams(location.search);
  var base = parseInt(params.get('pos') || '', 10);
  if (!isNaN(base)) {
    $('input[name="wpcf-cast-position"]').val(base + 1);
  }
});

But the server-side method above is more reliable (no dependence on front-end JS or timing), and it won’t block the save.

Again the points above are what I could guess but in the end if it gets more complicated you will need to hire a developer to handle such custom code.

Thanks.