行を追加するテーブルがあり、3つの行がある場合、以下のテーブルは次のようになります。
Question No Image
1 (file input)
2 (file input)
3 (file input)
以下は、上記のテーブルを作成するコードです。
var qnum = 1;
var numimage = 0;
var $qid = $("<td class='qid'></td>").text(qnum);
var $image = $("<td class='image'></td>");
$('.num_questions').each( function() {
var $this = $(this);
var $questionNumber = $("<input type='hidden' class='num_questionsRow'>").attr('name',$this.attr('name')+"[]")
.attr('value',$this.val());
$qid.append($questionNumber);
++qnum;
++numimage;
$(".questionNum").text(qnum);
$(".numimage").val(numimage);
});
var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" +
"Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><br/><label class='imagelbl'>" +
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" +
"<input type='hidden' class='numimage' name='numimage' value='" + numimage + "' />" +
"</p><p class='imagef1_cancel' align='center'><label>" +
"<input type='reset' name='imageCancel' class='imageCancel' value='Cancel' /></label>" +
"</p><p class='listImage' align='left'></p>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>");
$image.append($fileImage);
$tr.append($qid);
$tr.append($image);
私が抱えている問題は、ファイルが失敗またはキャンセルされた後、今回は成功した別のファイルをアップロードした場合、iframeで以下の通知を受け取ることです。
Notice: Undefined index: numimage in /web/stud/...../app/imageupload.php on line 150
私の質問は、試行またはキャンセルに失敗した後にファイルを正常にアップロードしたときに、データベースにデータを挿入する以下のphpコードでこの通知を受け取るのはなぜですか?
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
$lastID = $mysqli->insert_id;
$imagequestionsql = "INSERT INTO Image_Question (ImageId, SessionId, QuestionId)
VALUES (?, ?, ?)";
if (!$insertimagequestion = $mysqli->prepare($imagequestionsql)) {
// Handle errors with prepare operation here
echo "Prepare statement err imagequestion";
}
$qnum = (int)$_POST['numimage'];
$insertimagequestion->bind_param("isi",$lastID, $sessid, $qnum);
$sessid = $_SESSION['id'] . ($_SESSION['initial_count'] > 1 ? $_SESSION['sessionCount'] : '');
$insertimagequestion->execute();
if ($insertimagequestion->errno) {
// Handle query error here
}
$insertimagequestion->close();
}
<script language="javascript" type="text/javascript">window.top.stopImageUpload(<?php echo $result ? 'true' : 'false'; ?>, '<?php echo $_FILES['fileImage']['name'] ?>');</script>
アップデート:
startimageupload
以下は、アップロードを開始するときのstopimageupload
関数と、アップロードを停止または終了するときの関数を含むjqueryコードです。
function htmlEncode(value) { return $('<div/>').text(value).html(); }
function startImageUpload(imageuploadform){
$(imageuploadform).find('.imagef1_upload_process').css('visibility','visible');
$(imageuploadform).find('.imagef1_cancel').css('visibility','visible');
$(imageuploadform).find('.imagef1_upload_form').css('visibility','hidden');
sourceImageForm = imageuploadform;
$(".sbtnimage").attr("disabled", "disabled");
$(".sbtnvideo").attr("disabled", "disabled");
$(".sbtnaudio").attr("disabled", "disabled");
$(imageuploadform).find(".imageCancel").on("click", function(event) {
$('.upload_target_image').get(0).contentwindow
$("iframe[name='upload_target_image']").attr("src", "javascript:'<html></html>'");
return stopImageUpload(2);
});
return true;
}
var imagecounter = 0;
function stopImageUpload(success, imagefilename){
var result = '';
imagecounter++;
if (success == 1){
result = '<span class="imagemsg'+imagecounter+'">The file was uploaded successfully</span><br/><br/>';
$('.listImage').eq(window.lastUploadImageIndex).append('<div>' + htmlEncode(imagefilename) + '<button type="button" class="deletefileimage" image_file_name="' + imagefilename + '">Remove</button><br/><hr/></div>');
}
else if (success == 2){
result = '<span class="imagemsg'+imagecounter+'"> The file upload was canceled</span><br/><br/>';
}
else {
result = '<span class="imagemsg'+imagecounter+'">There was an error during file upload</span><br/><br/>';
}
$(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_upload_form').html(result + '<label>Image File: <input name="fileImage" class="fileImage" type="file"/></label><br/><br/><label><input type="submit" name="submitImageBtn" class="sbtnimage" value="Upload" /></label><label><input type="button" name="imageClear" class="imageClear" value="Clear File"/></label>');
$(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');
$(".sbtnimage").removeAttr("disabled");
$(".sbtnvideo").removeAttr("disabled");
$(".sbtnaudio").removeAttr("disabled");
$(".imageClear").on("click", function(event) {
event.preventDefault();
$(this).parents("form:first").find(".fileImage").replaceWith("<input type='file' class='fileImage' name='fileImage' />");
});