can't set html form input file from modal / popover - html

I have an html form. For the image file input, I have a modal that pops up to set the input field but also allow the user to crop. When I submit the form, it says the input field is empty.
Here is the form with modal embedded:
<form class="form-horizontal" role="form" action="" method="post" enctype="multipart/form-data">
{% csrf_token %}
<h5>First Name</h5>
<div class="form-group">
{{ form.first_name|attr:"class:form-control"|attr:"placeholder:Enter Your First Name" }}
</div>
<h5>Profile Image (Square Image)</h5>
<div class="form-group">
<button data-toggle="tk-modal-demo" data-modal-options="slide-down" class="btn btn-primary">Upload</button>
</div>
{{ form.crop_coords }}
<!-- Create the modal dynamically via Handlebars -->
<script id="tk-modal-demo" type="text/x-handlebars-template">
<div class="modal fade" >
<div class="modal-dialog">
<div class="v-cell">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title">Upload Profile Image</h4>
</div>
<div class="modal-body container">
<div class="max-width: 560px;">
<div>
<img style="max-width: 100%;" id="tempImg" src="{% static 'app/images/default-team-large.jpg' %}"/>
<h5>Profile Image (Square Image)</h5>
<div class="form-group">
<input id="id_profile_image2" type="file" name="pic" accept="image/*">
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary" data-dismiss="modal">Save</button>
</div>
</div>
</div>
</div>
</div>
</script>
<div class="form-group margin-none">
<div class="col-sm-offset-3 col-sm-9">
<button type="submit" class="btn btn-sm btn-primary" href="nml-home.html"> Confirm User Changes<i class="fa fa-fw fa-arrow-right"></i></button>
</div>
</div>
</form>
To test, I click the data-toggle button to popup the modal. It then opens the tk-modal-demo which has a file upload. I upload an image and then I click the save button in tk-modal-demo to dismiss the modal. Then I submit the form with the bottom form submit button. My validator says that the field is empty.
How can I set this required form field from the modal and have it populated when I submit?

