Inspired by the excellent LDAP Integration Module. I'm creating a module that will return user demographics from a LDAP query. There is just something that I'm not getting about the form submit process.

I have my form with 1 text field and 1 submit button:

function ldap_dir_menu() {
    $items = array();

    $items[] = array(
        'path' => 'directory',
        'title' => t(variable_get('form_title','LDAP Directory')),
        'callback' => 'drupal_get_form',
        'callback arguments' => array('ldap_dir_search_form'),
        'access' => user_access('administer ldap_dir'),
        'type' => MENU_CALLBACK,
    );

    return $items;
}

function ldap_dir_search_form(){

    $form['dir-search']['keyword'] = array(
        '#type' => 'textfield', 
        '#title' => t('Search directory'), 
        '#description' => t('Search the directory'),
        '#size' => 50, 
        '#maxlength' => 255, 
        '#required' => TRUE,
    );

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

    return $form ;
}

I have my "_submit" function:

function ldap_dir_search_form_submit($form_id, $form_values) {

// Run the LDAP query to get the matching entries from the form text field
$output = "bunch of logic to get LDAP entries"...

}

All of this is working so far. My problem is: How do I return the page with the results under the original form?

I was able to "render" the results with:

    print theme('page', $output);
    exit;

but... No form at the top and it doesn't seem like the right way...

Thanks,

Wade

Comments

timatlee’s picture

Wow, cool... I was gonna start digging into something similar for something at work (I want to have an auto-fill field thats populated from a list of usernames in AD).. I'm now interested in what you're coming up with...

Can you add the form to the _submit() function?

function ldap_dir_search_form_submit($form_id, $form_values) {

// Run the LDAP query to get the matching entries from the form text field
$output = drupal_get_form('ldap_dir_search_form');
$output .= "bunch of logic to get LDAP entries"...

return $output;

}

Not sure if that's what you're after though... Immediately, I can see that the form won't be populated with data from the previous submission..

hedroom’s picture

I tried that but I get a "PHP has encountered a Stack overflow" message.

I have this thing working, it's just that I have to put a "Search Again" link on the results page.

The thing I'm not getting is when I put return $output; at the end of the routine, the $output variable is passed to the querystring resulting in a page not found error.

This has to be real simple to fix, I believe that I'm just not seeing it.

If you would like the code I have you are welcome to it. Needs a little work....

Also, LDAP Integration must be installed also...

Wade

timatlee’s picture

hum. API reference for drupal_submit_form: http://api.drupal.org/api/5/function/drupal_submit_form

Maybe you shouldn't be returning anything from _submit() ? I looked through the modules I've written, and have yet to see me returning text from an _submit() - usually just a "drupal_set_message" followed by "drupal_goto"... but the symptoms you are seeing seems to be in line with what the documents say.

If you don't mind sharing, I would certainly love to see it. For some reason, LDAP queries seem to be a mental wall for me. I'd like to make an auto-complete text field for an AD Username for a problem ticket system.. and, left to my own devices, I'd make a local cache of the AD users I want in MySQL updated by cron or something, then retrieve from that... not exactly "good" programming. My email address is my website username @gmail.com.

Good luck... :)

hedroom’s picture

Yeah, I think that you are right about not having to use a _submit function. All I want to do is go and get the data and then display it under the search form. But how do I pass the form data to the LDAP processing and then display the results on the same page? I thought that there were some ajax functions built into the Drupal core. I'm going to look at that angle also.

Anyway, I'm e-mailing what I've done so far...

h