1

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).

2
  • I tried returning dsm($form_state['values']) in the _form_submit. When I check it then it just shows the default values which the elements already had. Which is not good... Apr 15, 2015 at 14:08
  • You need to show your ajax callback function. I'm guessing you're adding form elements there, which won't work, you need to add them in the form definition.
    – Jaypan
    Oct 5, 2016 at 22:52

1 Answer 1

1

I found the answer. The way to reach these values is by using: ['input'].

eg: $app_id = $form_state['input']['edit']['app_id'];

1
  • That's not a good way to do it, as the form values are not sanitized properly. You're opening yourself up to the possibility of a hack.
    – Jaypan
    Oct 5, 2016 at 22:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.