How to catch the displayed image ID in Laravel - html

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');

Related

Implement copy button functionality

I have this Bootstrap code which I would like to use to generate address and implement copy button functionality:
<div class="modal fade" id="bitcoinModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="container">
<div class="offset-top-20 text-md-left">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Copy address</h3>
</div>
<div class="section-60 offset-top-35">
<div class="offset-top-20 text-md-center">
<form class="rd-mailform form-inline-custom text-left" data-form-output="form-output-global" data-form-type="subscribe" method="post" action="http://.........">
<div class="form-group form-group-outside">
<div class="input-group">
<label class="form-label form-label-outside text-dark" for="forms-subscribe-email">Bitcoin Address</label>
<input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="#Required"/>
</div>
<div class="input-group-btn">
<button class="btn btn-width-165 btn-primary" type="submit">Copy</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
How I can copy the content from the input item and close the form when the value is copied into the clipboard?
You can use something like https://clipboardjs.com
p.s. To modify the state your site, you can use the success callback to modify the state of your site.
<script src="https://cdn.jsdelivr.net/npm/clipboard#2/dist/clipboard.min.js"></script>
<input class="form-control" id="forms-subscribe-email" type="text" name="bitcoin_address" value="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy " data-constraints="#Required" />
<input type="button" value="copy" id="copy-button" data-clipboard-text="3J98t1WpEZ73CNmQviecrnyiWrnqRhWNLy" />
<script>
var clipboard = new ClipboardJS('#copy-button');
var button = document.querySelector('#copy-button')
clipboard.on('success', function(e) {
button.value = "copied"
});
</script>
I showed you how to change the button text. Closing the modal should be similar.. Without knowing any details.. you probably have to remove or add a class to the modal to make it disappear.
For the delay you can use setTimeout() https://www.w3schools.com/jsref/met_win_settimeout.asp

Submit form to websocket