It might be that the modal input elements are not created inside the <form> element. Input elements outside of this container will not be sent to the server upon submit.
You can first check out what is actually transmitted with the development console of your browser (in Chrome it's the Network tab). If your fields aren't there, then you might need to write a bit of Javascript, to either:
copy the contents of the input elements inside the modal to hidden fields within the form, for using the regular submission process
or, hijack the submit event and manually compose which fields you want to send

Related

Form Fields Not Fitting Bootstrap Modal

I am trying to implement a bootstrap modal form in my Django project. When I do this, everything works except the fields are running much wider than the modal window and seem strangely offset. I feel like there's an easy fix here but I can't determine exactly what I'm doing wrong. Here is my html for the modal form:
modal.html
<form method="post" action="">
{% csrf_token %}
<div class="modal-header">
<h5 class="modal-title">Create new Book</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
{{form.Reference_Number|as_crispy_field}}
{{form.Ultimate_Consignee|as_crispy_field}}
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="submit-btn btn btn-primary">Create</button>
</div>
</form>
Try placing each crispy form field inside a Bootstrap form-group <div> container. Like this:
<div class="modal-body">
<div class="form-group">
{{form.Reference_Number|as_crispy_field}}
</div>
<div class="form-group">
{{form.Ultimate_Consignee|as_crispy_field}}
</div>
</div>
More on info on Bootstrap forms here

How to catch the displayed image ID in Laravel

I'm displaying a images from the database and i need to catch the image id from the controller .I'm trying to save image ID to Database
This is the image tag with contain image id
What we need to do is : When the user click the one of image and submit need to catch that selected id from the controller
$kudos->receiveuserid = $request->get('categories');
$kudos->imageid = 'how can i get image ID'
Try something like this and customize according to your requirement
#foreach($categories as $category)
<div>
<img src="{{$category->image}}" class="imgRating">
<input type="radio" value="{{$category->id}}" class="imgId" name="imageid">
</div>
#endforeach
Now in write a little jQuery
$(".imgRating").click(function(){
$(this).parent().find(".imgId").prop( "checked", true );
});
Finally, add CSS
.imgId{
display:none !important;
}
Now in your controller you will get the selected image id by $request->imageid
try this it should work. the idea here is that when you click on the image, the javascript function gets the id and assign it to a modal form then you submit. you can modify it to suit your preference.
<!-- modal to get image id store it in hidden field-->
<div id="imgModal" class="modal fade" >
<div class="modal-dialog box box-default" role="document" style="color:black;font-size:12.5px;">
<div class="modal-content">
<div class="modal-header">
<p class="modal-title">Image Form</p>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<form class="form-horizontal" action="{{ url('img/addid/') }}" method="post" role="form">
{{ csrf_field() }}
<div class="modal-body">
<div class="form-group" style="margin: 0 10px;">
<input type="hidden" class="form-control" id="imgid" name="imgid" value="">
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary btn-xs">Submit</button>
<button type="button" class="btn btn-danger btn-xs" data-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
<!-- display image code-->
<div class="col-sm-3">
<div class="form-group">
#foreach($category as $category)
<a onclick="imgfunc('{{ $category->id}}')"><img class="boxicon" src="{{ asset('kudosupload/badges/'. $category->image)}}" name="img" ></a>
<br>
#endforeach
</div>
</div>
<!-- javascript to get image id when click on image-->
<script type="text/javascript">
function imgfunc(r)
{
document.getElementById('imgid').value = r;
$("#imgModal").modal('show')
}
</script>
//controller to get input and store in database
public function saveImage(Request $request){
$this->validate($request, [
'imgid' => 'required|string',
]);
$imgid=$request->input('imgid');
$datasave=DB::table('imgtable')->insert([
'id'=>imgid,
]);
return redirect('url')->with('message','Image saved!');
}
//route
Route::post('img/addid','controller#saveImage');

form not rendering correctly in modal

I try to render a form with ajax and show it in a modal.
But the fom tags in the modal just appears next to eachother when i check the source code in my browser. How can I resolve this?
Here is my written code of the modal in:
<!-- Modal Edit client -->
<div id="editClient" class="modal fade" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">&times</button>
<h4 class="modal-title">Edit Client</h4>
</div>
<div class="modal-body">
<form id="editClientForm" action="{{ path('client_details') }}" method="POST">
{{ form_widget(form) }}
<br />
<input type="submit" class="btn btn-success" value="Save changes">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
</form>
</div>
</div>
</div>
</div>
The modal opens with the form fields and closes when cancel is clicked.
Here is the source code from the browser:
<div class="modal-body">
<form id="editClientForm" action="/ruben/avr_symfony/web/app_dev.php/configurator/client/details" method="POST"></form>
=> here comes all the form fields and buttons.
</div>
As you can see, it closes the form tag before putting in the form fields.
I found the problem.
I hade a closing table tag wich i typed as < table > instead of < / table > in my code above the modal code.
This solved the issue.

Form action refreshes the page

I am experiencing trouble with the action attribute from Form. When I submit my form data the page gets reloaded.
<form name="circuitForm" action="#idchart" class="form-horizontal" ng-submit="submitCircuit()" novalidate>
<div class="form-group">
<div class="col-md-offset-2 col-md-10 col-xs-offset-2 col-xs-10">
<button type="submit" id="submit" class="btn btn-default" ng-disabled="circuitForm.$invalid" >Run</button>
</div>
</div>
As you can see, on my Form action I refer is on the same page.
<div ng-show="isValidData()">
<div class="jumbotron jumbotron-graphLabel" id="idchart">
<h2>{{circuit.classe}} Chart</h2>
</div>
<div class="jumbotron jumbotron-Graph">
</div>
</div>
I only show this second page content when the form data is valid but when I click the run button the page gets reloaded and I lose all the input data.
Help me please...

password input on modal makes modal disappear

The html for a modal popup for a login is below (html). When a customer tries to login they must click on each input field, put the username and then password and click the submit submit button. If they click "enter" after inputting the username or login, the modal popup disappears.
Is there a way to have proceed to being logged in when clicking "enter/return" after in the password input box instead of clicking on the "Login" button. I would like both to work-- login button and hitting return after typing in the password.
<ul class="nav navbar-nav navbar-right">
<li><span class="glyphicon glyphicon-forward"></span> Login
</li>
</ul>
<div class="modal fade" id="customer-login" tabindex="-1" role="dialog" aria-labelledby="customer-login-header" aria-hidden="true">
<div class="modal-dialog">
<form action="https_link" method="POST" name="log-in">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">x</button>
<h2 id="customer-login-header">Customer Login</h2>
<p class="hidden-xs">Log in below to receive access to your existing policies and quotes. The log in information was emailed to your after purchasing your policy or saving a quote.</p>
</div>
<div class="modal-body">
<fieldset>
<input type="text" class="form-control" id="cl_username" name="username" placeholder="Username?">
<br>
<input type="password" class="form-control" id="cl_password" name="password" placeholder="Password?">
</fieldset>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<input type="submit" class="btn btn-primary" value="Login">
</div>
<div class="modal-footer">
<p>Forgot your Username or Password?<br><a class="forgot-password" href="https://www.mexpro.com/client_login/retrieve_password.mhtml?aff_id=2149&lang=en&agtdst=www">Click here to Retrieve »</a></p>
</div>
</div>
</form>
</div>
</div>
Here is a possible solution: https://jsfiddle.net/99x50s2s/73/
When the user clicks on the Login button, alert will be displayed if either User Name or Password is left blank.
I have made minor changes in your HTML and used jquery as shown below,
HTML:
<button type='button' data-toggle="modal" data-target="#customer-login" data-backdrop="static">Click here</button>
<div class="modal fade" id="customer-login" tabindex="-1" role="dialog" aria-labelledby="customer-login-header" aria-hidden="true">
<div class="modal-dialog">
<form action="https://www.mexpro.com/client_login/do/login.mhtml?aff_id=2149" method="POST" name="log-in">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">x</button>
<h2 id="customer-login-header">Customer Login</h2>
<p class="hidden-xs">Log in below to receive access to your existing policies and quotes. The log in information was emailed to your after purchasing your policy or saving a quote.</p>
</div>
<div class="modal-body">
<div id='error' class='alert alert-warning hide'></div>
<fieldset>
<input type="text" class="form-control" id="cl_username" name="username" placeholder="Username?"/>
<br/>
<input type="password" class="form-control" id="cl_password" name="password" placeholder="Password?"/>
</fieldset>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<input type="submit" class="btn btn-primary" value="Login" id="SubmitBtn"/>
</div>
<div class="modal-footer">
<p>Forgot your Username or Password?<br/><a class="forgot-password" href="https://www.mexpro.com/client_login/retrieve_password.mhtml?aff_id=2149&lang=en&agtdst=www">Click here to Retrieve »</a></p>
</div>
</div>
</form>
</div>
</div>
JS
$('#SubmitBtn').on('click', function(event){
var errorDiv = $('#error');
errorDiv.addClass('hide').empty();
var userName = $('#cl_username').val();
var password = $('#cl_password').val();
if ($.trim(userName) == '' || $.trim(password) == '')
{
errorDiv.removeClass('hide').append('User Name and Password is required');
event.preventDefault();
}
});
1.) Is there a way to make an error show up if they hit return without typing anything?
Yes. Handle the onSubmit event on the form and validate that you have entries in the fields you want.
2.) Is there a way to have proceed to next step when clicking "enter/return" -- i.e. cursor moves to the password/and submit takes
place?
Change the Login button from type="submit" to type="button" and write javascript code to handle the onClick event. By having it as type="submit" the browser automatically submits the form when the Enter key is pressed.
Bear in mind that many users expect the Enter key to submit a login form, not move them between fields. What you are trying to do goes against expected browser behavior.