2011-01-26 78 views
0

获取数据那么的HTML代码,我有:PHP + AJAX文件上传,不能从POST

<form id="loginIMGForm">    
<input type="file" name="file" id="loginIMG" /> 
<br /> 
<input type="submit" name="submit" value="Submit" id="submitloginIMG" /> 
</form> 

<div id="loginIMGStatus">Select an image and upload.</div> 

然后PHP代码我有:

<?php 

$imageinfo = getimagesize($_FILES['loginIMGForm']['tmp_name']); 

if($imageinfo['mime'] != 'image/gif' && $imageinfo['mime'] != 'image/jpeg') { 
    echo "Sorry, we only accept GIF and JPEG images\n"; 
    exit; 
} 

$uploaddir = 'uploads/'; 
$uploadfile = $uploaddir . basename($_FILES['loginIMGForm']['name']); 

if (move_uploaded_file($_FILES['loginIMGForm']['tmp_name'], $uploadfile)) { 
    echo "File is valid, and was successfully uploaded.\n"; 
} else { 
    echo "File uploading failed.\n"; 
} 
?> 

和jQuery是:

$('#loginIMGForm').submit(function() { 
     // 'this' refers to the current submitted form 
     var str = $(this).serialize(); 
     alert(str); 
     $.ajax({ 
      type: "POST", 
      url: "modules/upload.php", 
      data: str, 
      success: function(msg){ 
       $("#loginIMGStatus").ajaxComplete(function(event, request, settings){ 
        if(msg == 'OK'){ result = 'Login Image is updated'; } 
        else{ result = msg; } 

        $(this).html(result); 
       }); 

      } 

     }); 

     return false; 
    }); 

I did debuging using alert, the data is empty. What did I do wrong. 

谢谢。

回答

0

你为什么打电话给ajaxComplete

只是这样做,而不是:

$.ajax({ 
     type: "POST", 
     url: "modules/upload.php", 
     data: str, 
     success: function(msg){ 
      var result; 
      if(msg == 'OK'){ result = 'Login Image is updated'; } 
       else{ result = msg; } 

       $(this).html(result); 
      }); 

     } 

    }); 

此外,当你说的数据是空的,你的意思是,当你调用alerthtml组件?

编辑您不能在file上使用serialize。它不被支持,并且没有意义。见this page。另外,虽然这还不是问题,但我怀疑你打电话给ajaxComplete会有问题。当ajax请求完成时,success参数已经处理调用你的函数。我不确定你的代码会发生什么。

+0

当我呼叫警报的数据是空的。所以我不认为问题来自.ajaxComplete部分。 – 2011-01-27 02:25:10