Get 2024 Templates Mega Bundle!$1000 worth of 19 Bootstrap HTML, Vue & React Templates + 3 Vector Sets for just $9
Get for 99$

Image input


I have a problem with an image input. it shows nice, with their styles, and i checked by JS that on change the image the input fills himselves with the image. but when i submit the form, the avatar input doesn't appear. i have the form with enctype="multipart/form-data", and i tried with a normal input image and it works fine.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(

Replies (5)


Hi Jordi,

After checking, for some reason, the image data has been removed from the input form. Could you please try with the changes as a workaround?

In the /metronic/core/components/image-input/image-input.ts

Inside _change() function:

Please try to remove

this._inputElement.value = "";


<img src="https://i.ibb.co/zmhvmjy/image.png" alt="image" border="0">



Hi Jordi,

Add logging to your Laravel controller to confirm whether the file is being received properly. Here's an example:

if ($request->hasFile("avatar")) {
Log::info("Avatar received:", ["avatar" => $request->file("avatar")->getClientOriginalName()]);
} else {
Log::warning("Avatar not received.");
}


Make sure your JavaScript is not interfering with the form submission. Here's a sample check:

document.getElementById("avatar").addEventListener("change", function(event) {
const file = event.target.files[0];
if (file) {
console.log("File selected:", file.name);
}
});


If a normal file input works but this specific one doesn't, it might be related to custom scripts or how the form is being handled. Double-check any custom logic that may be affecting the submission.

If you continue to face issues, please provide more details about the input and any custom JavaScript involved.



Hi!

About the Controller. it gets that: [2024-08-08 15:50:30] local.WARNING: Avatar not received.

On javascript, i did what you sent and i got this:

File selected: 300-6.png

So as you can see, the input works fine but it is not going to the controller. there is some way. The form is this:

<pre lang="html>
<form method="POST" action="{{ route('profile.update') }}" class="mt-6 space-y-6" enctype="multipart/form-data" >
@csrf
@method('PUT')
</pre>

And this is the input complete:


<div class="image-input size-16" data-image-input="true" >
<input name="avatar_remove" type="hidden" />
<input accept=".png, .jpg, .jpeg" name="avatar" type="file" />
...



Hi Jordi,

Check the browser console for any JavaScript errors or warnings.
Inspect the form data being sent to the server. You can do this in the browser's network tab to ensure the file input is included in the form data.
Log the received data on the server side to verify that the file is being received.

Verify that your JavaScript code correctly updates the input value and that no JavaScript errors are occurring during form submission.

Sample code:

document.getElementById("avatar").addEventListener("change", function(event) {
const file = event.target.files[0];
if (file) {
// Ensure file is handled correctly
console.log("File selected:", file.name);
}
});



It shows like it is sent:

_token: Vt5xOByhvgciF41Ox3VcXOmqelDbPFuVt2BrNKmx
_method: PUT
avatar_remove:
avatar: (binario)
name: Jordi
email: jordi_lario@hotmail.com

But at DD on laravel avatar disappears. If i put a normal input file it works fine.


Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(
Text formatting options
Submit
Here's a how to add some HTML formatting to your comment:
  • <pre></pre> for JS codes block
  • <pre lang="html"></pre> for HTML code block
  • <pre lang="scss"></pre> for SCSS code block
  • <pre lang="php"></pre> for PHP code block
  • <code></code> for single line of code
  • <strong></strong> to make things bold
  • <em></em> to emphasize
  • <ul><li></li></ul>  to make list
  • <ol><li></li></ol>  to make ordered list
  • <h3></h3> to make headings
  • <a></a> for links
  • <img> to paste in an image
  • <blockquote></blockquote> to quote somebody
  • happy  :)
  • shocked  :|
  • sad  :(