0

In a module, I am using the following code.

function mysimple_form($form, &$form_state) {
  $form['#method'] = 'get';
  $form['#action'] = '/buy/{_val_}';
  $form['search_location'] = array(
    '#type' => 'textfield',
    '#title' => 'Search',
    '#default_value' => t('kathmandu'),
    '#size' => 60,
    '#maxlength' => 128,
    '#required' => TRUE,
    '#attributes' => array(
      'placeholder' => t('Search')
    )
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Search'),
  );

  $form['#submit'][] = 'mysimple_form_submit';
  return $form;
}

function mysimple_form_submit($form, &$form_state) {
  krumo('asdsads');
  die();
}

I am using that form in hook_node_view().

function mymodule_node_view($node, $view_mode, $langcode) {
  $form = drupal_get_form('mysimple_form');
  $node->content['simpleform'] = array(
    '#weight' => -1,
    '#markup'=> drupal_render($form)
  );
}

The form is displayed perfectly, but the callback function doesn't seem to work. I also tried $form['actions']['#submit][] = 'mysimple_form_submit'; and $form['actions']['submit']['#submit][] = 'mysimple_form_submit'; but the form submission handler is still not invoked.

2
  • And without die();?
    – leymannx
    Commented Nov 10, 2019 at 11:20
  • @leymannx Doesn't change anything, still doesn't call.
    – ShanjayG
    Commented Nov 10, 2019 at 11:22

2 Answers 2

1

There is two problems:

  1. You form method is GET so form submit action will be the actual path with the params like node/10?search_location=kathmandu&op=Search&form_build_id=form-s4JrAE91au_BWakeBLD7RUsktRTBXFCpJQrnHGiOjg8&form_token=ES2W0RDpLOnWZUsXOnP4f9gBmSXcYLvqhO9uy1ee4Vk&form_id=d7_exp_form

  2. The second one you set your action to '/buy/{_val_}' so automatically after the submit you will redirect to /buy/{_val_} without invoke the submit handler.

Suggestion:

  1. If you want to invoke the submit handler remove the $form['#method'] and $form['#action'], so your form will be with post method and your submit handler will invoked.
  2. Remove only $form['#method'] and create menu entry /buy/% and you can do your submit handler logic in the /buy/% callback function.
0

In your case, you don't need to specify custom submit handler because if your form ID is mysimple_form, Drupal should use mysimple_form_submit as default form submission function.

For more info see the https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.html/7.x#submit_property

1
  • Thank you your suggestion was the first thing I tried and also a variations of it. No luck so far.
    – ShanjayG
    Commented Nov 10, 2019 at 10:56

Your Answer

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

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