how to upload a file to my server using html - html

Basically, I have this form that allows user to upload to my server:
<form id = "uploadbanner" method = "post" action = "#">
<input id = "fileupload" type = "file" />
<input type = "submit" value = "submit" id = "submit" />
</form>
But the problem is that when I choose a file, then click submit, I don't see the file uploaded in the server directory.

<form id="uploadbanner" enctype="multipart/form-data" method="post" action="#">
<input id="fileupload" name="myfile" type="file" />
<input type="submit" value="submit" id="submit" />
</form>
To upload a file, it is essential to set enctype="multipart/form-data" on your form
You need that form type and then some php to process the file :)
You should probably check out Uploadify if you want something very customisable out of the box.

You need enctype="multipart/form-data" otherwise you will load only the file name and not the data.

On top of what the others have already stated, some sort of server-side scripting is necessary in order for the server to read and save the file.
Using PHP might be a good choice, but you're free to use any server-side scripting language. http://www.w3schools.com/php/php_file_upload.asp may be of use on that end.

you will need to have a backend on the server do handle the file upload request you can base your backend with this php example
<?php
$file = $_FILES['file'];
move_uploaded_file($file['tmp_name'], 'uploads/' . $file['name']);?>
Then point the form to the file
<form id="uploadbanner" enctype="multipart/form-data" method="post" action="/tourscript.php">
<input id="fileupload" name="myfile" type="file" />
<input type="submit" value="submit" id="submit" />
</form>

Related

html - How to get text input value from a file?

So I have a form in HTML which submits the content of 2 inputs to an external website. What I am trying to achieve is get the value of the input from a text file. Is that possible? If so, how can I do it?
<form action="http://blockbench.net/web/index.php" method="POST">
<input type="text" name="model" value="I want this value to be gotten from a text file I have in the directory of my website">
<input type="text" name="textures" value="">
<button type="submit">Submit</button>
</form>
You have two possibilities to do this: server side (which I'd recommend) or client side.
Server side:
Depending on what language you have available on your server, you can have the file content served as part of the HTML. For PHP for instance:
<form action="http://blockbench.net/web/index.php" method="POST">
<input type="text" name="model" value="<?php echo file_get_contents('some_file_on_server.txt'); ?>">
<input type="text" name="textures" value="">
<button type="submit">Submit</button>
</form>
Client side:
Alternatively, if the file you want to read is publicly available on the same web server, you can have the browser request it via JavaScript and fill in the data.
<form action="http://blockbench.net/web/index.php" method="POST">
<input type="text" name="model" value="">
<input type="text" name="textures" value="">
<button type="submit">Submit</button>
</form>
<script>
fetch('http://your-server/some_file.txt')
.then(response => response.text())
.then(text => document.querySelector('[name="model"]').value = text)
</script>
I recommend going for the server side way, because the text from the file will be immediately available as the HTML arrives in the browser while the client side solution will take some time (or may even fail) to download the text from the server and fill it into the input element.

HTML server cannot find the specified file

I'm currently doing some web development work and my assignment requires me to upload a file to a server. I have the following code:
<form id = "uploadbanner" method = "post">
<input id = "fileupload" type = "file" />
</form>
I don't have a submit button, since there's already one at the bottom of the page. Whenever I test out the code and input a file x, it tells me that it cannot find x. I've tried seeing if I could obtain the path name from the client to see if this was the issue, but that isn't allowed due to security reasons. Any help would be appreciated; thanks!
This could help:
<form action="upload.php" method="post" enctype="multipart/form-data">
Select file to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload File" name="submit">
</form>
In order to achieve the upload, you must:
add an action to your form
use name instead of id in order to read the file, check this: HTML input - name vs. id
Don't forget to link your submit button to the form and you're good to go.

HTML Form File Uploads doesn't upload file

Files doesn't get uploaded though all back-end code is correct. I tested the back end code with another front-end styled code and it worked fine.
but in my front end code it doesn't upload any files. I removed all css and scripts as well to figure out the issue.
here is my simple front end HTML form :
<form action="upload_handler.php" method="post">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
you forgot to mention the enctype="multipart/form-data"
<form action="upload_handler.php" enctype="multipart/form-data" method="post">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload" name="submit">
</form>
The issue is with the attributes in your <form> tag. To successfully enable files to be uploaded properly in HTML, following requirements should be there.
Make sure that the form uses method="post"
The form also needs the following attribute: enctype="multipart/form-data".
It specifies which content-type to use when submitting the form
So just add this enctype="multipart/form-data" part inside your <form> tag.

How to build a custom URL from a HTML form?

I have a HTML form like:
<form name="input" action="" method="post">
Username: <input type="text" name="user">
<input type="submit" value="Submit">
</form>
I need to build a URL when the submit link is clicked and send the user to it, using the username from the form in the correct position in the URL.
URL:
http://example.com/htmlchat/init.html?init_room=1&init_user=USERNAME_FROM_FORM_HERE
Can someone help (an example would be great)?
Don't use POST when you want the data to appear in the URL
Do put all the data you want in your URL in the form
Get the names of your fields right
Put the base URI in the action
such:
<form action="init.html">
<input type="hidden" name="init_room" value="1">
<label>
Username:
<input name="init_user">
</label>
<input type="submit" value="Submit">
</form>
This is possible with either Javascript or a server side language such as PHP. With Javascript you want to update the action attribute.
You could use jQuery to do this.

Can I have two form's in diffrent places which triggers one request to server?

I am beginner to HTML. I found there is something like form which is used to pass data to server. I know what is the basic usage.
Last time I cut children of my initial form between two HTML files (some reorganization to include later in JSP). Personally I don't like to start tag <form> in one file, and close the </form> in other file. And I know I could do something like this (I will use this probably):
<form>
// include file1
// include file2
</form>
But now I am just thinking... Is it possible to do something totally different? like this:
// first file
<form name="input" action="index.html" method="get">
<label for="iduser">User:</label><input id="iduser" type="text" name="user">
</form>
// second file
<form name="input">
<label for="iddata">Data:</label><input id="iddata" type="text" name="data">
<input type="submit" value="Submit">
</form>
I want to submit both inputs with button inside second form. I know above doesn't work even if I set the same name attribute. But maybe I missed something?
if you need to break the form in 2 separate files, you can't have nested form elements so what you need to do is this
// first file
User: <input type="text" name="user">
// second file
Data: <input type="text" name="data">
<input type="submit" value="Submit">
There is no need to break it into seperate forms..
Keep it as the one form they will both be poosted to the same location..
// only File
<form name="input" action="index.html" method="post">
<label>User:</label> <input type="text" name="user">
<label>Data:</label> <input type="text" name="data">
<input type="submit" value="Submit">
</form>
if you wanted to carry on to the second page with the first page post values you could use something like php
<?php
$value1FromPrevious = $_POST['user'];
$value2FromPrevious = $_POST['data'];
;?>
More HTML Code forms here
You will just need to change the form ACTION to your new php page..