I am trying to open a form which will take in a picture (from the camera) and than send it to my controller in laravel.
Does any body know why this is not working?
Form in laravel:
<div style="width: 0; height: 0; overflow: hidden;">
{{ Form::open(array('route' => 'upload_photo', 'files' => true)) }}
<input type="file" onchange="this.form.submit();" id="camera_input" name="camera_input" accept="image/*" capture="camera" />
</div>
<button class="btn btn-primary btn-lg normal-font" type="button" onclick="takePicture();">Take Picture And Upload</button>
{{ Form::close(); }}
Javascript which will simply just open the camera/file select on click:
function takePicture() {
document.getElementById("camera_input").click();
}
Laravel controller:
All I am doing is this to try to see if the file gets put over here:
public function upload_photo() {
$file = Input::file('camera_input');
var_dump($file);
}
It is getting NULL printed out for $file though...
Anyone know what I am doing wrong?
I got it apparently my input type needed:
enctype="multipart/form-data"
I thought have files => true on the form for laravel would do this and thats what that was for but apparently it needed to be on the input type.
Maybe because I didnt use laravel's Form::file()
Related
I'm still a beginner with Laravel. I wanted to do the usual "are you sure you want to delete this" question as a modal. I'm trying to do it with JS, and I have the following function that I found on this very website.
function confirmClick(){
let url = "{{route('comics.destroy', ':id') }}";
url = url.replace(':id', idComic);
window.location.href = url;
};
But when I use this what I get is the show route. This is what I have in my Controller
public function destroy($id)
{
$comic = Comic::findOrFail($id);
$comic->delete();
return redirect()->route('comics.index');
}
And the html
<div class="hidden" id="deleteModal">
<h2>Sei sicuro di volerlo eliminare?</h2>
<button type="submit" class="btn btn-danger" id="yesBtn">Sì</button>
<button type="submit" class="btn btn-info" id="noBtn">No</button>
</div>
Do i have to add in some way a method and the token? In case I have to, how do I do this? Sorry if this might look confusing, but it comes from a really confused mind at the moment lol
I suggest you use a different approach, mostly because Laravel's default route for DELETE requieres a POST.
This is a code I use very often. You have a form with the method and token, and a button with a class "deletebutton".
If you click the button, it triggers the "confirmation" from this library https://bootstrap-confirmation.js.org/. You can use another library, or use your own implementation returning a true/false value to evaluate and then decide if you submit the form or not.
if the user confirms, it looks for the form and then triggers the "submit"
<form class="form_delete" action="/record/{{ $record->id }}" method="POST">
{{ csrf_field() }}
{{ method_field('DELETE') }}
<button type="button" class="btn btn-sm btn-danger deletebutton"><i class="fa fa-trash"></i>
</button>
</form>
<script>
$(document).ready(function () {
$('.deletebutton').confirmation({
rootSelector: this,
container: 'body',
popout: true,
placement: 'top',
singleton: true,
onCancel: function () {
},
onConfirm: function (value) {
var myform = $(this).closest(".form_delete");
myform.submit();
}
});
});
</script>
Here is my code :
app.get("/editar-equipo?:id", (req, res) => {
const equipos = obtenerEquipos()
let equipoSeleccionado
for(let i = 0; i < equipos.length; i++){
if(equipos[i].numeroId === Number(req.query.id)){
equipoSeleccionado = equipos[i]
}
}
res.render("edit-team", {
layout: "header",
data: {
equipoSeleccionado
}
})
})
And my handlebars html is
<td>
<form id="editar-equipo" method="GET" action="/editar-equipo?id={{equipo.numeroId}}">
<button type="submit" class="btn btn-warning">Editar</button>
</form>
</td>
and the buttons are created well:
<form id="editar-equipo" method="GET" action="/editar-equipo?id=58">
<button type="submit" class="btn btn-warning">Editar</button>
</form>
But when i click the button it doesn't work. It loads
> http://localhost:3030/editar-equipo?
But if i manually write http://localhost:3030/editar-equipo?id=58 in the url bar it works correctly and the teams loads normally.
Any help? Is there any other parameter missing? Or something like that?
Im using express-handlebars and express
I have already test your case. Browser delete all query after question mark. So you need to put an hidden input inside your form element:
<form id="editar-equipo" method="GET" action="/editar-equipo">
<input type="hidden" name="id" value="58">
<button type="submit" class="btn btn-warning">Editar</button>
</form>
I have created an example of what I am trying to accomplish:
I have an index page with a form + input elements (example.php).
When submitted I call a php page via jQuery that processes the form data and outputs result in div on index page (proces.php)
From within this php process page loaded in the div I have a button that when clicked I want to save $_POST data + other values from that page into MySQL.
I simply just cannot figure out how to "re-use" $_POST data and new values from example.php file and process.php file so I can save it from proces page when clicking button on proces.php page.
This is the example: http://mazey.dk/example.php
When form on index page is submitted the proces.php is called and here all POST data is parsed. This data I want to save when clicking Save button in proces.php file.
I hope someone can guide me in the right direction.
Thanks
***** UPDATE *****
Let me break down the code I have:
On example.php page I have a POST form with an action and a jQuery/AJAX script that prevents form from submitting in a normal way, instead it sends POST to form action URL. Result is then shown inside the with .previews class.
<script>
$(document).ready(function(){
$('.selectform').ajaxForm( {
target: ".previews",
//success: function(res) {
//$( "#currentOrders" ).load(window.location.href + " #currentOrders" );
//}
});
});
</script>
<form action="proces.php" class="selectform" method="post">
<input name="test" type="hidden" value="123">
<div class="form-group col-md-9">
<input name="inputtest" type="text">
</div>
<div class="form-group col-md-9">
<div class="">
<button class="btn btn-primary btn-lg" type="submit"><i aria-hidden="true" class="fa fa-calculator"></i> Calculate</button> <button class="btn btn-default btn-outline btn-lg" type="reset">Reset</button>
</div>
</div>
</form>
<div class="col-md-6 col-lg-6 col-xl-6 col-xxl-6">
<div class="previews"></div>
</div>
Inside proces.php file (that displays in the .previews ) I have a button. This button I want to trigger a "save-action" so when clicked I can save POST values + other variables that will be defined in proces.php into the MySQL DB. I have no problem in handling functionality to MySQL, but I need to figure out how to call POST data + variables upon clicking the button.
Proces.php file:
<input type="submit" name="save" value="SAVE" class="btn btn-lg btn-default">
<?
print_r($_POST);
$newTest = "Check";
$data = 234;
// TEMPERATURE CONTROLLER
$controller = explode("|",$_POST[controller]);
$controllerCostEach = $controller[1];
$controllerName = $controller[2];
$controllerCost = $controller[1];
$qtyController = $controller[0];
$controllerAssemblyCost = $controller[3];
$totalControllerAssemblyCost = $controllerAssemblyCost * $qtyController;
$controllerCost = $qtyController * $controllerCost;
//SUBMIT BUTTON CLICKED
//E.g if(save).clicked{
echo "HERE I can save original $_POST data + other variables from $_POST values from index file and this proces.php file via standard mysqli_query";
//}
//SUBMIT
?>
I hope with this explanation that someone can help me :-)
I've been trying to push through a PHP variable $getUserID to a JavaScript function that doesn't seem to accept it. I've been looking through a lot of answers on stackoverflow as well as the internet in general but I haven't found a solution yet.
Code:
PHP :
<!-- Comments Form -->
<div class="well">
<h4>Leave a Comment:</h4>
<form role="form" method="post">
<div class="form-group">
<textarea class="form-control" rows="3" id="commentInput"></textarea>
</div>
<button type="submit" onclick="checkComment(<?php echo $getUserID ?>)" class="btn btn-primary">Submit</button>
</form>
</div>
JavaScript :
var comment = {
commentText: "",
userID: ""
};
function setValues() {
comment.commentText = document.getElementById("commentInput").value;
}
function validateComment(commentText) {
if(!(commentText === "" || commentText.length < 1))
return true;
}
function checkComment(userID) {
setValues();
alert(validateComment(comment.commentText));
comment.userID = userID;
alert(comment.userID);
alert(comment.commentText);
}
What I'm trying to do, for testing purposes, is to print alert messages to see if every variable is received. No alert messages pop up when I hit the submit button. I believe it is an error due to the php variable. Is anyone able to spot the error?
<button type="submit" onclick="checkComment(<?php echo $getUserID ?>)" class="btn btn-primary">Submit</button>
I wasn't using single quotations after "checkComment(".
Fixed code:
<button type="submit" onclick="checkComment(<?php echo $getUserID ?>')" class="btn btn-primary">Submit</button>
Is it possible to update a razor variable using onclick on a element?
MVC VIEW:
#{
string iconNumber = "1";
}
<button type="submit" onclick="#iconNumber = '2'; alert('#iconNumber');"></button>
Console error:
Uncaught ReferenceError: Invalid left-hand side in assignment
Thanks
I am not able to understand from your comments for what purpose you trying to update the razor variable, but if you really wants to update your razor variable from 1 to 2 (from your question), then i would suggest to try like below
#{
int iconNumber = 1;
}
then,
<button type="submit" onclick="#(iconNumber++); alert('#iconNumber');"></button>
Hope it helps...But also note that iconNumber will increment on each click of button. So, I can help you if you show some more code
When reading this question the day after, one would think I was under the influence of drugs.
I came up with a better solution that only uses javascript variables:
VIEW:
<script>var iconNumber = 0;</script>
#using (Ajax.BeginForm("", "",
new AjaxOptions
{
OnBegin = "$('#iconElement' + iconNumber).attr('class', 'fa fa-spinner fa-spin');",
HttpMethod = "POST"
}))
{
Html input here
<button type="submit" class="btn btn-danger" id="IconElement1" onclick="iconNumber = 1;">
<button type="submit" class="btn btn-danger" id="IconElement2" onclick="iconNumber = 2;">
<button type="submit" class="btn btn-danger" id="IconElement3" onclick="iconNumber = 3;">
}
I can now disable the form after submit to prevent multiple requests. Because I don't start the spinner directly in onclick I don't have to disable the buttons, (spinner wont start).