By kunalshah1307 on
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
Hi Kunal, you can use the
Hi Kunal,
you can use the '#default_value' attribute/property for text-field 'name' to populate the data on form/page load.
Please refer Drupal 7 form API reference for more information.
Tushar Shantaram Bodake
Hi Tushar, Your solution
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
You are coming at this from
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.
I am making a customized
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
Let me explain this from the start..
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.
There are also diff ways just retaining the form elements value. But above mention solution is best as per my understanding.
Tushar Shantaram Bodake
I am able to do the first
It depends on where you are calling the form, but here is an example:
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.