2016-04-13 9 views
0

標準的な方法でフォームを使用すると、PHPがうまく動作します。 しかし、フォームをAjaxで送信しようとすると、$ _file []が表示されず、ファイルがなければ "success | na" が返りますが、同じ結果が返されますファイルがあります。画像はAjaxでアップロードされますが、PHPではファイルが表示されません

ajaxの設定は正しいですか? cashe:false、processData:false?

私はいつもiframeを使用することができますが、私はむしろそうしないと助けてくれる人に助けになります。時間ありがとう!

function info_save(){ 
 
\t gf.wait(); 
 
\t var info_ava = ''; 
 
\t var info_abo = $('#info_abo').val() 
 
\t target = './php/profile_system.php'; 
 
\t \t \t $.ajax({ 
 
\t \t \t \t url: "./php/profile_image.php", 
 
\t \t \t \t type: "POST", 
 
\t \t \t \t data: new FormData('#info_for'), 
 
\t \t \t \t contentType: false, 
 
\t \t \t \t cache: false, 
 
\t \t \t \t processData:false, 
 
\t \t \t \t success: function(reply){ 
 
\t \t \t \t \t imgresponse = reply.split("|"); 
 
\t \t \t \t \t if(imgresponse[0] == 'success' || imgresponse[0] == 'Success'){ 
 
\t \t \t \t \t \t \t if(imgresponse[1] == 'na'){info_ava = ' ';} 
 
\t \t \t \t \t \t \t else{info_ava = imgresponse[1];} 
 
\t \t \t \t \t \t \t var formDataA = { 
 
\t \t \t \t \t \t \t \t 'action': 'info', 
 
\t \t \t \t \t \t \t \t 'info_abo': info_abo, 
 
\t \t \t \t \t \t \t \t 'info_ava': info_ava 
 
\t \t \t \t \t \t \t }; 
 
\t \t \t \t \t \t \t profile_ajax(target, formDataA,'info'); 
 
\t \t \t \t \t }else{ 
 
\t \t \t \t \t \t profile_stop(reply,'info'); 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t }); 
 
}
<form id="info_for" class="form-horizontal col-lg-8 dblue_text" role="form" action="../php/profile_image.php" method="post" enctype="multipart/form-data"> 
 
\t <div class="form-group"> 
 
\t \t <label class="control-label col-sm-2" for="info_abo"><i class="fa fa-info"></i> About</label> 
 
\t \t <div class="col-lg-8 col-sm-10"> 
 
\t \t \t <textarea class="form-control" id="info_abo" name="info_abo" placeholder="Let your friends know a little about yourself!"><?php echo $info_abo;?></textarea> 
 
\t \t </div> 
 
\t </div> 
 
\t <div class="form-group"> 
 
\t \t <label class="control-label col-sm-2" for="file"><i class="fa fa-picture-o"></i> Avatar</label> 
 
\t \t <div class="col-lg-8 col-sm-10"> 
 
\t \t \t <input type="file" name="file" id="file"> 
 
\t \t </div> 
 
\t </div> 
 
</form> 
 
<button onclick="info_save()" class="btn btn-info">Update</button> 
 

 
<?php 
 
\t if(ISSET($log_username) && $user_ok == true){ 
 
\t \t if(isset($_FILES["file"]["type"])){ 
 
\t \t \t $validextensions = array("jpeg", "jpg", "png","gif"); 
 
\t \t \t $temporary = explode(".", $_FILES["file"]["name"]); 
 
\t \t \t $file_extension = end($temporary); 
 
\t \t \t if ((($_FILES["file"]["type"] == "image/png") || ($_FILES["file"]["type"] == "image/jpg") || ($_FILES["file"]["type"] == "image/jpeg") || ($_FILES["file"]["type"] == "image/gif")) && ($_FILES["file"]["size"] < 100000) && in_array($file_extension, $validextensions)){ 
 
\t \t \t \t if ($_FILES["file"]["error"] > 0){ 
 
\t \t \t \t \t echo "Return Code: " . $_FILES["file"]["error"] . "<br/><br/>"; 
 
\t \t \t \t }else{ 
 
\t \t \t \t \t if (file_exists("../profiles/".$log_username."/".$_FILES["file"]["name"])){ 
 
\t \t \t \t \t \t echo "File Already Exists: ".$_FILES["file"]["name"]; 
 
\t \t \t \t \t }else{ 
 
\t \t \t \t \t \t $sourcePath = $_FILES['file']['tmp_name']; // Storing source path of the file in a variable 
 
\t \t \t \t \t \t $targetPath = "../profiles/".$log_username."/".$_FILES['file']['name']; // Target path where file is to be stored 
 
\t \t \t \t \t \t if(move_uploaded_file($sourcePath,$targetPath)){echo 'success|'.$_FILES["file"]["name"]; exit();} 
 
\t \t \t \t \t \t else{echo 'Connection Error';exit();} 
 
\t \t \t \t \t } 
 
\t \t \t \t } 
 
\t \t \t }else{ 
 
\t \t \t \t echo "Invalid file Size or Type"; exit(); 
 
\t \t \t } 
 
\t \t }else{echo 'success|na'; exit();} 
 
\t }else{echo 'Please Sign In'; exit();} 
 
?>

答えて

1

いるFormDataコンストラクタパラメータないセレクタ文字列またはjQueryオブジェクトとしてHTMLFormElementをとります。

data: new FormData(document.getElementById('info_for')), 

または

data: new FormData($('#info_for')[0]), 

または

data: new FormData(document.querySelector('#info_for')), 
+0

本当にありがとうございましたムーサ! 実際にそれらはすべて働いたが、 私が忘れていたものは[0]でした。 もう一度おねがいします! –

関連する問題