0

I have a query wherein it fetches data from the database, a simple SELECT query which is processed when a certain submit button is clicked. What I want to achieve is to get the results of that query so that I can populate it on my form. So far my code looks like this,

function my_module_my_form_submit_go($form, &$form_state){
    $values_array = array(
        'week'            => $form_state['values']['week'],
        'employee_name'   => $form_state['values']['emp_name'],
    );

    $result = db_query("SELECT * FROM {time} WHERE week='".$values_array['week']."' AND employee_name='".$values_array['employee_name']."' ");
}

and my form looks like this,

// this is where I want to populate the data which came from the database
$form['week'] = array(
    '#type'     => 'textfield',
    '#title'    => 'Week',
    '#value'    => $result['week'],
);

$form['emp_name'] = array(
    '#type'     => 'textfield',
    '#title'    => 'Name',
    '#value'    => $result['employee_name'],
);

$form['go_btn'] = array(
    '#type'           => 'submit',
    '#title'          => t('Go'),
    '#value'          => 'Go',
    '#id'             => 'go_btn',
    '#submit'         => array('my_module_my_form_submit_go'),
);

I am pretty sure that the query has results because I ran it on the database. What I want is to echo/print the results in the form because later on they will be also used in another form submission. How would I do such thing after the submit button is clicked. I have only managed to display a text using drupal_set_message but any data which came from the database, I can't display any. Please help. Thanks.

2
  • What column is {time}? What module creates that?
    – beth
    Commented Oct 21, 2013 at 18:31
  • Also are you will want to change this to using placeholders instead of putting user input into your database directly like this. Dangerous stuff you're doing there.
    – beth
    Commented Oct 21, 2013 at 18:34

1 Answer 1

0

The issue is due to the form submission and rendering process within Drupal. Long story short, Drupal submits the form then re-renders the form page, which goes through a 'submission' process. So, all the drupal_set_message calls are getting cleared during this process.

What you want to use is an AJAX callback on the submit button, populating a section of the form with the data retrieved. And lucky for you the process is much simpler in Drupal 7.

To use the data again in a later form, look at multistep forms.

1
  • Yep, that was my resort on solving this issue.tnx man. Commented Oct 22, 2013 at 13:48

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.