我正在尝试使用Ajax和&amp ;;上传文件PHP,但问题是我无法发送包含文件夹ID的输入类型文本。
这是我的HTML:
<form method="post" enctype="multipart/form-data" action="uploadify.php">
<input type="file" name="images" id="images" multiple />
<input type="hidden" name="eid" value="<?php echo $eid;?>" />
<input type="btn submit" id="btn" value="Upload" />
</form>
<div id="response"></div>
<ul id="image-list">
</ul>
这是我的JavaScript:
(function () {
var input = document.getElementById("images"),
formdata = false;
function showUploadedItem (source) {
var list = document.getElementById("image-list"),
li = document.createElement("li"),
img = document.createElement("img");
img.src = source;
li.appendChild(img);
list.appendChild(li);
}
if (window.FormData) {
formdata = new FormData();
document.getElementById("btn").style.display = "none";
}
input.addEventListener("change", function (evt) {
document.getElementById("response").innerHTML = "Uploading . . ."
var i = 0, len = this.files.length, img, reader, file;
for ( ; i < len; i++ ) {
file = this.files[i];
if (!!file.type.match(/image.*/)) {
if ( window.FileReader ) {
reader = new FileReader();
reader.onloadend = function (e) {
showUploadedItem(e.target.result, file.fileName);
};
reader.readAsDataURL(file);
}
if (formdata) {
formdata.append("images[]", file);
}
}
}
if (formdata) {
$.ajax({
url: "uploadify.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
}, false);
}());
这是我的PHP:
<?php
//upload.php
$eid = $_POST['eid'];
$output_dir = "./images/icons/".$eid."/screenshots/";
if (!file_exists($output_dir) and !is_dir($output_dir)) {
mkdir($output_dir);
}
foreach ($_FILES["images"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$name = $_FILES["images"]["name"][$key];
move_uploaded_file( $_FILES["images"]["tmp_name"][$key], "$output_dir" . $_FILES['images']['name'][$key]);
}
}
echo "<h2>Successfully Uploaded Images $eid</h2>";
我想要做的是我发送输入类型隐藏名称= eid但不知道什么是错的,为什么我无法通过我的JS函数发送它?
答案 0 :(得分:0)
"The only thing missing is that i am unable to send that eid field . [...] If i will be able to post that field with this function my gallery will be completed"
为输入字段指定一个id,从表单中获取id,因此您可以获取与id对应的值:
<input id='theEIdGuy' type="hidden" name="eid" value="<?php echo $eid;?>" />
因此,如果我们有PHP(在表单提交之后)我们可以像这样检索它,现在这是可能的,因为它期望通过标识符(id)来识别值,而不是名称。
$eid = isset($_POST['theEIdGuy']) ? $_POST['theEIdGuy'] : "It's not set :(";
使用原始javascript你可以这样得到它(但如果你只需要用PHP,你就不必这样做了):
var theEIdGuy = document.getElementById('theEIdGuy').value;
使用jQuery你可以这样:
var theEIDGuy = $('#theEIdGuy').val();
答案 1 :(得分:0)
这些天我遇到了同样的问题......解决方案更简单,你可以想象:)
改变这个:
if (formdata) {
formdata.append("images[]", file);
}
使用:
if (formdata) {
var variable_eid = document.getElementById("eid");
formdata.append("images[]", file);
formdata.append("eid", variable_eid);
}
对我来说使用Jquery / Ajax更简单..编码和清理更少。我试着快速调整它以满足你的需求......
$(document).ready(function() {
$('#images').change(function(){
var variable_eid = $('#eid').val();
var data;
data = new FormData();
data.append( 'images', $('#file')[0].files[0] );
data.append( 'eid', variable_eid);
$.ajax({
url: 'uploadify.php',
data:data,
processData: false,
contentType: false,
type: 'POST',
success: function ( data ) {
$('#response').html(data);
}
});
});
});