I'm trying to submit some simple log in data to a server. All of the requests and functions are working great for "testuser." I want people to be able to submit their username and password for consideration to the server (it all gets encrypted, and I can't see it.)
I can see where the registration request submits generic data "testuser" for username and pass. But I have no idea how to get it to accept user login information instead.
<!--This is where I'm attempting a login form-->
<form action="/action_page.php">
<div class="imgcontainer">
<img src="img_avatar2.png" alt="Avatar" class="avatar">
</div>
<div class="container">
<label><b>Username</b></label>
<input type="text" placeholder="Enter Username" name="uname" required>
<label><b>Password</b></label>
<input type="password" placeholder="Enter Password" name="psw" required>
<button type="submit">Login</button>
<input type="checkbox" checked="checked"> Remember me
</div>
<div class="container" style="background-color:#f1f1f1">
<button type="button" class="cancelbtn">Cancel</button>
<span class="psw">Forgot password?</span>
</div>
</form>
<!-- This is rock-solid code from the gamesparks API. Works flawlessly. -->
<button onClick='gamesparks.registrationRequest("testuser", "testuser", "testuser", registerResponse)'>Register</button>
<button onClick='gamesparks.authenticationRequest("testuser", "testuser", loginResponse)'>Login</button>
<button onClick='gamesparks.accountDetailsRequest(accountDetailsResponse)'>Account Details</button>
<button onClick='customEvent()'>Custom Event</button>
<button onClick='customEvent2()'>Custom Event 2</button>
<button onClick='testRT()'>Test RT</button>
<i>Special thanks to the awesome team at GameSparks!</i>
<div id="messages"></div>
<br/>
<br/>
All of my buttons work, spitting out data from my backend regarding "testuser." I know it works because I can alter my data and verify.
You'll notice "testuser" occupies three spaces for one of the onClicks. One is username, the other is password, and the third is displayname (in that order.)
Displayname can equal Username...if anyone wants extra credit.
But mostly, I want my modal to input the data and submit it for my buttons.
Again, I solved my own question! What I did was put my gamesparks function inside of another function. I learned this was called "nesting." I also defined variables as part of the onClick, which were then used in the function. The solution I used is below. Forgive my n00bish comments. But they help me learn.
<div class="container">
<h2>Click To Login</h2> <!-- This is a label above the button -->
<!-- Trigger the modal with a button -->
<button type="button" class="btn btn-info btn-lg" data-toggle="modal" data-target="#myModal">Martial Parks</button>
<!-- Modal -->
<div class="modal fade" id="myModal" role="dialog">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h4 class="modal-title"><img src="http://martialparks.com/wp-content/uploads/2017/10/cropped-mp_logo01-2.png" class="center-block img-circle logo-margin-negative"></h4>
</div>
<div class="modal-body">
<form>
<div class="row">
<div class="form-group col-sm-5 col-sm-offset-4">
<label class="sr-only" for="exampleInputAmount">Username</label>
<div class="input-group">
<div class="input-group-addon"><span class="glyphicon glyphicon-user"></span></div>
<input type="text" class="form-control" id="username" placeholder="Username">
</div>
</div>
<div class="form-group col-sm-5 col-sm-offset-4">
<label class="sr-only" for="exampleInputAmount">Password</label>
<div class="input-group">
<div class="input-group-addon"><i class="fa fa-key"></i></div>
<input type="text" class="form-control" id="password" placeholder="Password">
</div>
</div>
<button type="button" class="btn btn-danger btn-block" data-dismiss="modal" onClick="login()">Login</button>
<!-- There is a line of code that closes the modal once the button is clicked: data-dismiss="modal" I've removed it from the register button. -->
<!-- Also, I can call up multiple functions using this format: onclick="doSomething();doSomethingElse();" -->
<button type="button" class="btn btn-danger btn-block" onClick="register()">Register</button>
<script>
function login() {
var usernameinput = document.getElementById('username').value;
var passwordinput = document.getElementById('password').value;
console.log(usernameinput);
console.log(passwordinput);
gamesparks.authenticationRequest( usernameinput, passwordinput, loginResponse);
}
function register() {
var usernameinput = document.getElementById('username').value;
var passwordinput = document.getElementById('password').value;
console.log(usernameinput);
console.log(passwordinput);
gamesparks.registrationRequest( usernameinput, usernameinput, passwordinput, registerResponse)
}
</script>
</div>
</form>
</div>
<div class="modal-footer">
<div class="row">
<div class="col-sm-5 col-sm-offset-4">
<div id="messages"></div>
<br/>
<br/>
You need to manually grab each of your fields values. The most direct way is via document.getElementById and value:
document.getElementById( "username" ).value
In your example:
<button onClick='gamesparks.authenticationRequest( document.getElementById( "username" ).value, document.getElementById( "password" ).value, loginResponse )'>Register</button>
I would, however, suggest calling a function instead of inlining it:
<button onclick="login()">Login</button>
⋮
⋮ // Before </body>
⋮
<script>
const fetchValue = id => document.getElementById( id ).value;
function login() {
const username = fetchValue( "username" );
const password = fetchValue( "password" );
gamesparks.authenticationRequest( username, password, loginResponse );
}
</script>
Also1: You should never need to place an api secret in client-facing HTML/JavaScript.
Also2: Regarding your these lines:
<var username = username>
<var password = password>
These are invalid. <var> provides styling and nothing else. It looks like you are attempting to place JavaScript there? To do so, you need to either use <script> tags or event handlers, such as onclick.

How to pass Bootstrap Modal form data to Spring Boot controller

