2014-02-18 39 views
0

我的問題是,我的腳本不會上傳服務器上的圖片,並將其插入數據庫,當我使用ajax提交出於某種原因。如果我提交表單與PHP的動作=「file.php」它的作品。這裏是我的ajax腳本和php one.I沒有看到問題出在哪裏,爲什麼它與PHP提交和不與阿賈克斯工作。 Thnx提前。ajax沒有上傳文件圖片到數據庫和服務器

<script> 
$(function() { 
//twitter bootstrap script 
    $("button#submit").click(function(){ 
      $.ajax({ 
      type: "POST", 
     url: "engine/app/process.php", 
     data: $(\'form.edit\').serialize(), 
     dataType:\'json\', 
      success: function(data){ 
        $("#bday").html(data.a) 
        $("#locatie").html(data.b) 
        $("#descriere").html(data.c) 
        $("#interes").html(data.d) 
        $("#status").html(data.e) 
        $(".img").html(data.f) 

        $("#myModal").modal(\'hide\');  
       }, 
     error: function(){ 
      alert("failure"); 
      } 
       }); 
    }); 
}); 
</script> 

php腳本

<?php 
require_once('../core/dbconfig.php'); 
$dbc = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASS, DB_NAME); 
$nume=$_POST['nume']; 
$bday=$_POST['bday']; 
$locatie=$_POST['locatie']; 
$status=$_POST['status']; 
$interese=$_POST['interese']; 
$despre=$_POST['descriere']; 
$id_user=$_POST['id_user']; 

$query="Update `users` SET username='$nume',bday='$bday',Locatie='$locatie',Relatie='$status',Interese='$interese',Descriere='$despre' where id='$id_user' "; 
           $result=mysqli_query($dbc,$query) or die("query failed: " . mysqli_error($dbc)); 


$path = '../../images/users/'.$id_user.'/'; 
if(!is_dir($path)){ 
    mkdir($path,0755); 
} 
    $valid_formats = array("jpg", "png", "gif", "bmp", "jpeg", "JPG"); 

      $name = $_FILES['img1']['name']; 
      $size = $_FILES['img1']['size']; 


      if(strlen($name)) 
       { 
        list($txt, $ext) = explode(".", $name); 
        if(in_array($ext,$valid_formats)) 
        { 
        if($size<(1024*1024)) 
         { 
          /*$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;*/ 
          $actual_image_name=$id_user.'.'.$ext; 
          $tmp = $_FILES['img1']['tmp_name']; 
          if(move_uploaded_file($tmp, $path.$actual_image_name)) 
           { 
           $queryz="Insert into Galerie (ID_User,Poza,Poza_Profil) VALUES ('$id_user','$actual_image_name',1)";  
           $resultz=mysqli_query($dbc,$queryz) or die("query failed: " . mysqli_error($dbc));        
           } 
          else 
           echo "failed"; 
         } 
         else 
         echo "Image file size max 1 MB";      
         } 
         else 
         echo "Invalid file format.."; 
       } 



echo json_encode(array("a" => $bday, "b" => $locatie,"c" => $despre,"d" => $interese,"e" => $name,"f" => "")); 
?> 
+0

簡單的ajax函數不起作用用於文件上傳。請參閱http://stackoverflow.com/questions/11116766/php-jquery-image-upload-not-working?rq=1等帖子瞭解更多信息。 – sri

回答

-1

由於只有Ajax請求,你可以把文件上傳到服務器。一個更好的選擇是使用jQuery Form

此鏈接還包括一些示例代碼,請按照文檔和實現你想要的。

看看如何實現jQuery Form插件,在Ajax based image upload

+0

Thnx,我爲上傳圖片添加了一個腳本。 Coudnt修改我的我並不擅長ajax,我也想讓json迴歸。但它也適用於2個腳本,所以thnx的幫助:)。 ' – user3058067

+0

@Downvoters我知道在這裏投票的原因是什麼? –

1

序列化的方法把你的表單字段數據符合所使用的應用程序/ x-WWW的形式urlencoded的內容類型提交表單服務器字符串用於處理,而文件是以multipart/form-data內容類型編碼的請求提交的,所以serialize會忽略文件輸入。

您應該使用FORMDATA

var form = new FormData(document.getElementById('your_frm_id')); 
var file = document.getElementById('img1').files[0]; 
    if (file) { 
     $('#sent_progress').show(); 
     form.append('img1', file); 
    } 

在AJAX使用

data: form, 

在這裏閱讀更多https://developer.mozilla.org/en-US/docs/Web/Guide/Using_FormData_Objects

或者你可以使用一些jQuery插件上傳您的文件,表格 希望它會幫助

+0

thnx,這是很好的信息,但我無法修改我的腳本,我只是做了一個新的上傳圖像。所以實際上在1提交我打電話2函數一個正常的信息和1上傳:) – user3058067