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
Related
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.
I have a form which upon submit must redirect to another form to complete further details.
My problem is that i cant find out how to pass the post variables from the first form to the second.
In my controller I have something like this :
$urls = $_POST['app_frontbundle_urls']['urls'];
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
// var_dump($urls);die();
return $this->redirect($this->generateUrl('purchase_new'));
I cant use an array to pass the variables in GET as I absolutely need these variables in post as there will be a hidden field and a textarea
How can i do this ?
My var_dump here does print my variables but how to get it to the other controller?
Thanks for your help
Why don't you store them in the session or the request ? And you flush the session/request once you used them.
If you absolutely must have access to the data from earlier form submissions in order to process the next form submission I would use a Controller Event Listener to load the data you just saved and place it inside a service on the Container.
StackOverflow answer explaining how to do this
Symfony Cookbook article on how to do this
However, I would recommend that you redesign your form so that it doesn't need to know the data that was posted previously in order to process itself. Having forms that depend on one another's input is problematic because validation will depend on what was posted previously. If you just need to kick off a process at the end of the form process then just save each result, validating as you go through the various controllers and then in the last form do the extra processing as well as validating that the other forms were completed.
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');
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);
My problem is I have an HTML page that includes a short form on it. What I'd like is when this page posts/gets into my Yii model form, to be able to grab and pre-populate the empty form for the model with the values from the incoming form...
I dont think I can use the pagination widget because my initial page is HTML. Is there some way I can just pull this POST value in if it's set rather than the model.
Im still new to Yii so if this is a simple answer, I apologize
Edit: To clarify, my initial form has a few values from my main page form. The HTML page has 3 fields, say first name, last name, email address. That form then posts in to my full page form, which asks for additional information to complete the model. I'm hoping though that I can pre-populate the first and last name in the new php/yii form.
Basically I'm hoping when I first render _form.php I can grab a GET/POST value and assign it to the current model, or add it as a default value on the form.
"I'm hoping ... I can grab a GET/POST value and assign it to the current model"
You sure can. Do this in your controller and you'll be set:
$model->first_name = $_GET['first_name'];
Of course, you may also want to validate those values and set a model scenario to make sure you don't end up with bad data being passed in by an attacker. Otherwise you could run into XSS attacks. But the ability to directly assign model attributes is nice and powerful ...