【发布时间】:2015-04-29 20:06:52
【问题描述】:
我正在尝试使用 ajax 和 php 上传图像,并且我已成功使其工作到一定程度,但我无法将输入的值 (ID) 传递给我的 php 文件。
这是我的场景。
表格
<form enctype="multipart/form-data" id="myform">
<input type="file" name="images[]" multiple id="image"/>
</form>
按钮
<button type="button" id="uploadimages" name="thing_id"
value="<?php echo $row['thing_id']; ?>" class="btn btn-primary">Save changes
</button>
AJAX
$("#uploadimages").click(function () {
var form = new FormData($('#myform')[0]);
// Make the ajax call
$.ajax({
url: 'uploadimages.php',
type: 'POST',
xhr: function () {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
myXhr.upload.addEventListener('progress', progress, false);
}
return myXhr;
},
//add beforesend handler to validate or something
//beforeSend: functionname,
success: function (res) {
$('#content_here_please').html(res);
/**/
},
//add error handler for when a error occurs if you want!
//error: errorfunction,
data: form,
cache: false,
contentType: false,
processData: false
});
});
PHP
$get_id = $_POST['']; // This is where I stuck.
如何将$row['thing_id'] 传递给$get_id 变量?我可以在成功时使用另一个 GET ajax 调用传递它,但随后我丢失了 image_id 值(我使用 foreach 因为我可能会上传多个文件),所以我想在同一个 php 文件中处理它。
我没有包含我的上传脚本,因为除非我尝试使用 thing_id 进行制作,否则它可以正常工作。
【问题讨论】:
-
为什么不添加具有该值的隐藏输入字段并在 php 中接收它?
<input type="hidden" value="<?php echo $row['thing_id']; ?>" name="thing_id" id="thing_id" />
标签: javascript php jquery ajax