7

I've implemented hook_menu roughly as follows:

function sample_menu() {
    return array(
        'path/to/things' => array(
            'page callback' => 'callback_function',
            'access arguments' => array('access content'),
            'page arguments' => array(),
            'type' => MENU_CALLBACK
        )
    );
}

I want to be able to post data to this url. So says $_POST is the following array:

array(
    'data1' => 'value1',
    'data2' => 'value2',
)

How can I access $_POST['data1'] and $_POST['data2'] inside the callback? Directly accessing $_POST seems to not work (it's var_dumped as empty).

1 Answer 1

14

They access $_POST in the same way other functions do.

Take file_ajax_upload() as example; it contains the following code.

  if (empty($_POST['form_build_id']) || $form_build_id != $_POST['form_build_id']) {
    // Invalid request.
    drupal_set_message(t('An unrecoverable error occurred. The uploaded file likely exceeded the maximum file size (@size) that this server supports.', array('@size' => format_size(file_upload_max_size()))), 'error');
    $commands = array();
    $commands[] = ajax_command_replace(NULL, theme('status_messages'));
    return array('#type' => 'ajax', '#commands' => $commands);
  }

The function is a page callback defined in file_menu().

  $items['file/ajax'] = array(
    'page callback' => 'file_ajax_upload', 
    'delivery callback' => 'ajax_deliver', 
    'access arguments' => array('access content'), 
    'theme callback' => 'ajax_base_page_theme', 
    'type' => MENU_CALLBACK,
  );
1
  • looks like this is correct. I think my error had to do with how I was posting the data ... I wasn't doing it correctly anyway.
    – gregghz
    Commented Jul 6, 2012 at 21:08

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.