0

我有一个表格,它附加了行并且我有 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>

更新:

下面是jquery代码,其中包含startimageupload上传开始时的stopimageupload函数和上传停止或完成时的函数:

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' />");
}); 
4

3 回答 3

1

它在 $_POST 数组中找不到键 numimage - 这意味着 numimage 不是由您的 ajax 调用发送的。您是否尝试过使用 Firebug 查看发送到 php 脚本的内容?

于 2012-09-23T17:28:26.153 回答
0

线$qnum = (int)$_POST['numimage'];导致它。$_POST 数组似乎没有设置索引的值numimage

于 2012-09-23T17:31:37.027 回答
0

好的,通过查看您发布的代码,我正在猜测正在发生的事情,因为我看不到代码的完整流程。基本上以下代码导致您的问题:

$(sourceImageForm).find('.imagef1_upload_process').css('visibility','hidden');
$(sourceImageForm).find('.imagef1_cancel').css('visibility','hidden');
/// THIS LINE BELOW
$(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>');
/// THIS LINE ABOVE
$(sourceImageForm).find('.imagef1_upload_form').css('visibility','visible');

上面的代码取自在成功/错误上传发生后运行的代码。此代码继续覆盖负责上传图像的表单。现在这对于成功来说很好,因为正如我可以推断的那样,您的界面对每个图像使用单独的表单......一旦您获得成功,您就不会再次重复使用该表单(因此服务器端没有错误用于后续上传)

但是,当图像上传错误或取消时......我想我猜对了 - 您尝试重复使用该表格再次上传图像?如果是这样,您已经覆盖并删除了 numimage 输入 html:

<input type='hidden' class='numimage' name='numimage' value='2' />

这意味着您会收到一个错误,因为该字段不再存在。

正如我所说,这是一个猜测,但它会导致删除一个开始时确实存在的字段,并会产生您所看到的问题。

于 2012-09-23T18:22:35.293 回答