Moodle Custom Form updating - php

I have created custom form in Moodle and inserted the data into moodle database. Also i used most of the field in form as autocomplete like below eg
$mform->addElement('autocomplete', 'SBName', get_string('searcharea', 'search'), $sbnames, $options);
Then, I need to update this form . I got record from db as a array, Then i need to add db values in my update form to proceed further.
Here I could not populate the values in edit form for the field of autocomplete $mform->addElement('autocomplete.
Kindly help on this to apply the values in edit form.
Please let me know Is there any way to do this in moodle

A few things to note - in Moodle variables and database field names should all be lowercase, so you will find things easier if you call the field 'sbname' instead of 'SBName'.
You mention you have "got the record from the db as an array" - none of the Moodle database access functions return a record as an array, they all return records as objects (although they will return multiple records as an array of objects) - please double-check the code you are using to retrieve the record, if it is reaching you as an array.
Finally, to initialise values when editing a form, the usual method is to write (outside the form itself):
$form = new name_of_form();
$form->setData($recordfromdatabase);
Where $recordfromdatabase is the record object returned by one of the $DB->get_record_* functions.
After that, you can use the $form->getData() function to retrieve the submitted values and $form->display() to display the form itself.
There are lots of examples in the core Moodle code that you can follow for this.

Related

PHP Loading MySQL Columns For a UniqieID Then Updating Columns

Sorry very new to PHP and MySQL. I'm pretty close to having what I need. I created a database that hold values as shown here:
I was able to find some things online to create a form and initially get the data into the database
What I would like to do is have another page to load values for a specific customer by searching for their uniqueid. So have a input box where you input the uniqueid then the current values for that customer get loaded into a form just like the "Customer Creation Form". Then be able to update the loaded values in the the form and save it back. The enddate is probably the field that would be updated the most.
You could use Ajax requests and PHP to update the form live (or update once the user has entered the UniqueId)
https://www.w3schools.com/php/php_ajax_php.asp
To return multiple values grabbed from the database, use php to create an array return it as a json file.
https://www.w3schools.com/js/js_json_intro.asp
https://www.w3schools.com/js/js_json_arrays.asp
https://www.w3schools.com/js/js_json_php.asp

Laravel Database Updates with form data

All,
Just trying to see what the best option would be to update a database row with data submitted in a form. I understand the Laravel update query builder, but I am stumped on how to get form data, and then to execute that query. Relatively new to Laravel :) Here is what I had come up with, just from my PHP experience and logic:
I had tried to put the query into a function and then have the form action be that funtion:
function editsop(){
// query
}
Like I said, main problem is trying to get the named form objects to be the thing that will update the row. I have 2 text input fields, respectively named "body" and "notes." So the data there would be what is updated into the DB. Any help is greatly appreciated!
function editsop(Request $request){
// this below allows you to get the value of the input from the form
regardless if its a post or get
$request->input('name of input from form');
}
this has all the details you'll need
laravel 5.3 Requests

CodeIgniter set_value() is poplating values in a different form

I have two forms in a single page
Create Record Form
Update Record Form
Create Record form works perfectly but when I use the Update Record form the values are populated in the Create Form when an error occurs. What can be done to prevent this or any better structure to be followed for creating such functionality.
#Dharma Sai Teja - Please make sure that the names provided to the fields should be unique in all form. The values will be populated wrong if the

how to show a value from a database in an input field in CakePHP

I liked to know how I can show a value from my database into an input field. I'm working with CakePHP.
Right now, I have some empty fields, but I want them filled with the database values when I display my page the first time and of course when there are values in the database relative to these fields. I'm able to retrieve all my database data, but unable to show them into the fields.
Is there an easy way with CakePHP to do it or I need to set each field individually in my controller and retrieve it in my view.
Thank you
You are able to fill forms from the controller, by using $this->form->data.
Example:
$this->request->data = $this->YourModel->findById($id);
Edit:
You have to create your form with the form helpers to attach the form to your model, otherwise it won't work.
echo $this->Form->create('YourModel');

Multi-select does not retain value after post in codeigniter

I have a form in my application with a multi select. I'm using CI's form helper to build my forms, so the build of the element looks like this:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
$pre_selected, $additional_attributes);
This is all well and good if the items are in the database ($pre_selected gets existing responses). However, I'm also running the form through CI's form validation, and when that happens, if validation fails, then the multi select loses the values that had been selected.
I'm sure this is something simple that I'm just over looking, so hopefully someone can help me out here.
Adding more information
The field is marked as required so it is going through the validator (although it will always pass as I'm automatically selecting the current user).
(I'm assuming $pre_selected is an array of values?)
You can reset selected values after failed form submit using the $_POST array.
Since you're already using $pre_selected, you should be able to use the following:
return form_multiselect('authors[response][]', $faculty->get_all_for_multiselect(),
array_unique(array_merge($pre_selected, $_POST['response'])), $additional_attributes);

Categories