this query returns a name

$result=db_query("SELECT name FROM users WHERE users.uid=:uid",array(':uid'=>$artcheckin))->fetchField();

and now i want to save the name retrived in the textfield on form load..

will this work..$form['name']['value'] =$result;

if not any other way ..

Wat hook should i use so that values are stored in the textfield from the database at the time of page load ..

Comments

tusharbodke’s picture

Hi Kunal,

you can use the '#default_value' attribute/property for text-field 'name' to populate the data on form/page load.

$form['name']['#default_value'] = $result;

Please refer Drupal 7 form API reference for more information.

Tushar Shantaram Bodake

kunalshah1307’s picture

Hi Tushar,

Your solution dosent work with my code..
the code for my text box creation is this

$form['name'] = array(
'#title' => t('Name'),
'#size'=>12,
'#type' => 'textfield',
'#description' => t('Please enter your name.'),

this is wat i did

function basicinfo_submit($form, &$form_state) {
$artcheckin=1;
$result=db_query("SELECT name FROM users WHERE users.uid=:uid",array(':uid'=>$artcheckin))->fetchField();
$form['name']['#default_value'] = $result;
drupal_set_message('hello--'.$result);

Value of text box is null..as of now this is happening is on submit button click
and i want the textbox to be filled on page load..what is the hook for page load event in drupal

jaypan’s picture

You are coming at this from the wrong angle. Here is the process you can use:

1) Load values from the database, into an object (or array)
2) Pass that object to the form definition, populating your form with the values
3) On submit, save the values to the database

By doing this, the data in step three is loaded in step one, and entered into the form.

As for how to do that, it depends on how/where you are using/generating your form. The hook you are looking for doesn't exist.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.

kunalshah1307’s picture

I am making a customized module which has the forms I require.

I am able to do the first step and will be able to do the third step as well ..How do i pass the object to form definition and then populate the values of the form when the page is loaded

tusharbodke’s picture

Things Todo

Build the form with textfield and value of textfield set with prepopulated data.

sample form with textfield, Anytime we load form we will have name textfield set to value(if exist).
After submit new value will be set in textfield.


function MODULENAME_basicinfo_form() {
  $form = array();
  //Assumption : your query return proper result.
  $artcheckin=1;
  $result=db_query("SELECT name FROM users WHERE   users.uid=:uid",array(':uid'=>$artcheckin))->fetchField();

  $form['name'] = array(
    '#type' => 'textfield',
    '#title' => t('Name'),
    '#description' => t('Please enter your name.'),
    '#size'=>12,
    '#default_value' => $result,);

  $form['submit'] = array (
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}

function MODULENAME_basicinfo_form_submit($form, &$form_state) {
//Not required.
/*$artcheckin=1;
$result=db_query("SELECT name FROM users WHERE users.uid=:uid",array(':uid'=>$artcheckin))->fetchField();
$form['name']['#default_value'] = $result;*/


//Update the name with new value in DB.


drupal_set_message('hello--'. $form_state['values']['name']);

There are also diff ways just retaining the form elements value. But above mention solution is best as per my understanding.

Tushar Shantaram Bodake

jaypan’s picture

I am able to do the first step and will be able to do the third step as well ..How do i pass the object to form definition and then populate the values of the form when the page is loaded

It depends on where you are calling the form, but here is an example:


function my_module_menu()
{
  $menu['my_form_page/%'] = array
  (
    'title' => 'My Form',
    'page callback' => 'my_form_page',
    'page arguments' => array(1),
    'access callback' => TRUE,
  );
  return $menu;
}

function my_module_load_data($item_id)
{
  return db_query('SELECT value1 FROM {some_table} WHERE item_id = :item_id', array(':item_id' => $item_id))->fetchObject();
}

function my_form_page($item_id)
{
  $my_object = my_module_load_data($item_id);
  return drupal_get_form('my_form', $my_object);
}

function my_form($form, &$form_state, $my_object)
{
  $form['element1'] = array
  (
    '#type' => 'textfield',
    '#title' => t('Item 1'),
    '#default_value' => $my_object->value1,
  );
  return $form;
}

As you can see, my drupal_get_form() is called from a menu callback function. Before it is called, I load the data, and then pass that loaded data to the form definition. In the form definition, I use the data to populate the form element.

Contact me to contract me for D7 -> D10/11 migrations.
Or anything really. I'm friendly, and open to work or conversation.