我被困在这里。我一直在尝试上传一些照片,同时将相册的ID(值在表单中的隐藏形式)传递给处理上传的相同php脚本。但我不知道如何分别传递专辑ID这是代码。
的js
input.addEventListener("change", function (evt) {
document.getElementById("response").innerHTML = "<img src='../assets/admin/images/loading.gif' />"
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: "../assets/admin/ajaxupload/upload.php",
type: "POST",
data: formdata,
processData: false,
contentType: false,
success: function (res) {
document.getElementById("response").innerHTML = res;
}
});
}
PHP
//how do i retrieve the given album id value that was passed.
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], "../../uploads/pics/" .$_FILES['images']['name'][$key]);
}
}
echo "<p>Successfully Uploaded Images</p>";
请尽快回复。
答案 0 :(得分:1)
(对不起,我没有时间解释,但希望有所帮助)
在你的ajax php文件中
$errors = array(); // initialize empty error array
if (sizeof($errors) == 0) {
...
if ($securimage->check($captcha) == false) {
$errors['captcha_error'] = 'wrong code';
}
}
if (sizeof($errors) == 0) {
// no errors, send the form
$return = array('error' => 0, 'message' => 'OK');
die(json_encode($return));
} else {
$errmsg = '';
foreach ($errors as $key => $error) {
// set up error messages to display with each field
$errmsg .= " - {$error}\n";
}
$return = array('error' => 1, 'message' => $errmsg);
die(json_encode($return));
}
你的js
url: 'ajax.php',
type: "POST",
data: $('#formID').serialize(),
success: function(msg) {
try {
// alert( "Data Saved: " + msg );
json = jQuery.parseJSON(msg);
// alert(json.error)
if (json.error == 0) {
} catch(e) {
alert("Sorry, there was an error parsing the json");
}
},
error: function(msg) {
alert("Ajax request failed");
}