On a edit page I added a form so the user can select what he wants to edit. With that info I used ajax to show the info which belongs to that item.
But when submitting the form it isn't taking the values the way as I did it in a normal form. In this way (see code below) it gives an error: Notice: Undefined index: replace_textfield in push_notifications_edit_app_form_submit()
And When I leave out the ['replace_textfield'] part it gives no error but the values contain nothing.
Here is my code:
function push_notifications_edit_app_form($form, &$form_state) {
$form = array();
$query = db_select('push_notifications_api','pt');
$query->distinct();
$query->fields('pt',array('app_name'));
$result = $query->execute();
foreach ($result as $record){
$options[$record->app_name] = $record->app_name;
}
$form['title'] = array(
'#type' => 'fieldset',
'#title' => t('Edit application'),
'#description' => t('Select the application you want to edit.'),
'#tree' => TRUE,
);
$form['title']['changethis'] = array(
'#title' => t("Application Name"),
'#type' => 'select',
'#options' => array(
'Select Application',
$options,
),
'#ajax' => array(
// #ajax has two required keys: callback and wrapper.
// 'callback' is a function that will be called when this element changes.
'callback' => 'push_notifications_edit_app_form_callback',
// 'wrapper' is the HTML id of the page element that will be replaced.
'wrapper' => 'replace_textfield_div',
// There are also several optional keys - see ajax_example_autocheckboxes // below for details on 'method', 'effect' and 'speed' and // ajax_example_dependent_dropdown for 'event'.
),
);
// This entire form element will be replaced whenever 'changethis' is updated.
$form['replace_textfield'] = array(
// '#type' => 'textfield',
// '#title' => t("Why"),
// The prefix/suffix provide the div that we're replacing, named by
// #ajax['wrapper'] above.
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
// An AJAX request calls the form builder function for every change.
// We can change how we build the form based on $form_state.
if (!empty($form_state['values']['title']['changethis'])) {
$form['replace_textfield']['edit'] = array(
'#type' => 'fieldset',
'#title' => 'Application data',
'#description' => 'Edit your application data here.',
'#tree' => TRUE,
);
$app_name = $form_state['values']['title']['changethis'];
$app_id = db_query('SELECT DISTINCT app_id FROM push_notifications_api WHERE app_name = :appname LIMIT 1', array(':appname' => $app_name))->fetchField();
$gcm_key = db_query('SELECT DISTINCT GCM FROM push_notifications_api WHERE app_name = :appname LIMIT 1', array(':appname' => $app_name))->fetchField();
$ios_key = db_query('SELECT DISTINCT ios FROM push_notifications_api WHERE app_name = :appname LIMIT 1', array(':appname' => $app_name))->fetchField();
$form['replace_textfield']['edit']['app_id'] = array(
'#type' => 'textfield',
'#title' => 'Application ID',
'#description' => 'Enter the application ID here.',
'#value' => $app_id,
'#required' => TRUE,
'#size' => 20,
'#maxlength' => 20,
'#weight' => 25,
);
$form['replace_textfield']['oldid'] = array(
'#type' => 'hidden',
'#value' => $app_id,
);
$form['replace_textfield']['edit']['GCM'] = array(
'#type' => 'textfield',
'#title' => 'GCM Api Key',
'#description' => 'Enter the GCM API key here.',
'#value' => $gcm_key,
'#required' => TRUE,
'#size' => 40,
'#maxlength' => 255,
'#weight' => 30,
);
$form['replace_textfield']['edit']['iOS'] = array(
'#type' => 'textfield',
'#title' => 'iOS Api Key',
'#description' => 'Enter the iOS API key here.',
'#value' => $ios_key,
'#required' => TRUE,
'#size' => 40,
'#maxlength' => 255,
'#weight' => 35,
);
$form['replace_textfield']['submit'] = array(
'#type' => 'submit',
'#value' => 'Edit',
);
}
return $form;
}
function push_notifications_edit_app_form_callback($form, $form_state) {
// The form has already been submitted and updated. We can return the replaced
// item as it is.
return $form['replace_textfield'];
}
function push_notifications_edit_app_form_submit($form, &$form_state) {
// $app_id = $form_state['values']['app_id'];
$app_id = $form_state['replace_textfield']['values']['edit']['app_id'];
$gcm = $form_state['replace_textfield']['values']['edit']['GCM'];
$ios = $form_state['replace_textfield']['values']['edit']['iOS'];
$oldid = $form_state['replace_textfield']['values']['oldid'];
$query = db_update('push_notifications_api')
->fields(array(
'app_id' => $app_id,
'GCM' => $gcm,
'ios' => $ios,
))
->condition('app_id', $oldid)
->execute();
drupal_set_message(t('The form has been submitted.'));
}
Also in the following string it gives an error: $app_id = $form_state['values']['replace_textfield']['edit']['app_id'];
Notice: Undefined index: replace_textfield in push_notifications_edit_app_form_submit() (line 790 of C:\wamp\www\drupal2\sites\all\modules\push_notifications\includes\push_notifications.admin.inc).