Laravel Request $request->file() is always blank - javascript

I have a form that has a field to upload multiple files. So far so good. So when I select a few files and hit the submit button, the $request->file() is always empty no matter what. The name of my field is images_json.
I suspected the route being PATCH or PUT could cause this (considering I'm uploading images on create and update), I made a separate POST route to handle the uploading with its own controller action and had a 3rd party jQuery plugin handle the upload (blueimp jQuery File Upload plugin), and guess... I'm receiving an empty response yet again!
And yes I have 'files' => true in my form tag and it spits the enctype="multipart/form-data" attribute very nicely.
This is what I have in my view currently (normal file upload that relies on the submit button of the form):
<div class="form-group form-md-line-input">
{!! Form::label('images_json', 'Bilder hochladen', ['class' => 'col-md-2 control-label']) !!}
<div class="col-md-10">
<div class="fileinput fileinput-new" data-provides="fileinput">
<span class="btn green-seagreen btn-file">
<i class="glyphicon glyphicon-plus"></i>
<span class="fileinput-new">Bilder auswählen...</span>
<span class="fileinput-exists">Mehr auswählen</span>
{!! Form::file('images_json[]', ['multiple']) !!}
</span>
<span class="fileinput-preview" style="max-height: 32px;"></span>
 
</div>
</div>
</div>
And this is the action:
public function update(CreateEntryRequest $request, $id)
{
return $request->file('images_json');
}
And this would be a response:
[{},{},{}]
(Yes, I selected three pictures)
No matter what, it always comes back as blank. This is now something I am unable to figure out.

I think you sent data via ajax, so you can try to get file from $_FILES .
I have the same problem with fineuploader, and solve it when obtained data from $_FILES instead $request->file()

try put this in the top of your controller file
use Illuminate\Http\UploadedFile;

Related

Making text disappear using laravel livewire by timing out the flash message

I've written some code using laravel livewire that makes a modal appear whenever a user successfully subscribes or volunteers to join my website. I've tried writing some code that delays the modal, but now it's just making my code slower instead of making some text disappear after a few seconds. Here's the code that I've written on my blade file.
<div wire:loading.delay.short class="bg-white shadow" style="border-radius: 30px">
#if (session()->has('subscription_message'))
#include('livewire.frontend.subscribe.success')
#endif
</div>
I want the subscription_message to disappear and I've also written the subscription_message on another blade file.
<div wire:loading id="modal" class="alert alert-success" role="alert">
<h6 class="alert-heading">Thank you for your Subscription!🤗</h6>
<p>{{ session('subscription_message') }}<i class="fa fa-check"></i></p>
<p class="mb-0">Kind Regards,<br>
{!! 'Ben's Team' !!}
</p>
</div>
Here's the livewire component that controlls all the code on those two blade files.
public function save()
{
$this->validateForm();
Subscription::create([
'name' => $this->name,
'email' => $this->email
]);
$name = $this->name;
$email = $this->email;
$message = "Dear $name ! Thank you for subscribing with Us, Please check your email:($email) for more information.";
$this->resetInput();
// Sent Welcome email to subscriber
Mail::to($email)->send(new WelcomeSubscriberNotification());
session()->flash('subscription_message',$message);
}
I don't know how to timeout the flash message that appears after a user has subscribed. Can any assist me on a way to solve this problem? I want to use livewire, but if that doesn't work, I'm willing to use either Jquery or Javascript.
I've got it. On the blade file which shows the success message after clicking the submit button, I just added the following js code.
Blade file
<div id="modal" class="alert alert-success" role="alert">
<h6 class="alert-heading">Thank you for your Subscription!🤗</h6>
<p>{{ session('subscription_message') }}<i class="fa fa-check"></i></p>
<p class="mb-0">Kind Regards,<br>
{!! 'Ben's Team' !!}
</p>
</div>
JS Code
var timeout = 3000; // in miliseconds (3*1000)
$('.alert').delay(timeout).fadeOut(300);</script>

Clear form after submit image in Meteor

I’m using CFS for files upload in my Meteor App, almost everything works fine, except because when I try to upload another image, I see my previous sended image in the form, so I need to clear that form after submit the image. I've tried with .reset but it doesn't work. This is my code right now. Thanks for the help.
NewImage.html
<template name="newImage">
<div align="center">
<form align="center">
<div>
<div>
<span class="btn btn-success btn-file">
<input type="file" accept=".gif,.jpg,.png" class="myFileInputimagepub" id="image"/>
</span>
</div>
<div>
<img src="{{currentUser.profile.image}}" alt="Image" width="60px" height="60px" class="img-circle avatar-upload" value=''/>
</div>
</div>
</form>
</div>
</template>
NewImage.js
import './newImage.html';
Template.NewImage.events({
'change .myFileInputimagepub':function(evt,tmpl){
FS.Utility.eachFile(event,function(file){
fileImagespub.insert(file,function(err,fileObj){
if(!err){
var userId = Meteor.userId();
var imageurl = {
'profile.image':'/cfs/files/fileimages/' + fileObj._id
};
setTimeout(function(){
Meteor.users.update(userId,{$set:imageurl});
},2000);
}
})
})
},
'submit form':function(event,template){
event.preventDefault();
template.find("form").reset();
}
});
If the image in question is the one with class .img-circle, the issue is that its src attribute is being dynamically provided. Currently it is currentUser.profile.image. This won't clear just by resetting the form and manually clearing the image's src value would be fighting the framework.
Option 1 (Not Ideal):
If you don't want to keep the image, unset the database change made after the file upload by running something like this:
Meteor.users.update(userId, { $set: { 'profile.image': null }});
This is not ideal as it enables you to continue modifying the database with an image which may not be needed long-term.
Additionally, I'm assuming you're currently using the autopublish/insecure packages. You'll want to remove these before going public with your app as they allow any user to change the database without restriction.
Option 2:
You could save the returned value from your 'change .myFileInputimagepub' event as a ReactiveVar, then only actually run Meteor.users.update (preferably on the server using a Method) when your user submits the form. At that point you could clear the reactive variable.
Using a ReactiveVar will allow you to provide the saved URL to the src attribute via a helper, and then change the ReactiveVar's value when you wish to clear the form.
There's a simple example of manipulating ReactiveVars here: https://gist.github.com/ahoereth/a75d2d6528b1844ad503

Once I refresh the page that is getting data through a form , every thing will be gone

I've got a table of data with clickable rows which will lead to another page based on the row that was selected. To get the row, I am using a form and post the form to another page to pass the variable to php. Now, the problem is once I refresh the page my info is gone...
How can I avoid that?
<?php
$id = $_SESSION["uid"];
$sql = "select * from std_cources join courses where std_cources.std_id = '$id' and courses.course_id = std_cources.course_id";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '
<div class="col-md-4">
<span class="fa-stack fa-4x">
<i class="fa fa-circle fa-stack-2x text-primary"></i>
<i class="fa fa-shopping-cart fa-stack-1x fa-inverse"></i>
</span>
</br>
<p style="color:white;">'.$row["course_name"].'</p>
Check forthe exams
<form name="myform" id="'. $row["course_id"].'" action="checkMarks.php" method="post">
<input type= "text" value ="'. $row["course_id"].' " name="test" style="visibility: hidden;"></input>
</form>
</br>
</div>';
}
}
?>
<script >
function functionTest(form_id) {
document.getElementById(form_id).submit();
}
</script>
I am retrieving names of few courses from database and put them in a table. Then I can click on each one of them and the form submission will be triggered and sends info to the next page and in the next page I get the given info and retrieve info from database again. However, on the second page, if I refresh the page I get
When you refresh a page that has received data from an earlier POST request, the refreshed page won't have that data unless another POST request is made. This can be done in the window object's DOMContentLoaded or load event.
Or, you can make the initial request for the data via a GET request. This will send whatever data you are sending to the server as part of your request as a query string appended to the URL. If you refresh the page, that query string will persist. This will only work if the data you are attempting to get comes from the server-side processing of the current page and not some other URL entirely.
Lastly, POST requests are for requests that change the state of the server (insert, update and delete type of operations). GET should be used for read operations.
Without your code, there's really not much more to offer.
EDIT:
Now that you have posted your code, I would suggest spending some time and cleaning up the HTML string that is sent back from your .php file. There is no such tag as </input> and you should remove the inline HTML event attributes (onclick, etc.). Here's why. Don't use javascript:... either (for many of the same reasons as in the link.
Lastly, I would suggest you change this from a form submission to an AJAX GET request, which will allow you to stay on the same page and keep the currently loaded data.

How to submit additional data along the file upload in dropzone.js?

I have this form in my html code:
<form action="upload" id="upload-dropzone" class="dropzone">
<input type="hidden" name="browser-path" id="browser-path" value="/">
<div class="browser-buttons rrtl">
<a id="browser-btn-upload">Upload</a>
</div>
<div class="lltr" id="browser-path-view"></div>
</form>
<script type="application/javascript">
Dropzone.options.uploadDropzone = {
clickable: "#browser-btn-upload",
};
loadBrowserContent();
</script>
As this document said, the hidden input field browser-path will automatically be submitted as POST data to server.
I have this code in my server side:
System.out.println(request.getParameter("browser-path"));
But this code always prints null to output!
How can I submit this hidden field to my server and how can I read it?
Edit:
Thanks to steeno, the form enctype is multipart/form-data so I have to read the fields from another way.
I assume you use Java as backend language?
As mentioned in the following question: HttpServletRequest get JSON POST data
the problem yould be the encoding of the post request. Maybe try to get the post data with getReader instead of getParameter.

Creating a custom form for mechanize

I have a form on a website that is not directly selectable (as it is embedded in a javascript). So selecting it via mechanize browser object is not possible. What i want to do is to create a form just like that one and submit it using the browser object.
The form is
<form method="POST" action="Action.php?action=338&n=66&t=mine">
<input id="Mine66" type="hidden" value="22" name="duree">
<button class="boutonsGeneral" value="submit" type="submit" name="travail">
<span class="infoBoutonsGeneral" title="">
Work
<span id="dureeMine66" class="boutonsGeneral_duree boutonsGeneral_dureePlus">22 hours</span>
</button>
</form>
I used firebug and here is the info. The URL posted to is http://www.renaissancekingdoms.com/Action.php?action=338&n=66&t=mine
Parameters
duree 22
travail submit
Request Headers From Upload Stream
Content-Length: 23
Content-Type: application/x-www-form-urlencoded
What i have done so far is that i managed to log into the site and did the following
form = mechanize.HTMLForm('http://www.renaissancekingdoms.com/Action.php?action=338&n=66&t=mine', method='POST')
form.new_control('duree', 'hidden', {
'id' : 'Mine66',
'value' : '22'})
form.fixup()
br.form = form
br.submit()
But this doesn't seem to work. Any ideas where i am going wrong?
have you tried going directly to the link passing the post data as a parameter? like this :
r = opener.open('http://example.com/', data)
where data is the post data as a dictionnary

Categories