Jump to content

onchange not passing form data


timstring67

Recommended Posts

I borrowed this from several different sources.  The HTML code:

 

<form action="ImportTOA.php" method="POST" accept="text/csv">
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    Upload Daily TOA Logs: <br /><input name="userfile" type="file" onchange="this.form.submit()" />
</form>

 

and then, the PHP script:

 

$ImportFile = $_FILES['userfile']['name'];

 

For some strange reason, the form is not passing 'username' to the action file.  What is the simple solution that I'm overlooking?

Link to comment
Share on other sites

Sorry, I did not mention at all what I'm trying to accomplish.  I'm not trying to upload a file.  There are many tutorials on that.  All I need is to pass a file name from the form to my php action script, and let the script fold, spindle, and mutilate that file to put it into a MySQL database.  The form calls up the file dialog, but doesn't pass the file name that is selected.

Link to comment
Share on other sites

OK.  With a sufficiently narrow Google query, I found the answer.  Should anyone come along to read this thread, here's the code I borrowed that worked:

 

<form enctype="multipart/form-data" action="ImportTOA.php" method="POST" accept="text/csv">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Upload Daily TOA Logs: <br /><input name="userfile" type="file" onChange = "this.form.submit()" />

</form>
Edited by TimString
Link to comment
Share on other sites

Hmm . . . the fact that you say you don't want to actually upload the file is irrelevant because you are using a FILE input field. The sole purpose of that field is to upload a file. And the reason you were not getting the file name (which is included as part of the file upload) is specifically because you didn't configure your form for a file upload. If you had actually read the tutorial I linked to, that should have been apparent. To do a file upload you must set the form enctype to "multipart/form-data". Since you were not doing that, none of the information for that FILE field was being passed.

 

So, even though you say you don't want to upload the file, it is still being passed in the form submission. If you aren't going to actually save/read that file and only want the name, there is another option. If this is for an internal tool where you have a known user base and can be certain they have JavaScript enabled, you can set the form to NOT upload the file. Then, include the FILE field in the form and add an onchange event to the field to populate a hidden field with the value of the FILE field. Then, when the form is submitted the FILE field will not be included in the POST data, but you will have the name in the hidden field.

 

 

File: <input type="FILE" id="file" onchange="document.getElementById('filename').value=this.value;" /><br>
FileName: <input type="text" name="filename" id="filename" />
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.