Retrieve value of single form element

Retrieve value of single form element

by Ivn Vai -
Number of replies: 10

Hi there,

I have a form which has got many elements and each of those form elements when filled and submitted get stored in different database table.

So is there a way of just retrieving a specific form value and its data and store it in a table?

Like if I have

$mform->addElement('select', 'selectdepname', get_string('selectdepname', 'block_hierarchycreation'), $deplist);
        
 $mform->addElement('text','sname', get_string('sname', 'block_hierarchycreation'), array('size' => 60));

In the above code my select menu is getting filled with values from another database table say table X


So firstly, is it important to name the form elements same as the database field names?

Secondly if I need to store 'sname'  in a separate table, table Y and then take d newly inserted record's id (from Table Y) and the drop down menu's selected id from the table X and put those ids together in another Table Z, then how do I do it?

Hopefulyl this is not very difficult and I have referred the http://dev.moodle.org/mod/page/view.php?id=49 which shows step wise as to how create form and store data in a table.

But couldn't figure out how to retrieve specific form values and store it in different tables simultaneously wrt scenario I have mentioned above?

Any help is appreciated. Thanks ina advance.

Average of ratings: -
In reply to Ivn Vai

Re: Retrieve value of single form element

by Ivn Vai -

I forgot to say that I am using Moodle 2.2.1. And this is my code in detail, if it helps. Its basicallya  form with many elements. But how would I determine which element of the form is clicked and how?

        $mform->addElement('header', 'deptaddhierarchyheader', get_string('dept', 'block_hierarchycreation'));

        $mform->addElement('text','dname', get_string('deptname', 'block_hierarchycreation'), array('size' => 60));
         $submitlabel = null; // Default
       
            $submitlabel = get_string('addnewdept', 'block_hierarchycreation');
            $this->add_action_buttons(true, $submitlabel);
        
        $mform->addElement('header', 'sectionaddhierarchyheader', get_string('section', 'block_hierarchycreation'));
        foreach($deptnames as $deptname)
        {$deptlist[] = $deptname->name;}
        $mform->addElement('select', 'selectdeptname', get_string('selectdeptname', 'block_hierarchycreation'), $deptlist);
        
        $mform->addElement('text','sname', get_string('sectname', 'block_hierarchycreation'), array('size' => 60));
         $submitlabel = null; // Default
       
            $submitlabel = get_string('addnewsect', 'block_hierarchycreation');
            $this->add_action_buttons(true, $submitlabel);

        $mform->addElement('header', 'areaaddhierarchyheader', get_string('area', 'block_hierarchycreation'));
        foreach($sectnames as $sectname)
        {$sectlist[] = $sectname->name;}
        $mform->addElement('select', 'selectsectname', get_string('selectsectname', 'block_hierarchycreation'), $sectlist);
        
        $mform->addElement('text','aname', get_string('areaname', 'block_hierarchycreation'), array('size' => 60));
         $submitlabel = null; // Default
       
            $submitlabel = get_string('addnewarea', 'block_hierarchycreation');
            $this->add_action_buttons(true, $submitlabel);

n the form looks smthng like this:

My form

So now If I just want to check if only my 'Add new Section' button is clicked and just wanna retrieve my 'Choose a Department' dropdown n 'Section Name'  text values and store it in a  database, how do i do it? without touching or disturbing the rest of d form.

Please help as this is driving me crazy.

In reply to Ivn Vai

