HTML File Upload issue - html

possibly a stupid question but how do I upload files to a server using the browser ?
I'm sure I've used this before ! But when I run it nothing appears to happen.
No errors are shown and nothing is logged in the error_log
<?php
var_dump($_FILES);
echo $_FILES['uploadFile']['tmp_name'];
?>
<html>
<head>
<title>File Upload Form</title>
</head>
<body>
This form allows you to upload a file to the server.<br>
<form action="test.php" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="submit" value="Upload File">
</form>
</body>
</html>
What am I doing wrong ?

When you submit a form, the data you are sending to be encoded somehow to be put in the HTTP request.
By default, this uses the application/x-www-form-urlencoded algorithm which does not support file uploads. You need to use multipart/form-data instead.
<form action="test.php" method="post" enctype="multipart/form-data">

Try to correct you form declaration attribute and always when you need to upload files include "enctype". If not file input element is in form, the default enctype is "application/x-www-form-urlencoded":
<form action="test.php" method="post" enctype="multipart/form-data">
FORM in HTML

Related

how can i res.send() to a specific id of an html tag? - Node js

I have search.html with this form:
<form class="myform" action="/classi" autocomplete="on">
<input type="text" name="search" >
<button type="submit" ></button>
when i press the button:
index.js:
router.get('/classi',function(req,res,next){
res.send("ciaoo")
});
I want to send the text "ciaoo" to a specific id of my html tag in another page html "classi.html".
classi.html
<head>
<body>
<h1 id="ciao"></h1>
</body>
</head>
How can I do? Thanks
For that, you have to add a script in your HTML file and then you can communicate through ajax call and modify your HTML through the script.
Make a JS file and include the file in HTML and follow these steps -
Make an AJAX call(GET ..../classi) to your server using XMLHttpRequest.
Get response in callback (or in then if using promise).
Modify the HTML with the response.

Form action relative URL

I have html file like this:
<!DOCTYPE html>
<html>
<head>
<title>HTML Form</title>
</head>
<body>
</body>
</html>
<FORM action="/wsd/html_form_check" method="post">
<P>
//INPUTS AND LABES AND THINGS
<INPUT type="submit" value="Send">
</P>
</FORM>
Now I get an error saying
AssertionError: Invalid form action.
I suppose it has something to do with the
<FORM action="/wsd/html_form_check" method="post">
I was told that the HTTP request should look like this:
POST /wsd/html_form_check HTTP/1.1
Host: 127.0.0.1
So what am I doing wrong? Any help is appreciated!
Thanks!
A bit late, but I had to deal with the same issue.
Consider the following piece of HTML code:
<form id="file_uploader" action="/Home/Upload" class="dropzone">
<div class="fallback">
<input name="file" type="file" multiple />
</div>
</form>
In the development environment it runs fine. However, deploying it to IIS anywhere else but the root website will cause the action URL to become invalid. For instance, if it were deployed to https://example.com/deployed_app, then the actual action URL that the form will call is https://example.com/Home/Upload, which is invalid.
The solution is to change the action URL to this:
<form id="file_uploader" action="~/Home/Upload" class="dropzone">
If you're trying to send the data entered in the form to "html_form_check" you haven't used the file extension such as .php or .js. Try adding the file extension and see if that helps !

working with forms in HTML 5

i have tried the following code while trying to learn html forms:
<head>
<title>Example</title>
</head>
<body>
<form method="post" action="http://titan:8080/form">
<input name="fave"/>
<input name="name"/>
<button>Submit Vote</button>
</form>
</body>
but its not working the webpage shows this message: "Oops! Google Chrome could not find titan:8080"
The action attribute must contain a valid URL. In this case, the server part of the URL is not valid, since titan is not a valid domain name. It should be something like titan.example or titan.foobar.example.com (naturally, you need to use the real, working domain name; the names I used are guaranteed to not work, i.e. for use as dummy examples only).
Alternatively, if you are running a local HTTP server, at port 8080, in the computer where you use the HTML page, use the reserved name localhost as in action="http://localhost:8080/form">.
The url you have provided http://titan:8080/form is timing out ensure you have the correct url or that the page is live.
<title>Example</title>
<form method="post" action="#a">
<input name="fave"/>
<input name="name"/>
<button>Submit Vote</button>
</form>

Submitting form in HTML - redirecting

I have a form in my webpage,
The form uploads a file to my server.
currently when the file upload form submits, after the server side finishes saving the image it tries to redirect my webpage to a different page :( (the name of the form action)
is there a way to execute the form without the redirect part? like an ajax call for uploading my image?
thanks.
here is the form:
<form method="POST" action="/project/upload"
enctype="multipart/form-data">
Please select a file to upload : <input type="file" name="file" />
<input type="submit" value="upload" />
</form>
EDIT - Let me try to rephrase it:
is it possible to submita form in such a way that it wont change my page or redirect?
and is it possible to create a call back when it finishes?
You can use target="iframeName" on your form to get it to post to an iframe on the page with no reloads occuring. Something a little like this:
<form method="POST" action="/project/upload"
enctype="multipart/form-data" target="myFrame">
..Form content..
</form>
<iframe name="myFrame" height=0 width=0></iframe>
Think best way is using a php page which calls itself with no redirect:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
....
</form>
So you call the same page to do upload and don't need redirect. In other way you can add header in php page "/project/upload" with automatic redirect to put after upload code:
header("location: yourpage.html");

html, file uploading trouble

I have a trouble with file-uploding. Here is my part of the form:
<input type="file" name="image_file" />
<input type="submit" name="add_new" value="Upload" />
And in script i have a code:
print_r($_FILES);
After image choosing and sending form, I have an empty array $_FILES. Why?
The form must be set like this:
<form action="myuploadpage.php" method="post" enctype="multipart/form-data">
...
</form>
So that the browser knows to send the image along with the data.
Have you set the form method to POST data to the PHP script and set the action to point to it? If so then you should be able to get something like $_FILES['image_file']['name']