How can i get data from database for dropdownlist in Form in Laravel using laravel collectives? - laravel

I have used usual dropdown list and it's not worked,
when i click the submit button for all the data in the form except drop down value were sent to database.how can i fix it??

Your <select> is missing a name="foo" attribute.
A form will only submit elements with a name as part of the request.

Related

How to reload page with hash url using Laravel form select

I'm working on a laravel project and I notice a statement like this in a blade file:
{{ Form::select('chapter', $chapters, $chapter_id) }}
I see a drop down when I visit the page in my browser. The moment I change the drop down, the page refreshes and I see ?chapter=<some value> query string in my browser. My guess is that the <select> onchange event triggers a form submission, which is default Laravel behaviour
I want to change the behaviour such that form submission/page reload will create the following query string/url components : ?chapter=<some value>#searchresults. What's the Laravel approach to include the #searchresults hash phrase as part of the form submission / page reload?

Django add fields to form dynamically based on number of values in model choice field

Objective:
From a Django Form, modelchoice field i am making the widget display multiple checkboxes. But for each checkbox I would like to display exactly one textbox and then submit. I need to know if the checkbox is not selected, it's id still and the possible textbox value. How do I acheive this, if it is Ajax. please elaborate. as I am fairly new to django and haven't worked much with ajax.
So you have to possible approaches here,
Simpler(but a very tardy approach):
Submit the form after user's checkbox input, process the input in views.py and accordingly serve the other part of the form on a different template. This will lead you to reloading the page and changing URL's for the same form. This approach is fine if you are doing it only to learn Django at first.
The better approach. You can use on page JavaScript/JQuery to determine whether the checkbox is ticked or no and the accordingly show the checkbox. You can do something like
if(document.getElementById('yourCheckBoxID').checked)
{
$("#FormFieldID").show();
}
else
{
$("#FormFieldID").hide();
}
If you are doing the latter, do remember to NOT set the input field as "required", as it may throw up errors when not showing the text field. Use some sort of JS form validation if you have to.
Hope this helps!

Prevent from changing the page when there are form validation errors

I have few forms with several validation rules in a separated file named form_validation.php. One of them is an edit form that has the URL /usuario/painel/editar that receives the user id from the session, populating the form without the need to pass the id by GET, for security reasons.
The problem is that when I submit the form, the action is different, and when there are any errors, the URL changes to the action, making several errors to show when rendering the form, since the view is different and there are no id to receive.
<form action="/auto/usuario/edit/<?php echo $id_usuario; ?>" method="POST" id="editar_formulario_fisica_1" enctype="multipart/form-data">...</form>
For example: localhost:8000/auto/usuario/edit/8
Is there a way to refresh the same page and the same form, and show the errors? Or perhaps to prevent the URL from changing when there are errors?
put the id in a hidden form field
echo form_hidden('userid',$id_usuario);
then get the user id in your controller or model
$userid = $this->input->post( 'userid') ;
edit ======
yes the URL can now be the same, because you are not adding the user id to the end of it. you don't need to because the user id is being passed in a hidden form field.
the form itself is in a view file. to show the form, you are first getting the user id, then you are calling/loading the form view file. in a similar way, if the form validation fails, then you are showing the form view file again. this all happens in your controller. also if you use codeigniter form helper, and form validation, it makes showing the error messages, etc, much easier.

Multiple submit buttons with unique ids

In my application, I am trying to create a form that has some text fields and a collection of attachments. The user will have the ability to load an existing form from the database, and edit the fields and also add/remove individual attachments from the attachment collection.
For this purpose, in my view I have a foreach loop that renders the attachment details (id, name) AND also a submit button to remove the attachment. There is also a main submit button at the bottom of the form that the user can click once they have finished editing the form.
I have followed this solution to implement multiple submit buttons in my form. However the problem I have is when the user wants to remove an attachment, I would need to pass the attachment ID as a parameter to the "RemoveAttachment" action when the user clicks the submit button next to the attachment. But as it is, the application passes the main form ID to the RemoveAttachment action. Is there a way to pass the attachment ID when the user clicks the button?
Many thanks
Using a little javascript, first add a hidden field to your form:
<input type="hidden" name="attachmentID" id="attachmentID">
Then, on each of the submit buttons:
<input type="submit" onclick="document.getElementById('attachmentID').value='#ATTACHMENT_ID'" />
Of course, you replace ATTACHMENT_ID with whatever variable contains the id.
You can make a separate <form> for each remove button, with a hidden input (or URL parameter) specifying the ID.
Beware that <form>s cannot be nested.

codeigniter form prepopulation

Is there a method to prepopulate a certain field in a form using data from the previous page using codeigniter.
I have a page with input fields that are filled out, if the user presses a "add" button they are given a new form with similar fields. I want those fields to be populated from data received by the previous page.
You can use CI's form helper function set_value() which uses the $_POST and may save you some code testing if the $_POST value is empty because you can specify a default value if not set. Should be using this anyway if your using CI's form validation.
Can't you simply use the $_POST data to display the second form?
You could store the data entered in form 1 in sessions or in the database and display it in the second form. Or as mentioned use $_POST in the second form to retrieve submitted data from the previous.

Resources