javascript 浏览文件后php上传自动提交
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13711264/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
php upload auto submit after browse a file
提问by user1478434
I want to make a simple site that would let you browse for an mp3 and upload it automatically.
我想做一个简单的网站,让您浏览 mp3 并自动上传。
here is my code
这是我的代码
<?php
if(isset($_POST['submit'])){
$file = $_FILES['mp3']['name'];
$tmp = $_FILES['mp3']['tmp_name'];
echo "$file";
move_uploaded_file($tmp, "member/mp3/".$file);
echo"Success";
}
?>
<html>
<form enctype="multipart/form-data" method="POST" action="upload.php">
<input type="file" name="mp3" /><br />
<input type="submit" name="submit" value="Upload" />
</form>
</html>
This code here is my original attempt however this still uses a manual form.
这里的代码是我最初的尝试,但这仍然使用手动形式。
How can I make this upload automatically?
如何自动进行此上传?
回答by Ian
Maybe use something like this:
也许使用这样的东西:
<input type="file" name="mp3" onchange="this.form.submit();" />
Although I don't condone inline event handlers, it's just an example to make sure it works for you. The point is to catch the onchange
event of the file
input and then submit its form. Instead of using this.form
or whatever, you can grab the form
by ID (after giving it one) and call submit()
on it.
尽管我不容忍内联事件处理程序,但这只是一个确保它对您有用的示例。关键是要捕获输入的onchange
事件,file
然后提交其表单。this.form
您可以获取form
by ID(在给它一个之后)并调用submit()
它,而不是使用或其他任何东西。
UPDATE:
更新:
To keep your existing PHP code working, give your submit button an id
attribute (something other than "submit") and try using this:
为了让你现有的 PHP 代码正常工作,给你的提交按钮一个id
属性(不是“提交”)并尝试使用这个:
<input type="file" name="mp3" onchange="document.getElementById('submit_button_id').click();" />
Again, this could be more maintainable/readable if you made it a function call, like:
同样,如果您将其设为函数调用,这可能会更易于维护/可读,例如:
<input type="file" name="mp3" onchange="submitForm();" />
<input type="submit" name="submit" id="submit_button_id" value="Upload" />
<script type="text/javascript">
function submitForm() {
// However you need to submit the form
document.getElementById("submit_button_id").click(); // Or whatever
}
</script>
If you would rather change the PHP, you can use the original this.form.submit();
(or just use .submit()
on the form element in whatever way). In the PHP, what you really care for is to check if $_FILES['mp3']
is set, so it's up to you if you need to check for the submit button's presence. I forget exactly how isset()
works, so I don't want to assume and tell you something wrong. You might be able to use other logic, but the real thing you care about is if the file was sent in the submission, even though it might make it "better" if you make sure a button was used to submit it (though not necessary).
如果您更愿意更改 PHP,则可以使用原始版本this.form.submit();
(或仅以.submit()
任何方式在表单元素上使用)。在 PHP 中,您真正关心的是检查是否$_FILES['mp3']
已设置,因此是否需要检查提交按钮是否存在取决于您。我忘记了具体是如何isset()
工作的,所以我不想假设并告诉你一些错误。您可能可以使用其他逻辑,但您真正关心的是文件是否在提交中发送,即使如果您确保使用按钮提交它可能会使其“更好”(尽管不是必需的) )。
回答by Felix Guo
Simple, attach a onchange()
JS event to it.
很简单,onchange()
给它附加一个JS 事件。
<input type="file" name="mp3" onchange="call .submit() on a form"/><br />
This will submit the form when the user chooses a file. It's safer and more efficient to create these handlers in Jquery or a separate JS script. But for this example, it doesn't matter.
这将在用户选择文件时提交表单。在 Jquery 或单独的 JS 脚本中创建这些处理程序更安全、更有效。但对于这个例子,这并不重要。
For more information, view this question: HTML <input type='file'> File Selection Event
更多信息,查看这个问题:HTML <input type='file'> File Selection Event