ajax modified form drop down doesn’t save

I have used form_alter to add a ajax callback to an element to my form. When the user chooses an option, it creates dropdown options for the other field. However, while this works fine for changing the form, it doesn’t actually save the value in the field when you click save. I checked the database and its not saved, value is 0. Not exactly sure what is happening.

Here is my code:

 /**  *  Implements hook_form_alter().  */ function chapter_sponsor_levels_form_alter(&$form, &$form_state, $form_id) {   if ($form_id === 'sponsor_node_form') {     //we need to add an ajax call to the OG Group field on the Sponsor node page, so that it populates the sponsor level dropdown     $form['og_group_ref']['und'][0]['default']['#ajax'] = array(       'callback' => 'chapter_sponsor_levels_update_levels',       'wrapper' => 'edit-field-sponsor-level',       'method' => 'replace',     );     if(!empty($form['og_group_ref']['und'][0]['default']['#default_value']) && empty($form_state['values'])) { //if we are viewing the form not from an ajax request       dpm($form);       dpm($form_state);       $gid = $form['og_group_ref']['und'][0]['default']['#default_value'];       chapter_sponsors_levels_set_levels($gid, $form);       dpm($form_state);     }     elseif (!empty($form_state['values']['og_group_ref']['und'])) { //if we have chosen a group       //$gid = 8; //new york       $gid = $form_state['values']['og_group_ref']['und'][0]['target_id'];       chapter_sponsors_levels_set_levels($gid, $form);     }     $form['#submit'][] = 'chapter_sponsor_levels_save_level';   } } 
 function chapter_sponsors_levels_set_levels($gid, &$form) {   $query = 'Select * from {og_vocab_relation} where gid = :gid';   $results = db_query($query, array(':gid' => $gid));   foreach ($results as $result) {     $vid = $result->vid;   }   $chapter_vocab = taxonomy_vocabulary_load($vid); //get the vocabulary associated with the group   if(!empty($chapter_vocab)) {     $terms = entity_load('taxonomy_term', FALSE, array('vid' => $chapter_vocab->vid));     $form['field_sponsor_level']['und']['#options'] = array('_none' => '- None -');     foreach($terms as $term) {       $term_names[$term->weight] = $term->name;     }     ksort($term_names); //sort the terms by their weight and set them as the dropdown values     foreach($term_names as $name) {       $form['field_sponsor_level']['und']['#options'][] = $name;     }     $form['field_sponsor_level']['und']['#title'] = $chapter_vocab->name;   } } 

EDIT: Forgot to add this in to the post

 function chapter_sponsor_levels_save_level(&$form, &$form_state) {   dpm($form_state); } /*  * AJAX callback when click on OG Group field on Sponsor page  */ function chapter_sponsor_levels_update_levels($form, $form_state) {   return $form['field_sponsor_level']; //update the sponsor_level field with the new dropdown values } 

EDIT: I may be getting closer, but not yet there. I realized that if its not saving the value, its proabably because it doesn’t match a list of allowed values. While I didn’t see such a constraint in the database, I found a ‘allowed values’ list in the $form_state array. I modified my code above:

  foreach($term_names as $name) {       $form['field_sponsor_level']['und']['#options'][$name] = $name;       $form_state['field']['field_sponsor_level']['und']['field']['settings']['allowed_values'][$name] = $name;     } 

However, now when I try to save the form when choosing a value, it says that it is an illegal value. I found that it was throwing the error from line 397 in the list module – for some reason, its not passing the ‘allowed values’ test.

Sponsored by SupremePR
This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

ajax modified form drop down doesn’t save

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.