If I have a pop up, opened using Bootstrap Modal, and in this i have a form where i can put data, how can I pass this data to a Spring controller to update my sql DB?
I had the same issue and i find the solution using ajax, because every time you submit the values your page is refreshed and the modal is closed.
Here is my modal form:
<!-- Modal -->
<div class="modal fade" id="loginModal">
<div class="modal-dialog">
<!-- Modal content-->
<div class="modal-content">
<div class="modal-header" style="padding:35px 50px;">
<button type="button" data-dismiss="modal" class="close">×</button>
<h4><span class="glyphicon glyphicon-lock"></span> Iniciar sesion</h4>
</div>
<div class="modal-body" style="padding:40px 50px;">
<form id="login-form" action="${contextPath}/j_spring_security_check" method="POST" name="loginForm">
<div class="form-group">
<label for="usrname" ><span class="glyphicon glyphicon-user"></span> Correo</label>
<input type="text" class="form-control" name="correo" id="correo">
</div>
<div class="form-group">
<label for="psw"><span class="glyphicon glyphicon-eye-open"></span> Contraseña</label>
<input type="password" class="form-control" name="password">
</div>
<div class="checkbox">
<label><input type="checkbox" value="" checked>Remember me</label>
</div>
<div class="form-group">
<button type="submit" id="loginBtn" class="btn btn-success btn-block"><span class="glyphicon glyphicon-off"></span> Iniciar sesion </button>
</div>
</form>
<hr/>
<div id="errorMsg">${error}</div>
</div>
<div class="modal-footer">
<p>¿No tenes cuenta? Registrate</p>
<p>Olvidates tu contraseña?</p>
</div>
</div>
</div>
</div>
Ajax call:
$('#login-form').on('submit', function(event){
var self = this;
var form = $(this);
var errorMsg = $('#errorMsg');
if (form.data('requestRunning')) {
return;
}
form.data('requestRunning', true);
event.preventDefault();
$.ajax({
url: form.attr("action"),
type: form.attr("method"),
data: form.serialize(),
success: function(result){
console.log(result.login);
if(result.login == undefined){
self.submit();
}else{
errorMsg.text(result.login.FAILURE).addClass("alert alert-danger");
agitar('#errorMsg');
}
},
complete: function (e) {
form.data('requestRunning', false);
}
});
});
Here i'm using spring-security to validate my user login, but if you only want to update your DB, in the form action you can put the url that you want to use to call your controller i.e: "/updateDB" and declare a controller i.e:
#RequestMapping("/updateDB", method=RequestMethod.POST)
public String updateDB(#ModelAttribute("dataToUpdate")DataToUpdate dataToUpdate){
//do whathever you want
}
Hope this help!

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

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

Passing parameter to bootstrap modal button

I have a delete link , for that I am using bootstrap modal window . Here window appearing fine When I click yes button of window the record should be delete. So I have to call servlet in yes button along with a parameter Id here I am able to call servlet but parameter is not passing. How can I pass parameter to yes button of modal window. Here is code for it.
The bootstrap modal where I am calling,
<td>Edit
</td>
<td> Delete
</td>
in the same jsp page the markup of modal is,
<div class="modal fade" id="basicModal" tabindex="-1" role="dialog" aria-labelledby="basicModal" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title" id="myModalLabel">Delete Teacher Record</h4>
</div>
<div class="modal-body">
<h3>Are you sure?</h3>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">No</button>
<!-- <button type="button" class="btn btn-primary">Save changes</button> -->
<a href="<%=request.getContextPath()%>/controller/TeacherManagementController?flowName=DELETE_TEACHER_INFO&teacherId=${teacherId.getTeacherId()}"
class="btn btn-primary">Yes</a>
</div>
</div>
</div>
</div>
The problem I am facing is the modal is appearing but record is not delete because of not able to pass the teacherId. Please any one help me in this.
I am using like this call function and assign value to hidden fields
Html:
<div id="add_button" >
<a data-toggle="modal" href="#myModal_new" onClick="pop_up('<?php echo $section_value['subject_name']; ?>','<?php echo $section_value['section_id']; ?>')">Add Subjects</a>
</div>
<script>
function pop_up(name,id){
var standard = document.getElementById("standard");
var standard_id = document.getElementById("standard_id");
var standard_id_new = document.getElementById("standard_id_new");
standard.value = name;
standard_id.value = id;
standard_id_new.value = id;
}
</script>
<div id="myModal_new" class="modal hide fade">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">×</button>
<h3>Create Sections</h3>
</div><br><br>
<div id="errorContainers">
<p>Please correct the errors and try again:</p>
<ul />
</div>
<div class="modal-body" style="margin-left: 17%;">
<!-- Modal Form Starts Here -->
<form name="modalForm" id="formss" method="post">
<input type="hidden" name="standard" id="standard">
<input type="hidden" name="standard_id" id="standard_id">
<input type="hidden" name="standard_id_new" id="standard_id_new">
<fieldset>
<div class="division_center"><div class="division_left"><label class="form-signin-signup" for="input1">Sections </label> </div>
<div class="division_right">
<input type="text" name="standard_new" id="standard_new"> </div></div>
</fieldset>
<!-- Modal Form Ends Here -->
</div>
<div class="modal-footer">
<a class="btn" data-dismiss="modal" >Close</a>
<input type="submit" name="newsubmit" class="btn btn-primary btn-large" value="Create">
</div>
</form>
</div>
</div>
</div>