Re: Retrieve value of single form element

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You need to change the 'add_action_buttons', call to something like this (modified from http://docs.moodle.org/dev/lib/formslib.php_Form_Definition#submit.2C_reset_and_cancel ):

$buttonarray=array();
$buttonarray[] = &$mform->createElement('submit', 'submitdepartment', $submitlabel);
$buttonarray[] = &$mform->createElement('cancel');
$mform->addGroup($buttonarray, 'buttonar', '', array(' '), false);
$mform->closeHeaderBefore('buttonar');

Once you've done that (for each section, changing 'submitdepartment', as appropriate), you can then do the following to process the form:

$data = $form->get_data();
if (isset($data->submitdepartment)) {
// Process the department fields only
} else if (isset($data->submitsection)) {
// Process the area fields only

Average of ratings: Useful (1)
In reply to Davo Smith

Re: Retrieve value of single form element

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

And to add to my explanation a little, to save to the database, do something like this:

$newrecord = new stdClass();
$newrecord->name = $data->dname;
$newrecord->id = $DB->insert_record('tbl_department', $newrecord);

(you can call the fields on the form anything you like, but it is clearer if they match the names in the database; it also means you can just drop the $data object straight into the insert/update_record call, without any extra processing).

In reply to Davo Smith

Re: Retrieve value of single form element

by Ivn Vai -

@Davo Smith

Thanks for replying. Your idea for buttons worked and now I can properly access only the specific parts of the form when a button is clicked.

Also the new record is getting stored in the database. But Is there a way to just retrieve a field using any of the moodle function.

Like If I have selected 'Retail' from the drop-down and have entered 'Section X' in section name, I want two things to happen after the button being clicked:

1) Save 'Section X' in tbl_sections

2) Retrieve this newly inserted record's id from tbl_sections and retrieve the id of the selected department name from tbl_departments and insert both these ids as a  new record in tbl_mappings.

I wrote this code but obviously it threw an error saying 'Error reading from the database'  Can you spot the error in my code,

else if (isset($data->submitsect))

{

$newrecord->name = $data->sname;

$newrecord->id = $DB->insert_record('tbl_sections', $newrecord);

$deptid = $DB->get_records_sql('SELECT id FROM mdl_tbl_depts WHERE name = "' . $data->selectdeptname . '"');

$sectid = $DB->get_records_sql('SELECT id FROM mdl_tbl_sections WHERE name = "' . $data->sname .'"');

$newmappingrecord = new stdClass();

$newmappingrecord->sectionid = $sectid;

$newmappingrecord->deptid = $deptid;

$newmappingrecord->id = $DB->insert_record('tbl_secttodept', $newmappingrecord);

}

Thanks..

In reply to Ivn Vai

Re: Retrieve value of single form element

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Sorry, I don't know if you fixed this, but the database tables always have the prefix added automatically by the Moodle DB functions.

So, if your prefix is (the default) 'mdl_', then:

$DB->insert_record('tbl_secttodept', $newmappingrecord);

Will result in the record being inserted into the 'mdl_tbl_secttodept' table (assuming you have already created this).

Putting 'tbl_' at the start of a table name is a really bad idea - I only put it in the example to make it clear that it was the name of a database table - I should have explained that more clearly.

In reply to Davo Smith

Re: Retrieve value of single form element

by Ivn Vai -

@Davo,

Thanks but I did notice that before hand and hence always wrote only my actual table name. But thank you anyway for bringing that to my notice.

In reply to Davo Smith

Re: Retrieve value of single form element

by Ivn Vai -

I just used the following code to retrieve the value selected from a drop down box but I get the index value at which it is sitting in the drop down instead of the text.

foreach($deptnames as $deptname)
        {$deptlist[] = $deptname->name;}
        $mform->addElement('select', 'selectdeptname', get_string('selectdeptname', 'block_hierarchycreation'), $deptlist);

$dname = $data->selectdeptname;

echo 'Dname: ' . $dname;

How do I retrieve the text which is sitting at index 2 instead of the number 2? Please help.

In reply to Ivn Vai

Re: Retrieve value of single form element

by Hubert Chathi -

Setting the array key will change the value that gets returned.

foreach($deptnames as $deptname)
{$deptlist[$deptname->name] = $deptname->name;}
$mform->addElement('select', 'selectdeptname', get_string('selectdeptname', 'block_hierarchycreation'), $deptlist);

(You probably don't want to use the name, though.  You should probably use some other index.)

In reply to Hubert Chathi

Ynt: Re: Retrieve value of single form element

by mehmet akif -

hi to all,

I have a problem about moodle form:

 

// $mform->addElement('text','designername',get_string('designername','enrol_denizbankpos'));

// $mform->addElement('text','designersurname',get_string('designersurname','enrol_denizbankpos'));

// $mform->addElement('text','jobdescription',get_string('job_desc','enrol_denizbankpos'));

// $mform->addElement('text','employeenamesurname',get_string('employeenamesurname','enrol_denizbankpos'));

// $mfrom->addElement('text','designerfeeperminute',get_string('designerfeeperminute','enrol_denizbankpos'));

 

when I try to add this fields into mfrom,my form does not appear,it does not work,what can I do?this is simple addition to moodle form.thanks in advance.