0

I have created a dynamic dropdown list and am getting the warning

Illegal string offset 'User Role'.

Only the first character of the value is then being submitted to the database

add_action( 'gform_after_submission_6', 'add_entry_user_role', 10, 2 );
function add_entry_user_role( $entry, $form ) {
    $user_role = $entry[12];
    $add_user_roles = unserialize( $user_role );

    foreach( $add_user_roles as $add_user_role ) :

    $wpdb->insert(
        'inm_user_relation',
        array(
            'tag_id' => $add_user_role['User Role']
        )
    );

    endforeach;
}

Does anyone know what's wrong?

Bharata
  • 13,509
  • 6
  • 36
  • 50
Dudley
  • 63
  • 12
  • 1
    You may want to double check the value of `$add_user_role`. Most likely it's a string. – Jim Aug 23 '18 at 14:19
  • 3
    Possible duplicate of [Illegal string offset Warning PHP](https://stackoverflow.com/questions/9869150/illegal-string-offset-warning-php) – Jim Aug 23 '18 at 14:19
  • First thing is first, do a print_r or var_dump on your variables. See what is actually getting sent. Also, the foreach is wrong. it should look like `foreach ($add_user_roles as $add_user_role) { $wpdb->insert.... }` and no `endforeach;` – Chad K Aug 23 '18 at 14:20
  • 2
    @ChadK there's nothing wrong with that `foreach` syntax wise, check [alternative syntax for control structures](http://php.net/manual/en/control-structures.alternative-syntax.php) – AmmoPT Aug 23 '18 at 14:24
  • how to I check the values if they are in a form submit process? I've tried var_dump but the spinner just goes round on the submit – Dudley Aug 23 '18 at 14:26
  • Make sure the form is not submitted through ajax @Dudley is your form embeded in a post content? If so, try `[gravityform id=*theformid* ajax=false]` – AmmoPT Aug 23 '18 at 14:29
  • I set Ajax to false and now I get this error as well: Warning: Cannot modify header information - headers already sent by (output started at /home/rockpool/public_html/inm-developer/wp-content/themes/inm-developer/lib/inc/gravity-forms.php:200) in /home/rockpool/public_html/inm-developer/wp-content/plugins/gravityforms/form_display.php on line 199 – Dudley Aug 23 '18 at 14:35
  • What exactly is `$user_role = $entry[12];` gonna retrieve? According to documentation there are only [11 standard properties to the Entry object](https://docs.gravityforms.com/entry-object/). Have you tried [accessing the entry by looping through the form fields](https://docs.gravityforms.com/gform_after_submission/#3-access-the-entry-by-looping-through-the-form-fields)? – AmmoPT Aug 23 '18 at 15:04

0 Answers0